Cloud Build 可以通过向所选渠道发送通知来通知您构建更新。 本页介绍如何使用 GitHub Issues 通知程序配置通知。
准备工作
-
Enable the Cloud Build, Compute Engine, Cloud Run, Pub/Sub, and Secret Manager APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 安装 Google Cloud CLI。
配置 GitHub 问题通知
以下部分介绍如何使用 GitHub Issues 通知程序来手动配置 GitHub Issue 通知。如果您希望改为自动配置,请参阅自动配置通知。
如需配置 GitHub 问题,请执行以下操作:
创建 GitHub 个人访问令牌:
- 前往 GitHub 设置以创建新令牌。
选择
repo范围。点击生成令牌
将 GitHub 令牌存储在 Secret Manager 中:
在 Google Cloud 控制台中打开 Secret Manager 页面:
点击创建密钥。
为您的密钥输入一个名称。
在密钥值下,添加您的 GitHub 令牌。
如需保存您的密钥,请点击创建密钥。
虽然您的 Cloud Run 服务账号可能拥有项目的 Editor 角色,但 Editor 角色不足以访问 Secret Manager 中的 Secret。如需为 Cloud Run 服务账号授予对您的 Secret 的访问权限,请执行以下操作:
前往 Google Cloud 控制台中的 IAM 页面:
找到与您的项目关联的 Compute Engine 默认服务账号:
您的 Compute Engine 默认服务账号将如下所示:
project-number-compute@developer.gserviceaccount.com记下您的 Compute Engine 默认服务账号。
在 Google Cloud 控制台中打开 Secret Manager 页面:
点击包含 GitHub 令牌的密钥名称。
在权限标签页中,点击添加成员。
将与项目关联的 Compute Engine 默认服务账号添加为成员。
选择 Secret Manager Secret Accessor 权限作为角色。
点击保存。
向 Cloud Run 服务账号授予从 Cloud Storage 存储分区读取数据的权限:
前往 Google Cloud 控制台中的 IAM 页面:
找到与您的项目关联的 Compute Engine 默认服务账号:
您的 Compute Engine 默认服务账号将如下所示:
project-number-compute@developer.gserviceaccount.com点击您的 Compute Engine 默认服务账号所在行中的铅笔图标。 您将会看到修改访问权限标签页。
点击添加其他角色。
添加以下角色:
- Storage Object Viewer
点击保存。
编写模板配置文件,以描述创建的 GitHub 问题应采用的格式:
在以下示例模板配置文件中,
title和body字段使用了来自 build 的替换变量:{ "title": "Build {{.Build.BuildTriggerId}}: {{.Build.Status}}", "body": "[{{.Build.ProjectId}}] {{.Build.BuildTriggerId}} status: **{{.Build.Status}}**\n\n[View Logs]({{.Build.LogUrl}})" }如需查看此示例,请参阅 GitHub 问题通知程序的模板配置文件。
您可以从 GitHub API 端点(用于创建问题)的可用正文参数中设置其他字段。
编写通知程序配置文件以配置您的 GitHub 问题通知程序并过滤构建事件:
在以下示例通知程序配置文件中,
filter字段使用通用表达式语言和可用变量build来过滤状态为SUCCESS的构建事件:apiVersion: cloud-build-notifiers/v1 kind: GitHubIssuesNotifier metadata: name: example-githubissues-notifier spec: notification: filter: build.status == Build.Status.FAILURE template: type: golang uri: gs://bucket_name/template-file-name delivery: githubToken: secretRef: github-token githubRepo: myuser/myrepo secrets: - name: github-token value: projects/project-id/secrets/secret-name/versions/latest其中:
githubToken是此示例中用于引用 Secret Manager 中存储的 GitHub 令牌的配置变量。此处指定的变量名称应与secrets下的name字段匹配。bucket-name是您的存储分区的名称。template-file-name是模板文件的名称。myuser/myrepo是要针对其创建问题的代码库的名称。project-id是您的 Google Cloud 项目的 ID。secret-name是您的 Secret 的名称,其中包含您的 GitHub 令牌。
如需查看此示例,请参阅 GitHub Issues 通知程序的通知程序配置文件。
如需了解可用于过滤的其他字段,请参阅构建资源。 如需了解其他过滤示例,请参阅使用 CEL 过滤构建事件。
将通知程序配置文件和模板文件上传到 Cloud Storage 存储桶:
如果您没有 Cloud Storage 存储桶,请运行以下命令创建一个存储桶,其中 bucket-name 是您想要为存储桶指定的名称(须遵循命名要求部分)。
gcloud storage buckets create gs://bucket-name/将通知程序配置文件和模板文件上传到您的存储桶:
gcloud storage cp config-file-name gs://bucket-name/config-file-name gcloud storage cp template-file-name gs://bucket-name/template-file-name其中:
bucket-name是您的存储分区的名称。config-file-name是您的配置文件的名称。template-file-name是模板文件的名称。
将通知程序部署到 Cloud Run:
gcloud run deploy service-name \ --image=us-east1-docker.pkg.dev/gcb-release/cloud-build-notifiers/githubissues:latest \ --no-allow-unauthenticated \ --update-env-vars=CONFIG_PATH=config-path,PROJECT_ID=project-id其中:
service-name是您要将映像部署到的 Cloud Run 服务的名称。config-path是 GitHub Issues 通知程序的通知程序配置文件的路径,gs://bucket-name/config-file-name。project-id是您的 Google Cloud 项目的 ID。
gcloud run deploy命令会从 Cloud Build 拥有的 Artifact Registry 拉取最新版本的托管映像。Cloud Build 支持通知程序映像 9 个月。9 个月后,Cloud Build 会删除映像版本。如果您想使用旧的映像版本,则需要在gcloud run deploy命令的image属性中指定映像标记的完整语义版本。您可以在 Artifact Registry 中找到以前的映像版本和标记。授予 Pub/Sub 在您的 Google Cloud 项目中创建身份验证令牌的权限:
gcloud projects add-iam-policy-binding project-id \ --member=serviceAccount:service-project-number@gcp-sa-pubsub.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountTokenCreator其中:
project-id是您的 Google Cloud 项目的 ID。project-number是您的 Google Cloud 项目编号。
创建一个服务账号以表示您的 Pub/Sub 订阅身份:
gcloud iam service-accounts create cloud-run-pubsub-invoker \ --display-name "Cloud Run Pub/Sub Invoker"您可以使用
cloud-run-pubsub-invoker,也可以使用 Google Cloud 项目内唯一的名称。向
cloud-run-pubsub-invoker服务账号授予 Cloud RunInvoker权限:gcloud run services add-iam-policy-binding service-name \ --member=serviceAccount:cloud-run-pubsub-invoker@project-id.iam.gserviceaccount.com \ --role=roles/run.invoker其中:
service-name是您要将映像部署到的 Cloud Run 服务的名称。project-id是您的 Google Cloud 项目的 ID。
创建
cloud-builds主题以接收通知程序的 build 更新消息:gcloud pubsub topics create cloud-builds您还可以在 build 配置文件中定义自定义主题名称,以便将消息发送到自定义主题。在这种情况下,您将创建一个具有相同自定义主题名称的主题:
gcloud pubsub topics create topic-name如需了解详情,请参阅构建通知的 Pub/Sub 主题。
为通知程序创建 Pub/Sub 推送订阅者:
gcloud pubsub subscriptions create subscriber-id \ --topic=cloud-builds \ --push-endpoint=service-url \ --push-auth-service-account=cloud-run-pubsub-invoker@project-id.iam.gserviceaccount.com其中:
subscriber-id是您要为订阅指定的名称。service-url是 Cloud Run 为新服务生成的网址。project-id是您的 Google Cloud 项目的 ID。
您的 Cloud Build 项目的通知现已设置完毕。下次您调用构建时,如果构建与您已配置的过滤条件匹配,系统将针对定义的 GitHub 代码库创建问题。
使用 CEL 过滤构建事件
Cloud Build 在构建资源中列出的字段上搭配使用 CEL 与变量 build,以便访问与构建事件关联的字段,例如触发器 ID、图片列表或替换值。您可以使用 filter 字符串,通过构建资源中列出的任何字段,来过滤通构建配置文件中的构建事件。如需查找与您的字段关联的具体语法,请参阅 cloudbuild.proto 文件。
按触发器 ID 过滤
要按触发器 ID 过滤,请使用 build.build_trigger_id 在 filter 字段中指定触发器 ID 的值,其中 trigger-id 是字符串形式的触发器 ID:
filter: build.build_trigger_id == trigger-id
按状态过滤
要按状态过滤,请使用 build.status 在 filter 字段中指定要过滤的构建状态。
以下示例展示了如何使用 filter 字段过滤状态为 SUCCESS 的构建事件:
filter: build.status == Build.Status.SUCCESS
您也可以过滤具有不同状态的构建。以下示例展示了如何使用 filter 字段过滤状态为 SUCCESS、FAILURE 或 TIMEOUT 的构建事件:
filter: build.status in [Build.Status.SUCCESS, Build.Status.FAILURE, Build.Status.TIMEOUT]
要查看您可以作为过滤依据的其他状态值,请参阅“构建资源参考”下的状态。
按标记过滤
要按标记过滤,请使用 build.tags 在 filter 字段中指定标记的值,其中 tag-name 是标记的名称:
filter: tag-name in build.tags
您可以使用 size 根据构建事件中指定的标记数量进行过滤。在以下示例中,filter 字段会过滤仅指定了两个标记且其中一个标记指定为 v1 的构建事件:
filter: size(build.tags) == 2 && "v1" in build.tags
按图片过滤
如需按映像过滤,请使用 build.images 在 filter 字段中指定映像的值,其中 image-name 是 Artifact Registry 中列出的映像的全名,例如 us-east1-docker.pkg.dev/my-project/docker-repo/image-one:
filter: image-name in build.images
在以下示例中,filter 会过滤将 us-east1-docker.pkg.dev/my-project/docker-repo/image-one 或 us-east1-docker.pkg.dev/my-project/docker-repo/image-two 指定为映像名称的构建事件:
filter: "us-east1-docker.pkg.dev/my-project/docker-repo/image-one" in build.images || "us-east1-docker.pkg.dev/my-project/docker-repo/image-one" in build.images
按时间过滤
您可以在 filter 字段中指定以下某个选项,以根据构建的创建时间、开始时间或结束时间过滤构建事件:build.create_time、build.start_time 或 build.finish_time。
在以下示例中,filter 字段会使用 timestamp 来过滤创建构建的请求时间为 2020 年 7 月 20 日上午 6:00 的构建事件。
filter: build.create_time == timestamp("2020-07-20:T06:00:00Z")
您还可以按时间比较过滤构建事件。在以下示例中,filter 字段会使用 timestamp 过滤开始时间介于 2020 年 7 月 20 日上午 6:00 到 2020 年 7 月 30 日上午 6:00 之间的构建事件。
filter: timestamp("2020-07-20:T06:00:00Z") >= build.start_time && build.start_time <= timestamp("2020-07-30:T06:00:00Z")
如需详细了解 CEL 中的时区表示方式,请参阅时区的语言定义。
要按构建的持续时间进行过滤,您可以使用 duration 比较时间戳。
在以下示例中,filter 字段会使用 duration 过滤包含至少运行了五分钟的构建的构建事件:
filter: build.finish_time - build.start_time >= duration("5m")
按替换过滤
您可以使用 build.substitutions 在 filter 字段中指定替换变量,从而按替换进行过滤。在以下示例中,filter 字段会列出包含替代变量 substitution-variable 的构建,并检查 substitution-variable 是否与指定的 substitution-value 匹配:
filter: build.substitutions[substitution-variable] == substitution-value
其中:
substitution-variable是替代变量的名称。substitution-value是替代变量值的名称。
您还可以按默认替换变量值进行过滤。在下面的示例中,filter 字段列出分支名称为 master 的构建,以及代码库名称为 github.com/user/my-example-repo 的构建。默认替换变量 BRANCH_NAME 和 REPO_NAME 将作为密钥传递给 build.substitutions:
filter: build.substitutions["BRANCH_NAME"] == "master" && build.substitutions["REPO_NAME"] == "github.com/user/my-example-repo"
如果要使用正则表达式过滤字符串,则可以使用内置的 matches 函数。在以下示例中,filter 字段过滤状态为 FAILURE 或 TIMEOUT 的构建,并且有一个构建替代变量 TAG_NAME,以及一个与正则表达式 v{DIGIT}.{DIGIT}.{3 DIGITS}) 匹配的值。
filter: build.status in [Build.Status.FAILURE, Build.Status.TIMEOUT] && build.substitutions["TAG_NAME"].matches("^v\\d{1}\\.\\d{1}\\.\\d{3}$")
要查看默认替换值的列表,请参阅使用默认替换。
后续步骤
- 了解 Cloud Build 通知程序。
- 了解如何订阅构建通知。
- 了解如何编写 Cloud Build 构建配置文件。