連線至 Bitbucket Server 存放區

本頁說明如何將 Bitbucket Server 存放區連線至 Cloud Build。

事前準備

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

    Enable the APIs

連線至 Bitbucket Server 存放區

主控台

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

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

    開啟「存放區」頁面

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

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

  4. 選取「Bitbucket Server」

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

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

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

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

    4. 按一下「繼續」

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

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

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

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

    如要找出 webhook,您需要 webhook ID。您可以在 BitbucketServerConfig 中找到每個已連結存放區的 webhook ID。

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

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

    其中:

    • CONFIG_NAME 是 Bitbucket Server 設定的名稱
    • REGION 是 Bitbucket Server 主機連線的區域
  5. 按一下 [完成]。您可以按一下「建立觸發條件」來建立觸發條件。

API

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

  1. 請使用下列 JSON 範本連結存放區:

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

    其中:

    • PROJECT_NUMBER 是 Cloud 專案的專案號碼。
    • REGION 是與 Bitbucket Server 設定相關聯的區域
    • BITBUCKET_SERVER_CONFIG_NAME 是 Bitbucket 伺服器設定的名稱。
    • PROJECT_KEY 是 Bitbucket Server 專案的金鑰。如果您想連結個人存放區,專案金鑰應在使用者名稱前方加上波浪號 (~) 符號。例如:~${USERNAME}。主機存放區的完整網址可能會像 https://${HOST_URI}/projects/~${USER_NAME}/repos/${REPO_SLUG} 這樣。
    • REPO_SLUG 是 Bitbucket Server 存放區的 slug
  2. 在終端機中輸入下列 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_SERVER_CONFIG_NAME/connectedRepositories:batchCreate -d @requests.json
    

    其中:

    • PROJECT_NUMBER 是您的 Cloud 專案編號。
    • PROJECT_ID 是您的 Cloud 專案 ID。
    • REGION 是與 Bitbucket Server 設定相關聯的區域
    • BITBUCKET_SERVER_CONFIG_NAME 是 Bitbucket Server 設定的名稱。

    如果成功,回應主體即會包含新建立的 Operation 執行個體。

  3. 在終端機中輸入下列 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 是您的 Cloud 專案編號。
    • PROJECT_ID 是您的 Cloud 專案 ID。
    • REGION 是與 Bitbucket Server 設定相關聯的區域
    • OPERATION_ID 是 Bitbucket Server 設定建立作業的 ID。您可以在回應的 name 欄位中找到作業 ID。回應中的 name 欄位格式如下:projects/project-id/locations/region/operations/operation-id

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

後續步驟