You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨项目服务账号部署Firebase Hosting时指定项目失效的解决方法

问题描述
  • 场景:使用跨项目共享部署服务账号部署Firebase Hosting,已通过GOOGLE_APPLICATION_CREDENTIALS环境变量指定该服务账号密钥,且该账号在目标项目中拥有Firebase App Distribution管理员及所有必要权限。
  • 执行部署命令:
    ./node_modules/.bin/firebase deploy --only hosting:$ENVIRONMENT --project ${PROJECT_NAME} --non-interactive
    
  • 报错信息:系统尝试访问服务账号所属项目(而非命令指定的目标项目),提示该项目未启用Firebase Hosting API:
    Error: HTTP Error: 403, Firebase Hosting API has not been used in project 49XXYYZZ628 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firebasehosting.googleapis.com/overview?project=497XXYYZZ628 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
    
  • 已尝试但无效的操作:
    • gcloud config set project ${PROJECT_NAME}
    • firebase use --project ${PROJECT_NAME}
解决方案
  1. 确认服务账号在目标项目的IAM配置
    跨项目服务账号需在目标项目的IAM中明确添加并授权:

    • 登录目标项目的IAM控制台,添加服务账号邮箱,授予Firebase Hosting Admin和Editor权限(或最小必要权限集合)
    • 确认服务账号的身份已正确关联到目标项目,而非仅持有密钥文件
  2. 通过令牌强制绑定目标项目上下文
    绕过本地环境的默认项目设置,直接用服务账号生成目标项目的访问令牌,在部署命令中指定:

    • 生成目标项目的访问令牌:
      gcloud auth application-default print-access-token --impersonate-service-account=你的服务账号邮箱@所属项目.iam.gserviceaccount.com --project ${PROJECT_NAME}
      
    • 带令牌执行部署:
      ./node_modules/.bin/firebase deploy --only hosting:$ENVIRONMENT --project ${PROJECT_NAME} --non-interactive --token $(gcloud auth application-default print-access-token --impersonate-service-account=你的服务账号邮箱@所属项目.iam.gserviceaccount.com --project ${PROJECT_NAME})
      
  3. 清理firebase.json的硬编码项目配置
    检查firebase.json中是否存在"project"字段,如果有则删除该字段,避免命令行的--project参数被配置文件覆盖。

  4. 临时启用服务账号所属项目的Firebase Hosting API
    若上述方法仍无效,可临时启用服务账号所属项目的Firebase Hosting API——Firebase CLI在验证服务账号权限时,可能会检查账号所属项目的API状态,启用后再尝试部署到目标项目即可。

内容的提问来源于stack exchange,提问作者Jijo John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 12:00:59