連結至 Bitbucket Data Center 存放區

本頁說明如何將 Bitbucket Data Center 存放區連結至 Cloud Build。

事前準備

  • Enable the Cloud Build, Secret Manager, and Compute Engine APIs.

    Enable the APIs

連結至 Bitbucket Data Center 存放區

主控台

如要使用 Google Cloud 控制台將 Bitbucket Data Center 存放區連結至 Cloud Build,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「Repositories」(存放區) 頁面

  2. 選取頁面頂端的「第 1 代」分頁標籤。

  3. 按一下「Connect Repository」(連結存放區)

  4. 選取「Bitbucket Data Center」

    畫面會顯示「Connect Repository」(連結存放區) 面板。如要連結 Bitbucket Data Center 存放區,請完成下列步驟:

    1. 「Region」(區域):選取連線所在的區域。

    2. 在「Select source code management provider」(選取原始碼管理服務供應商) 下,選取「Bitbucket Data Center」(Bitbucket 資料中心)

    3. 主機連線:從下拉式選單中選取主機連線名稱。

    4. 按一下「繼續」

    5. 在「選取存放區」下方,選取要連結至 Cloud Build 的 Bitbucket Data Center 存放區。

    6. 選取 Bitbucket Data Center 帳戶和存放區後,請詳閱同意聲明免責事項,然後勾選旁邊的核取方塊,表示接受顯示的條款。

    7. 按一下「連結」即可連結存放區。

  5. 按一下 [完成]。(選用) 按一下「建立觸發條件」即可建立觸發條件。

API

如要使用 API 將 Bitbucket Data Center 存放區連結至 Cloud Build,請完成下列步驟:

  1. 建立含有下列內容的 JSON 檔案:

    {
      "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_DATA_CENTER_CONFIG_NAME",
      "requests": {
        "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_DATA_CENTER_CONFIG_NAME",
        "bitbucketServerConnectedRepository": {
          "parent": "projects/PROJECT_NUMBER/locations/REGION/bitbucketServerConfigs/BITBUCKET_DATA_CENTER_CONFIG_NAME",
          "repo": {
             "projectKey":"PROJECT_KEY",
             "repoSlug": "REPO_SLUG",
           }
         }
      }
    }
    

    其中:

    • PROJECT_NUMBER 是 Cloud 專案的專案號碼。
    • REGION 是與 Bitbucket Data Center 設定相關聯的區域
    • BITBUCKET_DATA_CENTER_CONFIG_NAME 是 Bitbucket Data Center 設定的名稱。
    • PROJECT_KEY 是 Bitbucket Data Center 專案的金鑰。如要連結個人存放區,專案金鑰的使用者名稱前應包含波浪號 (~) 符號。例如,~${USERNAME}。主機存放區的完整網址與 https://${HOST_URI}/projects/~${USER_NAME}/repos/${REPO_SLUG} 類似。
    • REPO_SLUG 是 Bitbucket Data Center 存放區的代碼
  2. 在終端機中,於 JSON 檔案所在的目錄執行下列 curl 指令:

curl -X POST -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8" -H "x-goog-user-project: PROJECT_NUMBER" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/bitbucketServerConfigs/BITBUCKET_DATA_CENTER_CONFIG_NAME/connectedRepositories:batchCreate -d @requests.json

其中:

  • PROJECT_NUMBER 是您的 Google Cloud 專案編號。
  • PROJECT_ID 是您的 Google Cloud 專案 ID
  • REGION 是與 Bitbucket Data Center 設定相關聯的區域
  • BITBUCKET_DATA_CENTER_CONFIG_NAME 是 Bitbucket Data Center 設定的名稱。如果成功,回應主體會包含新建立的 Operation 例項。
  1. 在終端機中執行下列 curl 指令:

    curl -X GET -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8"  -H "x-goog-user-project: PROJECT_NUMBER" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID
    

    其中:

    • PROJECT_NUMBER 是您的 Google Cloud 專案編號。
    • PROJECT_ID 是您的 Google Cloud ID。
    • REGION 是與 Bitbucket Data Center 設定相關聯的區域
    • OPERATION_ID 是 Bitbucket Data Center 設定建立作業的 ID。您可以在回應的 name 欄位中找到作業 ID。回應中 name 欄位的格式如下: projects/project-id/locations/region/operations/operation-id

    您可能需要持續執行 GetOperation API 指令,直到回應包含 done: true 為止,這表示作業已完成。如果 Bitbucket Data Center 存放區已成功連結,您可以在「response.bitbucketServerConnectedRepositories」欄位中看到連結的存放區。否則,請參閱 error 欄位中的詳細錯誤報告。

連結存放區後,Cloud Build 會在 Bitbucket Data Center 執行個體中的存放區設定 Webhook。當您變更存放區時,存放區會傳送 Webhook,叫用對應的 Bitbucket Data Center 觸發程序。存放區也可以透過多個主機連線多次連線。如要瞭解如何管理 Webhook,請參閱「管理 Webhook」。

如要找出 Webhook,您需要 Webhook ID。每個已連結存放區的 Webhook ID 都會顯示在 BitbucketServerConfig 中。

輸入下列指令來擷取 Webhook ID:

gcloud alpha builds enterprise-config bitbucketserver describe CONFIG_NAME  --region=REGION

其中:

  • CONFIG_NAME 是 Bitbucket Data Center 設定的名稱
  • REGION 是 Bitbucket Data Center 主機連線的地區

後續步驟