自動化處理通知設定

您可以使用 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.

Enable the APIs

取得及儲存憑證

  1. 為要傳送通知的 Slack 工作區建立 Slack 應用程式

  2. 啟用連入 Webhook,將 Cloud Build 的訊息發布到 Slack。

  3. 前往 Slack 應用程式,找出連入 Webhook 的網址。網址應與以下網址類似:

    http://hooks.slack.com/services/...
    
  4. 將連入 Webhook 網址儲存在 Secret Manager 中:

    1. 在 Google Cloud 控制台中開啟「Secret Manager」頁面:

      開啟 Secret Manager 頁面

    2. 按一下「建立密鑰」

    3. 輸入密鑰名稱。

    4. 在「密鑰值」下方,新增 Slack 應用程式的連入 Webhook 網址。

    5. 如要儲存密鑰,請按一下「建立密鑰」

編寫通知程式設定檔

編寫通知程式設定檔,設定 Slack 通知程式並篩選建構事件:

在下列範例的通知程式設定檔中,filter 欄位會使用 Common Expression Language 和可用變數 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 Webhook 網址路徑。您在此指定的變數名稱應與 secrets 下的 name 欄位相符。
  • project-id 是 Google Cloud 專案的 ID。
  • secret-name 是包含 Slack Webhook URL 的密鑰名稱。
  • 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 中的密鑰名稱。

執行指令碼後,您會看到以下訊息:

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

Enable the APIs

儲存憑證

  1. 將寄件者的電子郵件帳戶密碼儲存在 Secret Manager 中:

  2. 在 Google Cloud 控制台中開啟「Secret Manager」頁面:

    開啟 Secret Manager 頁面

  3. 按一下「建立密鑰」

  4. 輸入密鑰名稱。

  5. 在「密鑰值」下方,新增寄件者的電子郵件帳戶密碼。

  6. 如要儲存密鑰,請按一下「建立密鑰」

編寫通知程式設定檔

撰寫通知程式設定檔,設定 SMTP 通知程式並篩選建構事件:

在下列範例的通知程式設定檔中,filter 欄位會使用 Common Expression Language 和可用變數 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 是密碼的名稱,其中包含寄件者電子郵件帳戶的密碼。
  • 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 中的密鑰名稱。

執行指令碼後,您會看到以下訊息:

** NOTIFIER SETUP COMPLETE **

通知器設定完成。您可以在 cloud-build-notifiers 存放區中查看完整指令碼,或執行 ./setup.sh --help,取得與指令碼相關的使用說明。

BigQuery

設定

下列各節說明您必須完成的步驟,才能自動設定通知程式的通知。

啟用 API

Enable the Cloud Build, Cloud Run, Pub/Sub, and BigQuery APIs.

Enable the APIs

授予權限

授予 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. 按一下 [Add another role] (新增其他角色)

    2. 新增下列角色:

      • Artifact Registry 讀取者
      • BigQuery 資料編輯者

        Artifact Registry Reader 角色可讓您擷取圖片資料。BigQuery 資料編輯者可讀取及寫入資料。

    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

Enable the Cloud Build, Cloud Run, and Pub/Sub APIs.

Enable the APIs

編寫通知程式設定檔

撰寫通知程式設定檔,設定 HTTP 通知程式並篩選建構事件:

在下列範例的通知程式設定檔中,filter 欄位會使用 Common Expression Language 和可用變數 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

Enable the Cloud Build, Compute Engine, Cloud Run, Pub/Sub, and Secret Manager APIs.

Enable the APIs

取得及儲存憑證

  1. 在 Google Chat 中建立聊天室

  2. 在建立的聊天室中建立傳入 Webhook,將 Cloud Build 的訊息發布到 Google Chat。網址應類似於以下示例:

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

  3. 將連入 Webhook 網址儲存在 Secret Manager 中:

    1. 在 Google Cloud 控制台中開啟「Secret Manager」頁面:

      開啟 Secret Manager 頁面

    2. 按一下「建立密鑰」

    3. 輸入密鑰名稱。

    4. 在「密鑰值」下方,新增 Google Chat 聊天室的傳入 Webhook 網址。

    5. 如要儲存密鑰,請按一下「建立密鑰」

編寫通知程式設定檔

撰寫通知程式設定檔,設定 Google Chat 通知程式並篩選建構事件:

在下列範例的通知程式設定檔中,filter 欄位會使用 Common Expression Language 和可用變數 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 Webhook 網址路徑。您在此指定的變數名稱應與 secrets 下的 name 欄位相符。
  • project-id 是 Google Cloud 專案的 ID。
  • secret-name 是包含 Google Chat Webhook URL 的密鑰名稱。

如要查看範例,請參閱 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 中的密鑰名稱。

執行指令碼後,您會看到以下訊息:

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

Enable the APIs

取得及儲存憑證

  1. 建立 GitHub 個人存取權杖

    1. 前往 GitHub 設定建立新權杖。
    2. 選取 repo 範圍。

    3. 按一下「產生權杖」

  2. 將權杖儲存在 Secret Manager 中:

    1. 在 Google Cloud 控制台中開啟「Secret Manager」頁面:

      開啟 Secret Manager 頁面

    2. 按一下「建立密鑰」

    3. 輸入密鑰名稱。

    4. 在「密鑰值」下方,新增 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 Issues 通知程式的範本設定檔

您可以從建立問題的 GitHub API 端點,設定可用的主體參數,

編寫通知程式設定檔

撰寫通知程式設定檔,設定 Google Chat 通知程式並篩選建構事件:

在下列範例的通知程式設定檔中,filter 欄位會使用 Common Expression Language 和可用變數 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 權杖的密碼名稱。

如要查看範例,請參閱 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 中的密鑰名稱。

執行指令碼後,您會看到以下訊息:

** NOTIFIER SETUP COMPLETE **

通知器設定完成。您可以在 cloud-build-notifiers 存放區中查看完整指令碼,或執行 ./setup.sh --help,取得與指令碼相關的使用說明。

後續步驟