啟用工具鏈的事件通知

DevOps Insights 將於 2026 年 8 月 31 日結束服務並停止運作。Continuous Delivery 將於 2027 年 2 月 12 日在以下地區停止運作:au-sydca-torus-east。 Code Risk Analyzer 也將於該日於所有地區停止服務。 若某個區域未實際使用這些功能,該區域中的功能可能會提前停用,並停止接受新的實例。 了解更多

作為 IBM Cloud® Continuous Delivery 工具鏈的管理員,您可能需要透過電子郵件、簡訊、Slack 或其他支援的傳遞管道,PagerDuty, 將工具鏈或工具整合中的事件通知發送給其他使用者或人工接收端。 此外,您可能想要將這些事件通知傳送至其他應用程式,例如使用 Webhook 來使用事件驅動程式設計來建置邏輯。 此方法得以實現,是透過工具鏈與 IBM Cloud® Event Notifications. 之間的整合所促成。

若要將資訊傳送至 Event Notifications,您必須 將 Event Notifications 工具整合 新增至工具鏈。 如需使用 Event Notifications的相關資訊,請參閱 開始使用 Event Notifications

現在您可以透過使用 Event Notifications 工具集成。Event Notifications 是向 Slack 和其他通信渠道(如 PagerDuty, email、SMS、推送通知、webhook、Microsoft® Teams, ServiceNow, 和 IBM Cloud Functions )分發通知的首選方法。

工具鏈如何收集及傳送事件

當感興趣的事件發生在工具鏈或其中一個支援的工具整合時,工具鏈會與已連接的 Event Notifications 實例進行通訊,以將通知轉遞至 支援的目的地

工具鏈支援兩種類型的事件:

  • 內建事件會在工具鏈內自動產生。 例如,在工具鏈中新增或移除工具整合、管線執行開始及管線執行完成時,會傳送內建事件。 內建事件的有效負載由工具鏈所決定的資料組成。
  • 用戶端定製事件由工具鏈在用戶端要求時使用 POST /toolchains/{toolchain_id}/events API 產生。 用戶端定製事件的有效負載包含工具鏈所決定的資料,以及用戶端提供給 API 的資料。

Tekton 管道步驟可利用客戶端客製化的 POST {toolchain_id} /toolchains//events API,將自訂事件傳送至 Event Notifications 目的地,這些事件承載著與步驟相關且具意義的資訊。

Continuous Delivery 的事件

下表列出了工具鏈事件。

附加至每一個子類型的 :1 字元代表主要版本號碼。

產生事件通知的操作
事件名稱 事件類型 子類型 說明
Client event com.ibm.cloud.toolchain.client event:1 當用戶端呼叫 POST /toolchains/{toolchain_id}/events API 時,會傳送此用戶端定製事件。
Tool created com.ibm.cloud.toolchain.toolchain toolchain_bind:1 建立工具整合並新增至工具鏈時,會傳送此內建事件。
Tool deleted com.ibm.cloud.toolchain.toolchain toolchain_unbind:1 從工具鏈刪除及移除工具整合時,會傳送此內建事件。
Pipeline run started com.ibm.cloud.toolchain.pipeline pipeline_start:1 當 Tekton 管線執行或 Classic 管線階段啟動時,會傳送此內建事件。
Pipeline run succeeded com.ibm.cloud.toolchain.pipeline pipeline_success:1 當 Tekton 管線執行或 Classic 管線階段順利完成時,會傳送此內建事件。
Pipeline run failed com.ibm.cloud.toolchain.pipeline pipeline_fail:1 當 Tekton 管線執行或 Classic 管線階段完成且具有失敗狀態時,會傳送此內建事件。 例如,嘗試部署時傳送此事件,但無法順利完成。
Pipeline run cancelled com.ibm.cloud.toolchain.pipeline pipeline_cancel:1 取消 Tekton 管線執行或 Classic 管線階段時,會傳送此內建事件。
Pipeline run error com.ibm.cloud.toolchain.pipeline pipeline_error:1 當 Tekton 管線執行遇到錯誤且可能未順利完成時,會傳送此內建事件。 此事件主要用於基礎架構及配置問題,例如當形態異常的 Tekton 阻止管線執行啟動時。

啟用通知

工具鏈或相關聯工具整合所產生的內建及用戶端定製事件可以轉遞至相同帳戶中可用的 Event Notifications 服務實例。

請確保所選 Event Notifications 服務實例具備 IAM 授權政策,允許工具鏈向此服務實例發送事件。 有關透過服務 Event Notifications 實例授予授權的詳細資訊,請參閱《 為何我無法取得整合 Event Notifications 實例的權限? 》。

在主控台中連接至 Event Notifications

配置 Event Notifications 以從工具鏈及工具整合實例傳送重要事件:

  1. 若您已擁有工具鏈並欲將此工具整合至其中,IBM Cloud 請從控制台點擊選單圖示 (漢堡圖示 )> 平台自動化 > 工具鏈。 在「工具鏈」頁面中,點擊該工具鏈以開啟其「概覽」頁面。

    a. 點擊「新增工具」。

    b. 在「工具整合」區段中,按一下 Event Notifications

  2. 請在工具鏈中的「Event Notifications」卡片上,輸入您希望此工具整合所顯示的名稱。 此名稱用來識別工具鏈中的工具整合。

  3. 選取要連接工具鏈的 Event Notifications 實例。

  4. 點擊「建立整合」,將 Event Notifications 工具整合至您的工具鏈中。

  5. 在「工具鏈概觀」頁面上的 IBM Cloud 工具 卡片上,按一下 Event Notifications

使用 API 連接至 Event Notifications

您可以使用 API 將 Event Notifications 工具整合新增至工具鏈。

  1. 取得 IAM 持有人記號。 或者,如果您使用 SDK,請 取得 IAM API 金鑰,並使用環境變數來設定用戶端選項。

    export CD_TOOLCHAIN_AUTH_TYPE=iam && \
    export CD_TOOLCHAIN_APIKEY={iam_api_key} && \
    export CD_TOOLCHAIN_URL={base_url}
    
  2. 查閱您要在其中建立工具整合的工具鏈 ID

  3. 指定 eventnotifications 作為 tool_type_id

  4. 指定工具整合所需的下列 tool_parameters:

    • name: 用來識別 Event Notifications 工具整合的名稱。
    • instance-crn: Event Notifications 服務實例的雲端資源名稱 (CRN)。
  5. 在目標工具鏈內新增工具整合。

    curl -X POST --location --header "Authorization: Bearer {iam_token}" \
      --header "Accept: application/json" \
      --header "Content-Type: application/json" \
      --data '{ "name": "{tool_name}", "tool_type_id": "eventnotifications", "parameters": { "name": {event_notifications_tool_integration_name}, "instance-crn": {event_notifications_service_crn} } }' \
      "{base_url}/toolchains/{toolchain_id}/tools"
    
    const CdToolchainV2 = require('@ibm-cloud/continuous-delivery/cd-toolchain/v2');
    const toolchainService = CdToolchainV2.newInstance();
    ...
    (async() => {
       const toolParameters = {
          "name": {event_notifications_tool_integration_name},
          "instance-crn": {event_notifications_service_crn}
       }
       const toolPrototypeModel = {
          toolchainId: {toolchain_id},
          toolTypeId: "eventnotifications",
          name: {tool_name},
          parameters: toolParameters
       };
       const response = await toolchainService.createTool(toolPrototypeModel);
    })();
    
    import (
    	   "github.com/IBM/continuous-delivery-go-sdk/cdtoolchainv2"
    )
    ...
    toolchainClientOptions := &cdtoolchainv2.CdToolchainV2Options{}
    toolchainClient, err := cdtoolchainv2.NewCdToolchainV2UsingExternalConfig(toolchainClientOptions)
    toolParameters := map[string]interface{}{
       "name": {event_notifications_tool_integration_name},
       "instance-crn": {event_notifications_service_crn},
    }
    createToolOptions := toolchainClient.NewCreateToolOptions({toolchain_id}, "eventnotifications")
    createToolOptions.SetName({tool_name})
    createToolOptions.SetParameters(toolParameters)
    tool, response, err := toolchainClient.CreateTool(createToolOptions)
    
    from ibm_continuous_delivery.cd_toolchain_v2 import CdToolchainV2
    ...
    toolchain_service = CdToolchainV2.new_instance()
    tool_parameters = {}
    tool_parameters["name"] = {event_notifications_tool_integration_name}
    tool_parameters["instance-crn"] = {event_notifications_service_crn}
    tool = toolchain_service.create_tool(
       name = {tool_name},
       toolchain_id = {toolchain_id},
       tool_type_id = "eventnotifications",
       parameters = tool_parameters
    )
    
    import com.ibm.cloud.continuous_delivery.cd_toolchain.v2.CdToolchain;
    import com.ibm.cloud.continuous_delivery.cd_toolchain.v2.model.*;
    ...
    CdToolchain toolchainService = CdToolchain.newInstance();
    HashMap<String, Object> toolParameters = new HashMap<>();
    toolParameters.put("name", {event_notifications_tool_integration_name});
    toolParameters.put("instance-crn", {event_notifications_service_crn});
    CreateToolOptions createToolOptions = new CreateToolOptions.Builder()
       .name({tool_name})
       .parameters(toolParameters)
       .toolchainId({toolchain_id})
       .toolTypeId("eventnotifications")
       .build();
    Response<ToolchainToolPost> response = toolchainService.createTool(createToolOptions).execute();
    ToolchainToolPost tool = response.getResult();
    

下表列出並說明先前步驟中使用的每一個變數。

用於配置工具與 API 整合的變數
變數 說明
{base_url} 工具鏈 API 端點 URL 有關支援值的詳細資訊,請 閱端點 URL。
{iam_api_key} 您的 IAM API 金鑰。
{iam_token} 一個有效的 IAM 承載者憑證。
{tool_name} 工具整合的名稱。
{event_notifications_tool_integration_name} Event Notifications 服務實例的名稱。
{event_notifications_service_crn} Event Notifications 服務實例的雲端資源名稱 (CRN)。
{toolchain_id} 在其中建立工具整合的工具鏈。

新增與 Terraform 的工具整合

您可以使用 Terraform 將 Event Notifications 工具整合新增至工具鏈。

IBM Cloud 若要透過 Terraform 添加工具整合,需使用 Terraform 提供者版本 1.1.x 1.53.0 或更高版本。

  1. 若要安裝 Terraform 指令行介面 (CLI) 並配置 Terraform 的 IBM Cloud 提供者外掛程式,請遵循 在 IBM Cloud® 的指導教學。

  2. 建立一個名為 main.tf`` 的 Terraform 配置檔案。 在此檔案中,新增配置以使用 HashiCorp 配置語言 (HCL) 來建立資源實例。 如需使用此配置語言的相關資訊,請參閱 Terraform 文件

    下列範例會使用 ibm_cd_toolchain_tool_pipeline 資源來建立 Delivery Pipeline 工具整合,其中 toolchain_id 是 GUID,代表要在其中建立工具整合的工具鏈。

    data "ibm_cd_toolchain" "cd_toolchain" {
      toolchain_id = {toolchain_id}
    }
    resource "ibm_cd_toolchain_tool_eventnotifications" "en_instance" {
      toolchain_id = data.ibm_cd_toolchain.cd_toolchain.id
      parameters {
        name = "{event_notifications_tool_integration_name}"
        instance_crn = "{event_notifications_service_crn}"
      }
    }
    

    如需工具整合資源的相關資訊,請參閱 IBM Cloud Terraform 登錄中受支援工具整合資源的完整清單。

  3. 起始設定 Terraform CLI。

    terraform init
    
  4. 建立 Terraform 執行計劃。 此計劃彙總必須執行以建立工具整合的動作。

    terraform plan
    
  5. 套用 Terraform 執行計畫。 Terraform 會採取必要的動作來建立工具整合。

    terraform apply
    

下表列出並說明先前步驟中使用的每一個變數。

用於配置工具與 API 整合的變數
變數 說明
{event_notifications_tool_integration_name} Event Notifications 服務實例的名稱。
{event_notifications_service_crn} Event Notifications 服務實例的 CRN。
{toolchain_id} 在其中建立工具整合的工具鏈。

遞送通知至選取的目的地

啟用工具鏈的事件通知之後,請在 Event Notifications 中建立 主題目的地訂閱,以便可以將警示轉遞及遞送至您選取的目的地。

如需完整支援目的地清單,請參 閱 Event Notifications 文件

通知有效負載詳細資料

工具鏈及其相關聯工具整合實例所產生的事件包含各種欄位,可協助您識別事件的來源及詳細資料。

POST {toolchain_id} /toolchains//events API 將返回 200 狀態碼,表示請求已處理完成。 這並不一定表示事件已成功傳送至對應的 Event Notifications 服務實例。

來自工具鏈及工具整合實例的內建事件通知僅包含 meta 資料內容,例如資源的名稱或 ID。 機密資料 (例如 API 金鑰或密碼) 不會包含在產生的事件中。

客戶客製化事件通知包含客戶提供給 POST {toolchain_id} /toolchains//events API 的資料。 在呼叫 API 時,請勿包含認證、個人識別資訊或其他機密性資訊。

傳送至 Event Notifications 的內容會因事件類型而異。 例如,如果發生 com.ibm.cloud.toolchain.pipeline:pipeline_start:1 事件,則工具鏈會將類似於下列範例的通知有效負載傳送至 Event Notifications。

{
   "subject": {
      "name": "<user>",
      "email": "<user_email>",
      "iam_id": "<iam_id>"
   },
   "toolchain.instance": {
      "crn": "crn:v1:bluemix:public:toolchain:<region>:a/<account_id>:<toolchain_id>::",
      "href": "https://api.<region>.devops.cloud.ibm.com/toolchain/v2/toolchains/<toolchain_id>",
      "id": "357d4432-964a-46ae-83d4-df91eb539d1a",
      "name": "EventNotifications-toolchain",
      "resource_group_id": "<resource_group_id>",
      "ui_href": "https://cloud.ibm.com/devops/toolchains/<toolchain_id>?env_id=ibm:yp:us-south"
   },
   "toolchain.tool-instance": {
      "href": "https://api.<region>.devops.cloud.ibm.com/toolchain/v2/toolchains/<toolchain_id>/tools/<tool_id>",
      "id": "<tool_id>",
      "name": "ci-pipeline",
      "tool_type_id": "pipeline",
      "referent": {
         "ui_href": "https://cloud.ibm.com/devops/pipelines/<tool_id>?env_id=ibm:yp:us-south"
      }
   },
   "toolchain.pipeline-run": {
      "id": "<run_id>",
      "run_number": 11,
      "start_time": "2023-04-17T16:48:36.928Z",
      "ui_href": "https://cloud.ibm.com/devops/pipelines/<tool_id>/<stage_id>/<run_id>?env_id=<region_id>",
      "trigger": {
         "href": "https://api.<region>.devops.cloud.ibm.com/pipeline/v2/tekton_pipelines/<tool_id>/triggers/<trigger_id>",
         "id": "<trigger_id>",
         "name": "my-trigger",
         "type": "manual"
      }
   }
}

下表提供每一個事件通知內容的詳細資訊。

事件通知負載中的屬性
內容 說明
subject 選用。 代表起始事件之主旨的物件。 此物件可能包含下列欄位:

name: 主旨的名稱。

email: 主旨的電子郵件。

iam_id: 主體的 IAM ID。

toolchain.instance 代表產生事件之工具鏈的物件。 此物件包含下列欄位:

crn: 工具鏈的 CRN。

id: 工具鏈的 ID。

resource_group_id: 工具鏈資源群組的 ID。

name: 工具鏈的名稱。

href: 工具鏈的公用 API 端點。

ui_href: 工具鏈的使用者介面端點。

toolchain.tool-instance 選用。 代表參與事件之工具鏈實例的物件。 此物件呈現且僅適用於特定於工具或工具整合的事件。 對於 toolchain_bindtoolchain_unbind 子類型,此物件是正在連結或取消連結的工具整合實例。 對於管線事件,此物件是產生事件的管線工具整合實例。 此物件包含下列欄位:

id: 工具整合實例的 ID。

tool_type_id: 工具類型的 ID。

href: 工具整合實例的公用 API 端點。

state: 工具整合實例的狀態。

referent: 包含工具整合實例所代表之工具相關資訊的物件。 例如, ui_href 是工具整合實例所代表之工具整合的使用者介面端點。

name: 選用。 工具整合實例的名稱。

toolchain.pipeline-run 選用。 代表產生事件的 Tekton 管線執行或 Classic 管線階段執行的物件。 此物件呈現且僅適用於管線事件,並且包含下列欄位:

id: Tekton 管線執行或 Classic 管線階段的 ID。

ui_href: Tekton 管線執行或 Classic 管線階段的使用者介面端點。

run_number: 選用。 Tekton 管線執行或 Classic 管線階段的執行號碼。

start_time: 選用。 執行開始的時間 (ISO 8601 格式)。

finish_time: 選用。 執行完成的時間 (ISO 8601 格式)。

duration: 選用。 執行的持續時間 (ISO 8601 格式)。

trigger: 選用。 包含執行 Tekton 管線之觸發程式相關資訊的物件。 例如, name,這是 Tekton 管線觸發程式的名稱。

toolchain.external-event 選用。 此物件包含因呼叫 POST /toolchains/{toolchain_id}/events API 而產生之用戶端定製事件的詳細資料。 此物件存在且僅適用於用戶端定製事件。 此物件包含下列欄位:

id: POST /toolchains/{toolchain_id}/events API.

title所產生之用戶端定製事件的 ID: POST /toolchains/{toolchain_id}/events API.

description之要求有效負載中的 title 欄位值: POST /toolchains/{toolchain_id}/events API.

data之要求有效負載中的 description 欄位值: 選用。 此欄位的顯示狀態及值取決於提交至 POST /toolchains/{toolchain_id}/events API 的要求有效負載。 如果對 API 的請求指定 content_typetext/plain,則 data 欄位將與請求負載中的 data.text_plain.content 欄位值一起出現,其中包含字串資料。 如果對 API 的請求指定 content_typeapplication/json,則 data 欄位將與請求負載中的 data.application_json.content 欄位值一起出現,其中包含 JSON 資料。 請注意,JSON 資料的最大深度限制為 5。 如果 API 的要求指定 content_typenone,則會省略 data 欄位。