為整合作業建構 CI/CD


本教學課程說明如何使用設定變數 (預先發布版),自動在不同 Google Cloud 專案的不同環境之間促進整合。

總覽

在本教學課程中,您將使用設定變數為範例整合項目自動化應用程式整合部署作業。這個範例整合流程會呼叫 API,並將 API 的回應發布至 Pub/Sub 主題。這個範例旨在說明如何使用呼叫 REST 端點連接器工作。您可以將範例整合作業儲存在對應於不同環境和 Google Cloud 專案的 GitHub 存放區中。

目標

本教學課程將說明如何在整合中完成下列工作:

  • 建立整合項目。
  • 建立設定變數,並在整合中使用這些變數。
  • 將整合項目推送至其他環境。

費用

在本教學課程中,您將使用下列 Google Cloud 的計費元件:

本教學課程的說明旨在讓您在使用資源時,能維持在 Google Cloud 免費雲端功能和試用方案的限制範圍內。

完成本文件所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱「清除所用資源」。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Secret Manager API, Connectors API APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Pub/Sub Publisher (roles/pubsub.publisher) role to the service account.

      To grant the role, find the Select a role list, then select Pub/Sub Publisher (roles/pubsub.publisher).

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Secret Manager API, Connectors API APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Pub/Sub Publisher (roles/pubsub.publisher) role to the service account.

      To grant the role, find the Select a role list, then select Pub/Sub Publisher (roles/pubsub.publisher).

    6. Click Continue.
    7. Click Done to finish creating the service account.

建立 Pub/Sub 主題

  1. 在 Google Cloud 控制台中,前往 Pub/Sub 的「Topics」(主題)頁面。

    前往「主題」

  2. 按一下「建立主題」

  3. 在「主題 ID」欄位中輸入主題 ID,如要進一步瞭解如何命名主題,請參閱「命名主題、訂閱、結構定義或快照的規範 」。

  4. 保留「Add a default subscription」(新增預設訂閱) 選項。

  5. 請勿選取其他選項。

  6. 按一下「建立主題」

設定 Pub/Sub 連接器

  1. Google Cloud 主控台中,前往「Integration Connectors」>「Connections」頁面,然後選取或建立 Google Cloud 專案。

    前往「連線」頁面

  2. 按一下「+ 建立新連線」,開啟「建立連線」頁面。
  3. 在「位置」部分中,選擇連線的位置。
    1. 區域:從下拉式清單中選取位置。

      如需所有支援地區的清單,請參閱「位置」一節。

    2. 點按「下一步」
  4. 在「連線詳細資料」部分中,完成下列操作:
    1. 連接器:從可用連接器的下拉式清單中選取「Pub/Sub」
    2. 連接器版本:從可用版本的下拉式清單中選取連接器版本。
    3. 在「Connection Name」(連線名稱) 欄位中輸入 pub-sub-connector
    4. 服務帳戶:選取具備必要角色的服務帳戶。
    5. 專案 ID: Google Cloud Pub/Sub 執行個體所在的專案 ID。
    6. 主題 ID:輸入 Pub/Sub 主題的名稱。
    7. 點選「下一步」
  5. 在「驗證」部分中,按一下「下一步」
  6. 查看:查看連線和驗證詳細資料。
  7. 按一下 [建立]。

建立新的整合功能

設定應用程式整合後,您可以建立新的整合:

  1. 前往 Google Cloud 控制台的「Application Integration」頁面。

    前往「應用程式整合」

  2. 按一下左側導覽選單中的「整合」,開啟「整合」頁面。
  3. 按一下 「建立整合」,然後在「建立整合」頁面中提供下列詳細資料:
    1. Integration name:輸入整合作業的名稱。例如:build-cicd
    2. 說明:您可以選擇輸入整合功能的說明。例如:Demo integration created for CICD tutorial
    3. 區域:從已佈建區域清單中選取區域 us-central1。如果您要選取的區域未預先配置,請參閱「預先配置新的區域」一文。如需支援的地區清單,請參閱「Application Integration 位置」。
    4. 服務帳戶:如果您已為所在區域啟用治理功能,就會顯示這個欄位。 選取要整合的服務帳戶。如要瞭解如何為區域啟用治理功能,請參閱「編輯區域」。
    5. 按一下「建立」,系統會開啟整合服務編輯器頁面,讓您新增任務觸發條件,以建立整合服務。

建立整合流程

新增 API 觸發條件和呼叫 REST 端點

如要新增 API 觸發條件,請按照下列步驟操作:
  1. 前往 Google Cloud 控制台的「Application Integration」頁面。

    前往「應用程式整合」

  2. 在導覽選單中,按一下「整合」

    系統隨即會顯示「Integrations List」頁面,列出 Google Cloud 專案中可用的所有整合項目。

  3. 選取 build-cicd 整合。

    這會在整合編輯器中開啟整合項目。

  4. 整合服務編輯器導覽列中,依序點選「觸發條件」>「API 觸發條件」,即可新增觸發條件。
  5. 整合服務編輯器的導覽列中,依序點選「Tasks」>「Call REST Endpoint」,即可新增任務。
  6. 透過將滑鼠懸停在「API trigger」元素的控制點,然後點選並拖曳線條至「Call REST Endpoint」任務元素的控制點,即可從「API trigger」元素至「Call REST Endpoint」元素新增邊緣連線。

建立設定變數

如要建立設定變數,請按照下列步驟操作:
  1. 在整合服務編輯器導覽列中,按一下 (切換面板),即可顯示「變數」窗格。
  2. 點選「+建立」
  3. 在「Create Variable」窗格中執行下列操作:
    1. 在「Name」 中輸入 url

      Application Integration 會在設定變數中加入 `CONFIG_ 前置字串。如果您想使用 API 將設定變數新增至整合項目,請使用以下格式設定變數:`CONFIG_CONFIG_VARIABLE_NAME`。如要瞭解如何下載及上傳整合項目,請參閱「上傳及下載整合項目」。

    2. 在「變數類型」部分中,選取「設定變數」
    3. 在「資料類型」部分,選取「字串」
    4. 按一下「建立」

      設定變數會顯示在「變數」窗格中。

  4. 重複執行步驟 2 和 3,新增下列設定變數:
    • 名稱:connection_name
    • 變數類型: Config Variable
    • 資料類型: String

新增及設定呼叫 REST 端點

如要建立設定變數,請按照下列步驟操作:
  1. 整合服務編輯器中,按一下「Call REST Endpoint」任務,開啟任務設定窗格。接著,按照下列步驟操作:
    1. 展開「工作輸入」部分。
    2. 在「Endpoint base URL」(端點基本網址) 中,按一下「變數」並新增 $`CONFIG_url`$

新增及設定「Connectors」(連接器) 任務

  1. 整合編輯器的導覽列中,依序點選「Task」>「Connectors」
  2. 按一下「設定連接器」
  3. 在「Region」清單中,選取您建立 Pub/Sub 連接器的區域。
  4. 在「Connection」欄位中,選取您在先前步驟中建立的 Pub/Sub 連接器
  5. 在「類型」部分中,選取「動作」
  6. 在「設定實體/動作」部分,針對「動作」選取「publishMessage」
  7. 在「預設值」中,新增設定變數的預設值。
  8. 按一下 [建立]。
  9. 在「Connectors」任務設定窗格中,展開「Connection details」區段,更新下列欄位:
    1. 在「連線名稱」欄位中,點選「變數」並選取 $`CONFIG_connection_name`$

新增資料對應工作

  1. 整合編輯器的導覽列中,依序按一下「工作」>「資料對應」
  2. 透過將滑鼠懸停在「Call REST Endpoint」元素的控制點,接著點選線條並拖曳到「Data Mapping」任務元素的控制點,即可從「Call REST Endpoint」元素至「Data Mapping」元素新增邊緣連線。
  3. 透過將滑鼠懸停在「Data Mapping」元素的控制點,接著點選線條並拖曳到「Connectors」任務元素的控制點,即可從「Data Mapping」元素至「Connectors」元素新增另一個邊緣連線。
  4. 在「資料對應」工作設定窗格中,按一下「開啟資料對應編輯器」
  5. 將「Call REST Endpoint」任務的輸出變數對應至「Connectors」任務的輸入變數。

發布整合

如要發布整合,請按照下列步驟操作:
  1. 如要發布整合項目,請在整合服務編輯器的工具列中按一下「Publish」

    系統會隨即顯示「發布整合」對話方塊。

  2. 在「發布整合」對話方塊中,輸入下列設定變數的值:
    1. url:輸入端點網址的名稱。
    2. connection_name:輸入您在上一個步驟中建立的 Pub/Sub 連接器名稱。
  3. 按一下 [發布]
  4. 成功發布整合後,您可以查看及檢查已發布的整合項目的執行記錄。如要查看記錄檔,請按一下整合服務編輯器工具列中的「Logs」

促進跨環境整合

如要跨環境推廣整合功能,您可以使用 integrationcli 工具,這項開放原始碼工具可讓您與 Application Integration APIIntegration Connectors API 互動。這項工具可讓您管理整合實體,例如整合、驗證設定等。如要瞭解如何為整合作業產生腳架,並套用腳架產生的變更,請參閱社群網誌中的「integrationcli 簡介」。

您也可以設定 DevOps 管道 (例如使用 Jenkins、GitLab、Cloud Build 等),在各環境中推送變更。詳情請參閱「與 Cloud Build 整合」。

如果您不想使用 integrationcli,可以手動下載並上傳整合項目至新專案。由於目前無法下載連接器,因此您必須在新的專案中手動建立連接器。在新的環境中發布整合後,應用程式整合功能會讓您輸入設定變數的值。

後續步驟