更正 Google Cloud Build 的权限以在单独的项目中部署 Cloudfunction

Correct permissions for Google Cloud Build to deploy a Cloudfunction in a separate project

我们有针对 "build" 和 "prod" 环境的单独 GCP 项目。我想为 "build" 项目使用 Cloud Build 在 "prod" 项目中部署 Cloud Functions。当跟随documentation

值得注意的是,我将 "Cloud Functions Developer" 角色添加到构建项目中的构建服务帐户,并将 "IAM Service Account User" 角色添加到 PROJECT_ID@appspot.gserviceaccount.com 帐户,如中所述文档和 ,当 运行 构建时,我得到:

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Forbidden], message=[The caller does not have permission]

我的 cloudbuild.yaml 的一个简化示例是

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['source', 'repos', 'clone', 'a_mirrored_repo', '--project=build-xxxx']

 - name: 'gcr.io/cloud-builders/gcloud'
   args: ['functions', 'deploy', 'some_function', '--trigger-http', '--runtime', 'python37', '--project', 'prod-yyyy']

我能够使用笔记本电脑上的 gsutil 命令行实用程序将我的功能部署到 prod,并且我能够使用我的 cloudbuild.yaml 将此功能部署到 build 项目。但我不确定我需要将哪些角色分配给哪些帐户才能使构建项目能够将 cloudfunction 部署到 prod 项目。

如果要在 prod 项目中部署该函数,则必须将构建项目的 Cloud Build 服务帐户添加为 prod 项目中的 "Cloud Functions Developer"。如果我对你的描述理解正确,那么你已经在构建项目中设置了它。

具体来说:在 GCP 控制台的 prod 项目中,在 IAM > members 下添加一个名为 [build-project-id]@cloudbuild.gserviceaccount.com 的新成员,并将角色 "Cloud Functions Developer" 添加到该成员。此时尝试部署可能会导致错误消息:

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Forbidden], message=[Missing necessary permission iam.serviceAccounts.actAs for prod-xxxx@appspot.gserviceaccount.com on project prod-xxxx.

Please grant prod-xxxx@appspot.gserviceaccount.com the roles/iam.serviceAccountUser role.

You can do that by running 'gcloud projects add-iam-policy-binding prod-xxxx --member=prod-xxxx@appspot.gserviceaccount.com --role=roles/iam.serviceAccountUser'

错误消息中建议的命令稍有不正确。正确的语法是:

gcloud projects add-iam-policy-binding prod-xxxx --member='serviceAccount:[build-project-number]@cloudbuild.gserviceaccount.com' --role='roles/iam.serviceAccountUser'