自动配置通知

您可以将 Cloud Build 配置为使用 Cloud Build 通知程序将构建通知发送到 Slack、Google Chat、SMTP 服务器、HTTP 端点或 BigQuery 实例。本页面介绍如何为所需的通知程序自动执行配置过程。

自动通知配置

Cloud Build 提供设置脚本,可用于自动通知配置。要使用设置脚本配置通知,请执行以下操作:

Slack

设置

以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。

正在启用 API

启用 Cloud Build, Compute Engine, Cloud Run, Pub/Sub, and Secret Manager API。

启用 API

获取和存储凭据

  1. 为要向其发送通知的 Slack 工作区创建 Slack 应用

  2. 激活传入的网络钩子,将消息从 Cloud Build 发布到 Slack。

  3. 导航到您的 Slack 应用,找到传入的网络钩子的网址。您的网址将如下所示:

    http://hooks.slack.com/services/...
    
  4. 将传入的网络钩子网址存储在 Secret Manager 中:

    1. 在 Google Cloud 控制台中打开 Secret Manager 页面:

      打开 Secret Manager 页面

    2. 点击创建密钥

    3. 为您的密钥输入一个名称。

    4. 密钥值下,为您的 Slack 应用添加传入的网络钩子网址。

    5. 如需保存您的密钥,请点击创建密钥

编写通知程序配置文件

编写通知程序配置文件以配置您的 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 通知程序的通知程序配置文件

运行自动化脚本

如需为通知程序自动配置通知,请执行以下操作:

  1. 克隆 cloud-build-notifiers 代码库

  2. 使用您的项目 ID 和区域配置 Google Cloud CLI:

    gcloud config set project project-id
    gcloud config set run/region region
    

    其中:

    • project-id 是您的 Google Cloud 项目 ID。
    • region 是部署通知程序的区域
  3. 在代码库的根目录中运行以下命令:

    ./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

启用 Cloud Build, Compute Engine, Cloud Run, Pub/Sub, and Secret Manager API。

启用 API

存储凭据

  1. 将发件人的电子邮件账号密码存储在 Secret Manager 中:

  2. 在 Google Cloud 控制台中打开 Secret Manager 页面:

    打开 Secret Manager 页面

  3. 点击创建密钥

  4. 为您的密钥输入一个名称。

  5. 密钥值下,添加发件人的电子邮件账号密码。

  6. 如需保存您的密钥,请点击创建密钥

编写通知程序配置文件

编写通知程序配置文件以配置您的 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 通知程序的通知程序配置文件

运行自动化脚本

如需为通知程序自动配置通知,请执行以下操作:

  1. 克隆 cloud-build-notifiers 代码库

  2. 使用您的项目 ID 和区域配置 Google Cloud CLI:

    gcloud config set project project-id
    gcloud config set run/region region
    

    其中:

    • project-id 是您的 Google Cloud 项目 ID。
    • region 是部署通知程序的区域
  3. 在代码库的根目录中运行以下命令:

    ./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

启用 Cloud Build, Cloud Run, Pub/Sub, and BigQuery API。

启用 API

授予权限

向 Cloud Run 服务账号授予创建和写入 BigQuery 表的权限,以及获取与构建相关的 Artifact Registry 数据的权限:

  1. 转到 Google Cloud 控制台中的 IAM 页面:

    打开 IAM 页面

  2. 找到与您的项目关联的 Compute Engine 默认服务账号

    您的 Compute Engine 默认服务账号将如下所示,其中 project-number 是您的项目编号:

        project-number-compute@developer.gserviceaccount.com
    
  3. 点击您的 Compute Engine 默认服务账号所在行中的铅笔图标。

    您将看到修改访问权限标签页。

    1. 点击添加其他角色

    2. 添加以下角色:

      • Artifact Registry Reader
      • BigQuery Data Editor

        借助 Artifact Registry Reader 角色,您可以针对映像提取数据。BigQuery Data Editor 为您提供数据的读写访问权限。

    3. 点击保存

编写通知程序配置文件

编写通知程序配置文件以配置您的 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 通知程序的通知程序配置文件

运行自动化脚本

如需为通知程序自动配置通知,请执行以下操作:

  1. 克隆 cloud-build-notifiers 代码库

  2. 使用您的项目 ID 和区域配置 Google Cloud CLI:

    gcloud config set project project-id
    gcloud config set run/region region
    

    其中:

    • project-id 是您的 Google Cloud 项目 ID。
    • region 是部署通知程序的区域
  3. 在代码库的根目录中运行以下命令:

     ./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

启用 Cloud Build, Cloud Run, and Pub/Sub API。

启用 API

编写通知程序配置文件

编写通知程序配置文件以配置您的 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 通知程序的通知程序配置文件

运行自动化脚本

如需为通知程序自动配置通知,请执行以下操作:

  1. 克隆 cloud-build-notifiers 代码库

  2. 使用您的项目 ID 和区域配置 Google Cloud CLI:

    gcloud config set project project-id
    gcloud config set run/region region
    

    其中:

    • project-id 是您的 Google Cloud 项目 ID。
    • region 是部署通知程序的区域
  3. 在代码库的根目录中运行以下命令:

     ./setup.sh http -t config-path
    

    其中:

    • config-path 是通知程序配置文件的路径。

运行脚本后,您将看到以下消息:

** NOTIFIER SETUP COMPLETE **

现在,你的通知程序已设置完毕。您可以在 cloud-build-notifiers 代码库中查看完整脚本,也可以运行 ./setup.sh --help,了解与该脚本关联的使用说明。

Google Chat

设置

以下部分介绍了在对通知程序进行自动通知配置前需要完成的步骤。

正在启用 API

启用 Cloud Build, Compute Engine, Cloud Run, Pub/Sub, and Secret Manager API。

启用 API

获取和存储凭据

  1. 在 Google Chat 中创建聊天室

  2. 在所创建的聊天室中,创建传入的网络钩子,将 Cloud Build 中的消息发布到 Google Chat。您的网址将如下所示:

    https://chat.googleapis.com/v1/spaces/...

  3. 将传入的网络钩子网址存储在 Secret Manager 中:

    1. 在 Google Cloud 控制台中打开 Secret Manager 页面:

      打开 Secret Manager 页面

    2. 点击创建密钥

    3. 为您的密钥输入一个名称。

    4. 密钥值下,为您的 Google Chat 聊天室添加传入的网络钩子网址。

    5. 如需保存您的密钥,请点击创建密钥

编写通知程序配置文件

编写通知程序配置文件,以配置 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 是包含 Google Chat 网络钩子网址的 Secret 的名称。

如需查看此示例,请参阅 Google Chat 通知程序的通知程序配置文件

运行自动化脚本

如需为通知程序自动配置通知,请执行以下操作:

  1. 克隆 cloud-build-notifiers 代码库

  2. 使用您的项目 ID 和区域配置 Google Cloud CLI:

    gcloud config set project project-id
    gcloud config set run/region region
    

    其中:

    • project-id 是您的 Google Cloud 项目 ID。
    • region 是部署通知程序的区域
  3. 在代码库的根目录中运行以下命令:

./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

启用 Cloud Build, Compute Engine, Cloud Run, Pub/Sub, and Secret Manager API。

启用 API

获取和存储凭据

  1. 创建 GitHub 个人访问令牌

    1. 前往 GitHub 设置以创建新令牌。
    2. 选择 repo 范围。

    3. 点击生成令牌

  2. 将您的令牌存储在 Secret Manager 中:

    1. 在 Google Cloud 控制台中打开 Secret Manager 页面:

      打开 Secret Manager 页面

    2. 点击创建密钥

    3. 为您的密钥输入一个名称。

    4. Secret 值下,添加您的 GitHub 令牌。

    5. 如需保存您的密钥,请点击创建密钥

编写模板配置文件

编写模板配置文件,描述所创建 GitHub 问题应采用的格式:

在以下示例模板配置文件中,titlebody 字段使用构建中的替代变量:

{
    "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 是包含 GitHub 令牌的 Secret 的名称。

如需查看此示例,请参阅 Google Chat 通知程序的通知程序配置文件

如需了解可用于过滤的其他字段,请参阅构建资源。如需了解其他过滤示例,请参阅使用 CEL 过滤构建事件

运行自动化脚本

如需为通知程序自动配置通知,请执行以下操作:

  1. 克隆 cloud-build-notifiers 代码库

  2. 使用您的项目 ID 和区域配置 Google Cloud CLI:

    gcloud config set project project-id
    gcloud config set run/region region
    

    其中:

    • project-id 是您的 Google Cloud 项目 ID。
    • region 是部署通知程序的区域
  3. 在代码库的根目录中运行以下命令:

    ./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,了解与该脚本关联的使用说明。

后续步骤