連結至 Bitbucket Data Center 存放區

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

事前準備

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

    Enable the APIs

連結至 Bitbucket Data Center 存放區

主控台

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

  1. 在 Google Cloud 控制台中開啟「Repositories」頁面。

    開啟「存放區」頁面

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

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

  4. 選取「Bitbucket Data Center」

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

    1. 區域:選取連線所在的區域。

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

    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 存放區的 slug
  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。您可以在 BitbucketServerConfig 中找到每個已連結的存放區的 webhook ID。

輸入下列指令,擷取 webhook ID:

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

其中:

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

後續步驟