This page explains how to connect to a Bitbucket Server repository to Cloud Build.
Before you begin
-
Enable the Cloud Build, Secret Manager, and Compute Engine APIs.
- Follow the instructions to connect a Bitbucket Server host before connecting repositories.
Connecting to a Bitbucket Server repository
Console
To connect a Bitbucket Server repository to Cloud Build using the Google Cloud console:
Open the Triggers page in the Google Cloud console.
Click Connect Repository.
You will see the Connect repository panel.
Under Select source, select Bitbucket Server.
Under Select Repository, select the Bitbucket Server repositories you wish to connect to Cloud Build.
Click Connect to connect your repositories.
When a repository is connected, Cloud Build configures a webhook on the repository in your Bitbucket Server instance. The repository then sends webhooks to invoke the corresponding Bitbucket Server triggers when you make changes to your repository. A repository can also be connected multiple times with multiple host connections. To learn how to manage webhooks, see Manage webhooks.
To locate the webhook, you need the webhook ID. The webhook ID for each connected repository can be found in the
BitbucketServerConfig
. Enter the following command to retrieve the webhook IDgcloud alpha builds enterprise-config bitbucketserver describe CONFIG_NAME --region=REGION
Where:
- CONFIG_NAME is the name of the Bitbucket Server config
- REGION is the region of the Bitbucket Host connection
Click Done. Optionally, click Create a trigger to create a trigger.
API
To connect your Bitbucket Server repository to Cloud Build using the API, complete the following steps:
Use the following JSON template below to connect your repository:
{ "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" } } } }
Where:
- PROJECT_NUMBER is the project number of your Cloud project.
- REGION is the region associated with your Bitbucket Server configuration.
- BITBUCKET_SERVER_CONFIG_NAME is the name of your Bitbucket Server configuration.
- PROJECT_KEY is the key of your Bitbucket Server project.
If you want to connect a personal repository, your project key should
contain the tilde (
~
) symbol before your username. For example,~${USERNAME}
. The complete URL for a host repository may look likehttps://${HOST_URI}/projects/~${USER_NAME}/repos/${REPO_SLUG}
. - REPO_SLUG is the slug of your Bitbucket Server repository.
Enter the following
curl
command in your terminal: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/global/bitbucketServerConfigs/BITBUCKET_SERVER_CONFIG_NAME/connectedRepositories:batchCreate -d @requests.json
Where:
- PROJECT_NUMBER is your Cloud project number.
- PROJECT_ID is your Cloud project ID.
- BITBUCKET_SERVER_CONFIG_NAME is the name of your Bitbucket Server configuration.
If successful, the response body contains a newly created instance of Operation.
Enter the following
curl
command in your terminal: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/global/operations/OPERATION_ID
Where:
- PROJECT_NUMBER is your Cloud project number.
- PROJECT_ID is your Cloud project ID.
- OPERATION_ID is the ID of your Bitbucket Server configuration creation operation.
You can find the Operation ID in the
name
field of your response. The format of thename
field in your response looks like the following:projects/project-id/locations/region/operations/operation-id
.
You may need to keep running the
GetOperation
API command until the response containsdone: true
, which indicates the operation is completed. If the Bitbucket Server repository is connected successfully, you can see the connected repository in theresponse.bitbucketServerConnectedRepositories
field. Otherwise, see theerror
field for a detailed error report.
What's next
- Learn how to build repositories from Bitbucket Server.
- Learn how to build repositories from Bitbucket Server in a private network.