您可以使用 Cloud Build 通知程序配置 Cloud Build,以将构建通知发送到 Slack、Google Chat、SMTP 服务器、HTTP 端点或 BigQuery 实例。本页面介绍如何为所需的通知程序自动执行配置过程。
自动通知配置
Cloud Build 提供设置脚本,可用于自动通知配置。要使用设置脚本配置通知,请执行以下操作:
Slack
设置
以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。
正在启用 API
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 the serviceusage.services.enable permission. Learn how to grant
          roles.
        
获取和存储凭据
- 为要向其发送通知的 Slack 工作区创建 Slack 应用。 
- 激活传入的网络钩子,将消息从 Cloud Build 发布到 Slack。 
- 导航到您的 Slack 应用,找到传入的网络钩子的网址。您的网址将如下所示: - http://hooks.slack.com/services/...
- 将传入的网络钩子网址存储在 Secret Manager 中: - 在 Google Cloud 控制台中打开 Secret Manager 页面: 
- 点击创建密钥。 
- 为您的密钥输入一个名称。 
- 在密钥值下,为您的 Slack 应用添加传入的网络钩子网址。 
- 如需保存您的密钥,请点击创建密钥。 
 
编写通知程序配置文件
编写通知程序配置文件以配置您的 Slack 通知程序并过滤构建事件:
在以下示例通知程序配置文件中,filter 字段使用通用表达式语言和可用变量 build 来过滤状态为 SUCCESS 的构建事件:
  apiVersion: cloud-build-notifiers/v1
  kind: SlackNotifier
  metadata:
    name: example-slack-notifier
  spec:
    notification:
      filter: build.status == Build.Status.SUCCESS
      delivery:
        webhookUrl:
          secretRef: webhook-url
      template:
        type: golang
        uri: gs://example-gcs-bucket/slack.json
    secrets:
    - name: webhook-url
      value: projects/project-id/secrets/secret-name/versions/latest
其中:
- webhook-url是此示例中引用 Secret Manager 中存储的 Slack 网络钩子网址路径的配置变量。此处指定的变量名称应与- secrets下的- name字段匹配。
- project-id是您的 Google Cloud 项目的 ID。
- secret-name是您的 Secret 的名称,其中包含 Slack Webhook 网址。
- uri字段引用- slack.json文件。此文件包含托管在 Cloud Storage 上的 JSON 模板,表示您发送给 Slack 空间的通知消息。
如需查看此示例,请参阅 Slack 通知程序的通知程序配置文件。
运行自动化脚本
如需自动配置通知,请执行以下操作:
- 使用您的项目 ID 和区域配置 Google Cloud CLI: - gcloud config set project project-id gcloud config set run/region region- 其中: - project-id是 Google Cloud 项目 ID。
- region是要部署通知程序的区域。
 
- 在代码库的根目录中运行以下命令: 
    ./setup.sh slack config-path -t template-path -s secret-name
其中:
- config-path是通知程序配置文件的路径。
- template-path是通知程序模板文件的路径。您的通知程序模板文件包含托管在 Cloud Storage 上的 JSON 模板,并表示您的通知消息。您可以使用此变量或在通知程序配置文件的- uri字段中以路径的形式添加通知程序模板文件。
- secret-name是 Secret Manager 中存储的 Secret 的名称。
运行脚本后,您将看到以下消息:
** NOTIFIER SETUP COMPLETE **
现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 ./setup.sh --help,了解与该脚本关联的使用说明。
SMTP
设置
以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。
正在启用 API
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 the serviceusage.services.enable permission. Learn how to grant
          roles.
        
存储凭据
- 将发件人的电子邮件账号密码存储在 Secret Manager 中: 
- 在 Google Cloud 控制台中打开 Secret Manager 页面: 
- 点击创建密钥。 
- 为您的密钥输入一个名称。 
- 在密钥值下,添加发件人的电子邮件账号密码。 
- 如需保存您的密钥,请点击创建密钥。 
编写通知程序配置文件
编写通知程序配置文件以配置您的 SMTP 通知程序并过滤构建事件:
在以下示例通知程序配置文件中,filter 字段使用通用表达式语言和可用变量 build 来过滤状态为 SUCCESS 的构建事件:
 apiVersion: cloud-build-notifiers/v1
 kind: SMTPNotifier
 metadata:
   name: example-smtp-notifier
 spec:
   notification:
     filter: build.status == Build.Status.SUCCESS
     delivery:
       server: server-host-name
       port: "port"
       sender: sender-email
       from: from-email
       recipients:
         - recipient-email
         # optional: more emails here
       password:
         secretRef: smtp-password
      template:
        type: golang
        uri: gs:example-gcs-bucket/smtp.html
   secrets:
   - name: smtp-password
     value: projects/project-id/secrets/secret-name/versions/latest
其中:
- server-host-name是 SMTP 服务器的地址。
- port是处理 SMTP 请求的端口。此值应指定为字符串。
- sender-email是指定的 server-host-name 看到的发件人账号的电子邮件地址。
- from-email是收件人看到的电子邮件地址。
- recipient-email是接收来自发件人的消息的一个或多个电子邮件地址的列表。
- smtp-password是此示例中用于引用 Secret Manager 中存储的发件人的电子邮件账号密码的配置变量。此处指定的变量名称应与- secrets下的- name字段匹配。
- project-id是您的 Google Cloud 项目的 ID。
- secret-name是您的 Secret 的名称,其中包含发件人的电子邮件账号的密码。
- uri字段引用- smtp.html文件。此文件是指托管在 Cloud Storage 上的 HTML 模板,表示您的通知电子邮件。
如需查看此示例,请参阅 SMTP 通知程序的通知程序配置文件。
运行自动化脚本
如需自动配置通知,请执行以下操作:
- 使用您的项目 ID 和区域配置 Google Cloud CLI: - gcloud config set project project-id gcloud config set run/region region- 其中: - project-id是 Google Cloud 项目 ID。
- region是要部署通知程序的区域。
 
- 在代码库的根目录中运行以下命令: 
    ./setup.sh smtp config-path -t template-path -s secret-name
其中:
- config-path是通知程序配置文件的路径。
- template-path是通知程序模板文件的路径。您的通知程序模板文件包含托管在 Cloud Storage 上的 JSON 模板,并表示您的通知消息。您可以使用此变量或在通知程序配置文件的- uri字段中以路径的形式添加通知程序模板文件。
- secret-name是 Secret Manager 中存储的 Secret 的名称。
运行脚本后,您将看到以下消息:
** NOTIFIER SETUP COMPLETE **
现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 ./setup.sh --help,了解与该脚本关联的使用说明。
BigQuery
设置
以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。
正在启用 API
Enable the Cloud Build, Cloud Run, Pub/Sub, and BigQuery APIs.
Roles required to enable APIs
          To enable APIs, you need the Service Usage Admin IAM
          role (roles/serviceusage.serviceUsageAdmin), which
          contains the serviceusage.services.enable permission. Learn how to grant
          roles.
        
授予权限
向 Cloud Run 服务账号授予创建和写入 BigQuery 表的权限,以及获取与构建相关的 Artifact Registry 数据的权限:
- 前往 Google Cloud 控制台中的 IAM 页面: 
- 找到与您的项目关联的 Compute Engine 默认服务账号: - 您的 Compute Engine 默认服务账号将如下所示,其中 project-number 是您的项目编号: - project-number-compute@developer.gserviceaccount.com
- 点击您的 Compute Engine 默认服务账号所在行中的铅笔图标。 - 您将会看到修改访问权限标签页。 - 点击添加其他角色。 
- 添加以下角色: - Artifact Registry Reader
- BigQuery Data Editor - 借助 Artifact Registry Reader 角色,您可以针对映像提取数据。BigQuery Data Editor 为您提供数据的读写访问权限。 
 
- 点击保存。 
 
编写通知程序配置文件
编写通知程序配置文件以配置您的 BigQuery 通知程序并过滤构建事件:
在以下示例通知程序配置文件中,filter 字段使用通用表达式语言和变量 build 来过滤具有指定触发器 ID 的构建事件:
 apiVersion: cloud-build-notifiers/v1
 kind: BigQueryNotifier
 metadata:
   name: example-bigquery-notifier
 spec:
   notification:
     filter: build.build_trigger_id == "123e4567-e89b-12d3-a456-426614174000"
     delivery:
       table: projects/project-id/datasets/dataset-name/tables/table-name
     template:
       type: golang
       uri: gs://example-gcs-bucket/bq.json
其中:
- project-id是您的 Google Cloud 项目的 ID。
- dataset-name是您要为数据集指定的名称。
- table-name是您要为表指定的名称。
通知程序配置文件中的 table-name 可以参考:
- 不存在的表
- 没有架构的空表
- 具有与 BigQuery 通知程序中的架构规范匹配的架构的现有表 
- uri字段引用- bq.json文件。此文件引用了 Cloud Storage 上托管的 JSON 模板,并表示要插入到 BigQuery 表中的信息。
如需查看此示例,请参阅 BigQuery 通知程序的通知程序配置文件。
运行自动化脚本
如需自动配置通知,请执行以下操作:
- 使用您的项目 ID 和区域配置 Google Cloud CLI: - gcloud config set project project-id gcloud config set run/region region- 其中: - project-id是 Google Cloud 项目 ID。
- region是要部署通知程序的区域。
 
- 在代码库的根目录中运行以下命令: - ./setup.sh bigquery -t config-path -t template-path- 其中: - config-path是通知程序配置文件的路径。
- template-path是通知程序模板文件的路径。您的通知程序模板文件包含托管在 Cloud Storage 上的 JSON 模板,并表示您的通知消息。您可以使用此变量以路径形式包含通知程序模板文件,也可以在通知程序配置文件的- uri字段中包含该文件。
 - 运行脚本后,您将看到以下消息: - ** NOTIFIER SETUP COMPLETE **- 现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 - ./setup.sh --help,了解与该脚本关联的使用说明。
HTTP
设置
以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。
正在启用 API
Enable the Cloud Build, Cloud Run, and Pub/Sub APIs.
Roles required to enable APIs
          To enable APIs, you need the Service Usage Admin IAM
          role (roles/serviceusage.serviceUsageAdmin), which
          contains the serviceusage.services.enable permission. Learn how to grant
          roles.
        
编写通知程序配置文件
编写通知程序配置文件以配置您的 HTTP 通知程序并过滤构建事件:
在以下示例通知程序配置文件中,filter 字段使用通用表达式语言和可用变量 build 来过滤状态为 SUCCESS 的构建事件:
    apiVersion: cloud-build-notifiers/v1
    kind: HTTPNotifier
    metadata:
      name: example-http-notifier
    spec:
      notification:
        filter: build.status == Build.Status.SUCCESS
        delivery:
          # The `http(s)://` protocol prefix is required.
          url: url
        template:
          type: golang
          uri: gs://example-gcs-bucket/http.json
其中:
- url是此示例中用于指定请求网址的配置变量。
- url 是您想要指定为收件人服务器的网址。
- uri字段引用- http.json文件。此文件是指 Cloud Storage 上托管的 JSON 模板,表示 webhook 端点的 JSON 载荷。
如需查看此示例,请参阅 HTTP 通知程序的通知程序配置文件。
运行自动化脚本
如需自动配置通知,请执行以下操作:
- 使用您的项目 ID 和区域配置 Google Cloud CLI: - gcloud config set project project-id gcloud config set run/region region- 其中: - project-id是 Google Cloud 项目 ID。
- region是要部署通知程序的区域。
 
- 在代码库的根目录中运行以下命令: - ./setup.sh http -t config-path- 其中: - config-path是通知程序配置文件的路径。
 
运行脚本后,您将看到以下消息:
** NOTIFIER SETUP COMPLETE **
现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 ./setup.sh --help,了解与该脚本关联的使用说明。
Google Chat
设置
以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。
正在启用 API
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 the serviceusage.services.enable permission. Learn how to grant
          roles.
        
获取和存储凭据
- 在 Google Chat 中创建聊天室。 
- 在创建的聊天室中,创建传入的网络钩子,以便将消息从 Cloud Build 发布到 Google Chat。您的网址将如下所示: - https://chat.googleapis.com/v1/spaces/... 
- 将传入的网络钩子网址存储在 Secret Manager 中: - 在 Google Cloud 控制台中打开 Secret Manager 页面: 
- 点击创建密钥。 
- 为您的密钥输入一个名称。 
- 在密钥值下,为您的 Google Chat 聊天室添加传入的网络钩子网址。 
- 如需保存您的密钥,请点击创建密钥。 
 
编写通知程序配置文件
编写通知程序配置文件以配置您的 Google Chat 通知程序并过滤构建事件:
在以下示例通知程序配置文件中,filter 字段使用通用表达式语言和可用变量 build 来过滤状态为 SUCCESS 的构建事件:
  apiVersion: cloud-build-notifiers/v1
  kind: GoogleChatNotifier
  metadata:
    name: example-googlechat-notifier
  spec:
    notification:
      filter: build.status == Build.Status.SUCCESS
      delivery:
        webhookUrl:
          secretRef: webhook-url
    secrets:
    - name: webhook-url
      value: projects/project-id/secrets/secret-name/versions/latest
其中:
- webhook-url是此示例中用于引用 Secret Manager 中存储的 Google Chat 网络钩子网址路径的配置变量。此处指定的变量名称应与- secrets下的- name字段匹配。
- project-id是您的 Google Cloud 项目的 ID。
- secret-name是您的 Secret 的名称,其中包含 Google Chat Webhook 网址。
如需查看此示例,请参阅 Google Chat 通知程序的通知程序配置文件。
运行自动化脚本
如需自动配置通知,请执行以下操作:
- 使用您的项目 ID 和区域配置 Google Cloud CLI: - gcloud config set project project-id gcloud config set run/region region- 其中: - project-id是 Google Cloud 项目 ID。
- region是要部署通知程序的区域。
 
- 在代码库的根目录中运行以下命令: 
./setup.sh googlechat config-path -s secret-name
其中:
- config-path是通知程序配置文件的路径。
- secret-name是 Secret Manager 中存储的 Secret 的名称。
运行脚本后,您将看到以下消息:
** NOTIFIER SETUP COMPLETE **
现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 ./setup.sh --help,了解与该脚本关联的使用说明。
GitHub 问题
设置
以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。
正在启用 API
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 the serviceusage.services.enable permission. Learn how to grant
          roles.
        
获取和存储凭据
- 创建 GitHub 个人访问令牌: - 前往 GitHub 设置以创建新令牌。
- 选择 - repo范围。
- 点击生成令牌 
 
- 在 Secret Manager 中存储令牌: - 在 Google Cloud 控制台中打开 Secret Manager 页面: 
- 点击创建密钥。 
- 为您的密钥输入一个名称。 
- 在密钥值下,添加您的 GitHub 令牌。 
- 如需保存您的密钥,请点击创建密钥。 
 
编写模板配置文件
编写模板配置文件,以描述创建的 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 端点(用于创建问题)的可用正文参数中设置其他字段。
编写通知程序配置文件
编写通知程序配置文件以配置您的 Google Chat 通知程序并过滤构建事件:
在以下示例通知程序配置文件中,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://project-id-notifiers-config/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字段匹配。
- project-id-notifiers-config是模板的上传位置,如果该存储桶尚不存在,系统会创建该存储桶。
- template-file-name是模板文件的名称。
- myuser/myrepo是将针对其创建问题的代码库的名称。
- project-id是您的 Google Cloud 项目的 ID。
- secret-name是您的 Secret 的名称,其中包含您的 GitHub 令牌。
如需查看此示例,请参阅 Google Chat 通知程序的通知程序配置文件。
如需了解可用于过滤的其他字段,请参阅构建资源。如需了解其他过滤示例,请参阅使用 CEL 过滤构建事件。
运行自动化脚本
如需自动配置通知,请执行以下操作:
- 使用您的项目 ID 和区域配置 Google Cloud CLI: - gcloud config set project project-id gcloud config set run/region region- 其中: - project-id是 Google Cloud 项目 ID。
- region是要部署通知程序的区域。
 
- 在代码库的根目录中运行以下命令: 
    ./setup.sh githubissues config-path -t template-path -s secret-name
其中:
- config-path是通知程序配置文件的路径。
- template-path是通知程序模板文件的路径。您的通知程序模板文件包含托管在 Cloud Storage 上的 JSON 模板,并表示您的通知消息。您可以使用此变量或在通知程序配置文件的- uri字段中以路径的形式添加通知程序模板文件。
- secret-name是 Secret Manager 中存储的 Secret 的名称。
运行脚本后,您将看到以下消息:
** NOTIFIER SETUP COMPLETE **
现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 ./setup.sh --help,了解与该脚本关联的使用说明。
后续步骤
- 了解 Cloud Build 通知程序。
- 了解如何订阅构建通知。
- 了解如何编写 Cloud Build 构建配置文件。