如何完全自动化 Google Cloud Build 触发器创建

How to fully automate the Google Cloud Build trigger creation

我尝试通过 sh 脚本完全自动化创建云构建触发器

作为来源,我使用 Github。

到目前为止可以创建触发器

gcloud beta builds triggers create github \
  --repo-name=organisation/repo \
  --repo-owner=organisation \
  --branch-pattern="^main$" \
  --build-config=cloudbuild.yaml

但是每个 repo 都必须手动授权,否则你会得到错误:

ERROR: (gcloud.beta.builds.triggers.create.github) FAILED_PRECONDITION: Repository mapping does not exist. Please visit https://console.cloud.google.com/cloud-build/triggers/connect?project=********* to connect a repository to your project

将我链接到 UI 以手动创建授权

有没有办法使该步骤也自动化?

如果您使用另一个托管 Git 提供商,例如 GitHub 或 Bitbucket,并且仍然需要将存储库镜像到 Cloud Source Repositories,您必须拥有 cloudbuilds.builds.create您正在使用的 Google Cloud 项目的权限。此权限通常通过 cloudbuild.builds.editor 角色授予。

这里有一些链接 information. Creating and managing build trigger

目前无法使用 API 连接到外部存储库,但正在进行 feature request 以实现此目的。

您现在有两种选择:

  1. 从 Cloud Console 一次连接所有存储库。这样,您将能够自动为这些存储库创建触发器。

  2. 使用 Cloud Source Repositories,默认连接到 Cloud Build,如 here. Check this documentation 如何在 CSR 中创建远程存储库来自本地 git 存储库。