建立您的通知程式

Cloud Build 可將通知傳送至所選管道,讓您掌握建構狀態的最新資訊。除了 Cloud Build 維護的通知器 (例如 Slack 或 SMTP),您也可以使用 cloud-build-notifiers 存放區中提供的程式庫,建立自己的通知器。

本頁說明如何建立自己的通知器。

事前準備

  • 啟用 Cloud Build、Cloud Run、Pub/Sub 和 Secret Manager API (如有尚未啟用的 API)。

    啟用 API 所需的角色

    如要啟用 API,您必須具備 serviceusage.services.enable 權限。如果您建立了專案,可能已透過擁有者角色 (roles/owner) 取得這項權限。否則,您可以透過 Service Usage 管理員角色 (roles/serviceusage.serviceUsageAdmin) 取得這項權限。瞭解如何授予角色。

    啟用 API

  • 安裝 Go 程式設計語言。

  • 安裝 Google Cloud CLI。

設定

  1. 在電腦上開啟終端機視窗。

  2. 複製並前往 cloud-build-notifiers 存放區:

      git clone https://github.com/GoogleCloudPlatform/cloud-build-notifiers.git && cd cloud-build-notifiers
    
  3. 為通知器新增目錄並前往該目錄,其中 DIRECTORY_NAME 是目錄名稱:

      mkdir DIRECTORY_NAME && cd DIRECTORY_NAME
    
  4. 在新目錄中初始化 Go 模組,其中 DIRECTORY_NAME 是新目錄的名稱:

      go mod init github.com/GoogleCloudPlatform/cloud-build-notifiers/DIRECTORY_NAME
    

    目錄中應該會顯示 go.mod 檔案。

  5. 在 go.mod 檔案中新增下列程式碼,確保您使用的是最新版本通知器:

     replace github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers => ../
    

依附元件已設定完成,可以開始建立自己的通知器。

建立您的通知程式

cloud-build-notifiers 包含 lib/notifiers 目錄。在 lib/notifiers 目錄中,您會看到名為 notifier.go 的檔案。這個檔案包含可用來建立通知器的架構。

您必須定義兩種方法,才能在主檔案中建立通知器。

  1. 在新目錄中,建立名為 main.go 的檔案。

  2. 在 main.go 中,匯入通知器程式庫架構和任何其他依附元件:

    package main
    
    import (
    	"context"
    	"fmt"
    
    	cbpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
    	"github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers"
    	log "github.com/golang/glog"
    	"google.golang.org/protobuf/encoding/prototext"
    )
    
  3. 為通知器定義主要方法。在本例中,logger 是通知器的名稱:

    func main() {
    	if err := notifiers.Main(new(logger)); err != nil {
    		log.Fatalf("fatal error: %v", err)
    	}
    }
    

    main 方法會使用 notifier.go 檔案中定義的 Main 方法,用於設定通知器二進位檔。

  4. 為通知器定義 struct,並在其中定義介面的變數。在這個範例中,logger 是通知器的名稱:

    type logger struct {
    	filter notifiers.EventFilter
    }
    
  5. 新增通知器功能。通知器介面由兩種方法定義:

    • SetUp:SetUp 方法會接受設定、擷取 Secret,並從設定中提取指定的篩選器,然後將其儲存為 Common Expression Language 述詞,可用於傳送通知。如要進一步瞭解 CEL,請參閱 cel-spec 存放區。
    • SendNotification:SendNotification 方法用於將通知傳送至所選管道或服務。

      通知器的定義可在 notifier.go 和 Go 說明文件中找到。

      在以下範例中,通知器介面是使用 SetUp 和 SendNotification 方法定義,用於列印建構記錄,並以 logger 做為通知器的名稱:

      func (h *logger) SetUp(_ context.Context, cfg *notifiers.Config, _ notifiers.SecretGetter, _ notifiers.BindingResolver) error {
      	prd, err := notifiers.MakeCELPredicate(cfg.Spec.Notification.Filter)
      	if err != nil {
      		return fmt.Errorf("failed to create CELPredicate: %w", err)
      	}
      	h.filter = prd
      	return nil
      }
      
      func (h *logger) SendNotification(ctx context.Context, build *cbpb.Build) error {
      	// Include custom functionality here.
      	// This example logs the build.
      	if h.filter.Apply(ctx, build) {
      		log.V(1).Infof("printing build\n%s", prototext.Format(build))
      	} else {
      		log.V(1).Infof("build (%q, %q) did NOT match CEL filter", build.ProjectId, build.Id)
      	}
      
      	return nil
      }
      

      最終的 main.go 檔案應與下方檔案類似。在本範例中,logger 是通知器的名稱。

      package main
      
      import (
      	"context"
      	"fmt"
      
      	cbpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
      	"github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers"
      	log "github.com/golang/glog"
      	"google.golang.org/protobuf/encoding/prototext"
      )
      
      
      func main() {
      	if err := notifiers.Main(new(logger)); err != nil {
      		log.Fatalf("fatal error: %v", err)
      	}
      }
      
      
      type logger struct {
      	filter notifiers.EventFilter
      }
      
      
      func (h *logger) SetUp(_ context.Context, cfg *notifiers.Config, _ notifiers.SecretGetter, _ notifiers.BindingResolver) error {
      	prd, err := notifiers.MakeCELPredicate(cfg.Spec.Notification.Filter)
      	if err != nil {
      		return fmt.Errorf("failed to create CELPredicate: %w", err)
      	}
      	h.filter = prd
      	return nil
      }
      
      func (h *logger) SendNotification(ctx context.Context, build *cbpb.Build) error {
      	// Include custom functionality here.
      	// This example logs the build.
      	if h.filter.Apply(ctx, build) {
      		log.V(1).Infof("printing build\n%s", prototext.Format(build))
      	} else {
      		log.V(1).Infof("build (%q, %q) did NOT match CEL filter", build.ProjectId, build.Id)
      	}
      
      	return nil
      }
      

    接著,設定通知工具。

設定通知

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

    在下列通知器設定檔範例中,filter 欄位使用 CEL 和可用變數 build,篩選狀態為 SUCCESS 的建構事件:

    apiVersion: cloud-build-notifiers/v1
    kind: YourNotifier
    metadata:
      name: logging-sample
    spec:
      notification:
        filter: build.status == Build.Status.SUCCESS

    其中:

    • logging-sample 是通知工具的名稱。

    如要瞭解其他可篩選的欄位,請參閱建構資源。如需其他篩選範例,請參閱「使用 CEL 篩選建構事件」。

  2. 將通知工具設定檔上傳至 Cloud Storage bucket:

    1. 如果您沒有 Cloud Storage bucket,請執行下列指令來建立 bucket,其中 BUCKET_NAME 是您要為 bucket 指定的名稱,須符合命名規定。

      gcloud storage buckets create gs://BUCKET_NAME/
      
    2. 將通知工具設定檔上傳至 bucket:

      gcloud storage cp CONFIG_FILE_NAME gs://BUCKET_NAME/CONFIG_FILE_NAME
      

      其中:

      • BUCKET_NAME 是 bucket 名稱。
      • CONFIG_FILE_NAME 是設定檔名稱。
  3. 建構及部署通知器:

    1. 為 logging-sample 建立 Dockerfile:

      
      FROM golang@sha256:957952ae74dc010608717144399a59a2c6e82ea03d63be1c7ff6577c5a135f93 AS build-env
      COPY . /go-src/
      WORKDIR /go-src/
      RUN go build -o /go-app .
      
      FROM gcr.io/distroless/base
      COPY --from=build-env /go-app /
      ENTRYPOINT ["/go-app", "--v=1", "--alsologtostderr"]
      
    2. 使用下列 cloudbuild.yaml 檔案建構及部署通知器。

      steps:
      - # Build the binary and put it into the builder image.
        name: gcr.io/cloud-builders/docker
        args: ['build', '--tag=gcr.io/$PROJECT_ID/logging-sample', '.']
      
      - # Push the container image to Container Registry
        name: gcr.io/cloud-builders/docker
        args: ['push', 'gcr.io/$PROJECT_ID/logging-sample']
      
      - # Deploy to Cloud Run
        name: google/cloud-sdk
        args: 
          - gcloud
          - run
          - deploy
          - logging-sample-notifier
          - --platform=managed
          - --region=us-central1
          - --image=gcr.io/$PROJECT_ID/logging-sample
          - --no-allow-unauthenticated
          - --update-env-vars=CONFIG_PATH=${_CONFIG_PATH}
      
      # Push the image with tags.
      images:
      - gcr.io/$PROJECT_ID/logging-sample

      其中:

      • _CONFIG_PATH 是通知工具設定的路徑,例如 gs://BUCKET_NAME/CONFIG_FILE_NAME.yaml。

    如要執行 cloudbuild.yaml,請傳入通知器路徑做為替代變數。

     gcloud builds submit .  --substitutions=_CONFIG_PATH=gs://BUCKET_NAME/CONFIG_FILE_NAME
    
  4. 授予 Pub/Sub 權限,在專案中建立驗證權杖:

     gcloud projects add-iam-policy-binding PROJECT_ID \
       --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub. \
       --role=roles/iam.serviceAccountTokenCreator
    

    其中:

    • PROJECT_ID 是 Google Cloud 專案的 ID。
    • PROJECT_NUMBER 是您的 Google Cloud 專案編號。
  5. 建立服務帳戶,代表 Pub/Sub 訂閱項目身分:

    gcloud iam service-accounts create cloud-run-pubsub-invoker \
      --display-name "Cloud Run Pub/Sub Invoker"
    

    您可以使用 cloud-run-pubsub-invoker,或使用 Google Cloud 專案中不重複的名稱。

  6. 將 Cloud Run Invoker 權限授予 cloud-run-pubsub-invoker 服務帳戶:

    gcloud run services add-iam-policy-binding SERVICE_NAME \
       --member=serviceAccount:cloud-run-pubsub-invoker@PROJECT_ID. \
       --role=roles/run.invoker
    

    其中:

    • SERVICE_NAME 是要部署映像檔的 Cloud Run 服務名稱。
    • PROJECT_ID 是 Google Cloud 專案的 ID。
  7. 建立 cloud-builds 主題,接收通知工具的建構作業更新訊息:

    gcloud pubsub topics create cloud-builds
    

    您也可以在建構設定檔中定義自訂主題名稱,將訊息傳送至自訂主題。在這種情況下,您會建立一個具有相同自訂主題名稱的主題:

    gcloud pubsub topics create topic-name
    

    詳情請參閱「用於建構作業通知的 Pub/Sub 主題」。

  8. 為通知器建立 Pub/Sub 推送訂閱者:

     gcloud pubsub subscriptions create subscriber-id \
       --topic=cloud-builds \
       --push-endpoint=SERVICE_URL \
       --push-auth-service-account=SUB_IDENTITY_SERVICE_ACCOUNT@PROJECT_ID.

其中: + SUBSCRIBER_ID 是您要為訂閱項目指定的名稱。 + SERVICE_URL 是 Cloud Run 為新服務產生的網址。 + PROJECT_ID 是 Google Cloud 專案的 ID。

Note: By default, [subscriptions expire after 31 days of inactivity](/pubsub/docs/subscription-overview#lifecycle).
You can adjust or disable the expiration period by including the
[`--expiration-period` flag](/sdk/gcloud/reference/pubsub/subscriptions/create#--expiration-period)
when creating the subscription.

Cloud Build 專案的通知現已設定完成。下次叫用建構作業時,如果建構作業符合您設定的篩選器,就會在頻道中收到通知。

測試通知

如要測試本指南範例的通知,您可以執行 gcloud builds submit 指令來叫用建構作業。

在以下範例中,我們將 success.yaml 指定為設定路徑。執行這個指令後,應該會成功建構最小的建構作業。您應該也會看到建構記錄的輸出內容。

 gcloud builds submit --no-source --config=success.yaml

其中 success.yaml 為:

 steps:
 - name: busybox
   args: ["true"]

在以下範例中,我們將 failure.yaml 指定為設定路徑。執行這個指令後,建構作業應該會失敗。您不會看到建構記錄的輸出內容,而是會看到輸出內容,告知您來源中指定的 CEL 篩選器沒有相符項目。

gcloud builds submit --no-source --config=failure.yaml

其中 failure.yaml 為:

 steps:
 - name: busybox
   args: ["false"]

如果您建立的通知工具設定為執行其他工作,而非將輸出內容記錄到 Cloud Run 服務記錄,也可以執行 gcloud builds submit 指令來測試通知。如要檢查與建構作業相關的錯誤,請查看服務的 Cloud Run 記錄。詳情請參閱「在 Cloud Run 中查看記錄」。

後續步驟