本頁說明如何將 Bitbucket Server 存放區連結至 Cloud Build。
事前準備
- 
  
  
    
      Enable the Cloud Build, Secret Manager, and Compute Engine APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 請先按照操作說明連結 Bitbucket Server 主機,再連結存放區。
連線至 Bitbucket Server 存放區
主控台
如要使用 Google Cloud 控制台將 Bitbucket Server 存放區連結至 Cloud Build,請按照下列步驟操作:
- 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。 
- 選取頁面頂端的「第 1 代」分頁標籤。 
- 按一下「Connect Repository」(連結存放區)。 
- 選取「Bitbucket Server」。 - 畫面會顯示「Connect Repository」(連結存放區) 面板。請完成下列步驟,連結 Bitbucket Server 存放區: - 「Region」(區域):選取連線所在的區域。 
- 在「Select source code management provider」(選取原始碼管理服務供應商) 中,選取「Bitbucket Server」。 
- 主機連線:從下拉式選單中選取主機連線名稱。 
- 按一下「繼續」。 
- 在「選取存放區」下方,選取要連結至 Cloud Build 的 Bitbucket Server 存放區。 
- 選取 Bitbucket Server 帳戶和存放區後,請閱讀同意聲明免責事項,然後勾選旁邊的核取方塊,表示您接受顯示的條款。 
- 按一下「連結」,即可連結存放區。 
 - 連結存放區後,Cloud Build 會在 Bitbucket Server 執行個體的存放區中設定 Webhook。當您變更存放區時,存放區會傳送 Webhook,叫用對應的 Bitbucket Server 觸發條件。存放區也可以透過多個主機連線多次連線。如要瞭解如何管理 Webhook,請參閱「管理 Webhook」。 - 如要找出 Webhook,您需要 Webhook ID。每個已連結存放區的 Webhook ID 都位於 - BitbucketServerConfig中。- 輸入下列指令來擷取 Webhook ID: - gcloud alpha builds enterprise-config bitbucketserver describe CONFIG_NAME --region=REGION- 其中: - CONFIG_NAME 是 Bitbucket Server 設定的名稱
- REGION 是 Bitbucket Server 主機連線的區域
 
- 按一下 [完成]。(選用) 按一下「建立觸發條件」即可建立觸發條件。 
API
如要使用 API 將 Bitbucket Server 存放區連結至 Cloud Build,請完成下列步驟:
- 請使用下列 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 Server 設定的名稱。
- PROJECT_KEY 是 Bitbucket Server 專案的金鑰。
如要連結個人存放區,專案金鑰應在使用者名稱前包含波浪符號 (~)。例如:~${USERNAME}。主機存放區的完整網址可能如下:https://${HOST_URI}/projects/~${USER_NAME}/repos/${REPO_SLUG}。
- REPO_SLUG 是 Bitbucket Server 存放區的 slug。
 
- 在終端機中輸入下列 - 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 執行個體。 
- 在終端機中輸入下列 - 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。
 - 您可能需要持續執行 - GetOperationAPI 指令,直到回應包含- done: true為止,這表示作業已完成。如果 Bitbucket Server 存放區成功連線,您可以在- response.bitbucketServerConnectedRepositories欄位中看到已連線的存放區。否則,請參閱- error欄位中的詳細錯誤報告。