自動化處理通知設定

您可以使用 Cloud Build 通知器,設定 Cloud Build 將建構通知傳送至 Slack、Google 即時通訊、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. 按一下「Create secret」

    3. 輸入密鑰的名稱。

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

    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 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. 按一下「Create secret」

  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[email protected]
    
  3. 按一下包含 Compute Engine 預設服務帳戶的資料列中的鉛筆圖示。

    您會看到「編輯存取權」分頁。

    1. 按一下 [Add another role] (新增其他角色)

    2. 新增下列角色:

      • Artifact Registry Reader
      • BigQuery 資料編輯器

        您可以使用 Artifact Registry Reader 角色擷取映像檔的資料。BigQuery 資料編輯器可讓您讀取及寫入資料。

    3. 按一下 [儲存]

編寫通知器設定檔

編寫通知器設定檔,設定 BigQuery 通知器並篩選建構事件:

在以下示例通知器設定檔中,filter 欄位會使用 Common Expression Language 和變數 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 欄位會使用通用運算式語言搭配可用的變數 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. 按一下「Create secret」

    3. 輸入密鑰的名稱。

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

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

編寫通知器設定檔

撰寫通知器設定檔,設定 Google 聊天通知器和建構事件篩選器:

在以下通知器設定檔範例中,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 webhook 網址路徑的設定變數。您在此指定的變數名稱,應與 secrets 下方的 name 欄位相符。
  • project-id 是 Google Cloud 專案的 ID。
  • secret-name 是包含 Google Chat webhook 網址的秘密名稱。

如需範例,請參閱 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. 按一下「Create secret」

    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 聊天通知器和建構事件篩選器:

在以下通知器設定檔範例中,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 權杖的密鑰名稱。

如需範例,請參閱 Google Chat 通知工具的通知工具設定檔

如要瞭解可用於篩選的其他欄位,請參閱「Build」資源。如需其他篩選範例,請參閱「使用 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,取得指令碼的使用說明。

後續步驟