This page explains how to connect a Bitbucket Data Center host to Cloud Build.
Before you begin
-
Enable the Cloud Build, Secret Manager, and Compute Engine APIs.
- Have your source code ready in a Bitbucket Data Center repository.
- Have either a
Dockerfile
or a Cloud Build config file in your Bitbucket Data Center source repository. - If you have not installed a Bitbucket Data Center instance, see Install Bitbucket Data Center for instructions.
To use
gcloud
commands on this page, install the Google Cloud CLI.
Connect to a Bitbucket Data Center host
Console
To connect your Bitbucket Data Center host to Cloud Build using the Google Cloud console:
Open the Repositories page in the Google Cloud console:
At the top of the page, select the 1st gen tab.
Click Connect Host.
Select Bitbucket Data Center from the drop-down menu.
You will see the Connect host panel.
Enter the following information to connect your Bitbucket Data Center instance to Cloud Build:
Region: Select the region for your connection.
Name: Enter a name for your connection.
Host url: Your host URL of your Bitbucket Data Center instance. For example,
https://bbs.example-test.com:7990
.Google Cloud API key: Enter the API key used to authenticate your credentials.
CA Certificate: Your self-signed certificate. Your certificate must not exceed 10 KB in size and should be in PEM format (
.pem
,.cer
,or.crt
). If you leave this section blank, Google Cloud uses a certificate from the default set of certificates.Username: Your Bitbucket Data Center account username. This account should have admin access to the repositories that you want to connect with Cloud Build.
Read access token: Enter your Bitbucket Data Center account personal access token with read permissions.
Admin access token: Enter your Bitbucket Data Center account personal access token with admin permissions on projects and repositories.
Under Network type, select one of the following options:
Public internet: Select this option if your instance is accessible using the public internet.
Private network: Select this option if your instance is hosted on a private network.
Project: Select your Google Cloud project ID.
Network: Select your network from the drop-down menu. If you have not created a network, see Create and manage VPC networks to learn how to create a network.
IP range: Enter the internal IP range that VMs can be assigned within a peered network's allocated range.
You can specify the range using the Classless Inter-Domain Routing (CIDR) routing notation in the format
STARTING_IP/SUBNET_PREFIX_SIZE
. For example,192.0.2.0/24
has a prefix length of 24. The first 24 bits of the IP range are used as the subnet mask (192.0.2.0
) while the possible hosts addresses range from192.0.2.0
to192.0.2.255
.The value of your prefix length must not exceed
/29
. If no value is specified for the range, a default value of/24
is automatically assigned. If no value is specified for the prefix length, IP addresses are automatically assigned within the peered VPC network. If no value is specified for the IP address, the IP address is automatically assigned a range within the peered VPC network.
Click Connect Host.
If your Bitbucket Data Center instance is on a peered network, connecting your host may take several minutes to complete.
You will be redirected to the Connect Repository panel.
After creating a host connection, your personal access tokens and webhook secret will be securely stored in Secret Manager. You can view and manage your secrets on the Secret Manager page.
gcloud
To connect your Bitbucket Data Center host to Cloud Build
using gcloud
commands, you must run the
gcloud alpha builds enterprise-config bitbucketserver create
command in your terminal. Unlike connecting your host using the
Google Cloud console, you'll need to manually store your personal
access tokens and webhook secret in Secret Manager before running the following command:
gcloud alpha builds enterprise-config bitbucketserver create
--name=BITBUCKET_DATA_CENTER_CONFIG_NAME \
--user-name=USERNAME \
--host-uri=HOST_URI \
--admin-access-token-secret-version=ADMIN_ACCESS_TOKEN_SECRET_VERSION \
--read-access-token-secret-version=READ_ACCESS_TOKEN_SECRET_VERSION \
--webhook-secret-secret-version=WEBHOOK_SECRET_SECRET_VERSION \
--api-key=API_KEY \
--peered-network=PEERED_NETWORK \
--peered-network-ip-range=PEERED_NETWORK_IP_RANGE \
--ssl-ca-file=SSL_CA_FILE
Where:
- BITBUCKET_DATA_CENTER_CONFIG_NAME is the name of your Bitbucket Data Center configuration.
- USERNAME is your Bitbucket Data Center username.
- HOST_URI is the host URI of your Bitbucket Data Center instance.
- ADMIN_ACCESS_TOKEN_SECRET_VERSION is the resource name of your
admin access token stored in Secret Manager. The
expected format for secrets stored in Secret Manager
is
projects/${PROJECT_ID}/secrets/${SECRET_NAME}/versions/${VERSION_NUMBER}
. You can specifylatest
as your version to use the latest version of your secret. This applies to each resource stored in Secret Manager.
- READ_ACCESS_TOKEN_SECRET_VERSION is the resource name of your read access token stored in Secret Manager.
- WEBHOOK_SECRET_SECRET_VERSION is the resource name of your of your webhook secret stored in Secret Manager.
- API_KEY is the Google Cloud API key.
Optional: PEERED_NETWORK is the VPC network to connect to for your Bitbucket Data Center instances on-premises. To learn more, see Build repositories from Bitbucket Data Center in a private network.
Optional: PEERED_NETWORK_IP_RANGE is the internal IP range that VMs can be assigned to within a peered network's allocated range.
SSL_CA_FILE is the path to a local file that contains your SSL certificate to use for requests to Bitbucket Data Center. The certificate should be in PEM format.
API
To connect your Bitbucket Data Center host to Cloud Build using the API, use the following JSON template. Unlike connecting your host using the Google Cloud console, you need to manually store your personal access tokens and webhook secret in Secret Manager before calling the API:
{
"hostUri": "HOST_URI",
"username": "USERNAME",
"apiKey": "API_KEY",
"secrets": {
"adminAccessTokenVersionName": "ADMIN_ACCESS_TOKEN_SECRET_VERSION",
"readAccessTokenVersionName": "READ_ACCESS_TOKEN_SECRET_VERSION",
"webhookSecretVersionName": "WEBHOOK_SECRET_SECRET_VERSION",
},
"peeredNetwork": "PEERED_NETWORK",
"peeredNetworkIpRange": "PEERED_NETWORK_IP_RANGE",
"sslCa": "SSL_CERTIFICATE"
}
Where:
- HOST_URI is the host URI of your Bitbucket Data Center instance.
- USERNAME is your Bitbucket Data Center username.
- API_KEY is the Google Cloud API key.
ADMIN_ACCESS_TOKEN_SECRET_VERSION is the resource name of your admin access token stored in Secret Manager. You may need to grant the Secret Manager Secret Accessor role to your Cloud Build service agent,
service-${PROJECT_NUMBER}@gcp-sa-cloudbuild.iam.gserviceaccount.com
. To learn more, see Granting Secret Manager role to your service account.READ_ACCESS_TOKEN_SECRET_VERSION is the resource name of your read access token stored in Secret Manager.
WEBHOOK_SECRET_SECRET_VERSION is the resource name of your of your webhook secret stored in Secret Manager.
Optional: PEERED_NETWORK is the VPC network to peer to for your Bitbucket Data Center instances on-premises.
You can specify the range using the Classless Inter-Domain Routing (CIDR) routing notation in the format
STARTING_IP/SUBNET_PREFIX_SIZE
. For example,192.0.2.0/24
has a prefix length of 24. The first 24 bits of the IP range are used as the subnet mask (192.0.2.0
) while the possible hosts addresses range from192.0.2.0
to192.0.2.225
.Optional: PEERED_NETWORK_IP_RANGE is the internal IP range that VMs can be assigned to within a peered network's allocated range.
Optional: SSL_CERTIFICATE is the SSL certificate used for your Bitbucket Data Center instances on-premises.
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" https://cloudbuild.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/bitbucketServerConfigs/?bitbucketServerConfigId=BITBUCKET_DATA_CENTER_CONFIG_NAME -d @config.json
Where:
- PROJECT_ID is your Google Cloud project ID.
- REGION is the region associated with your Bitbucket Data Center configuration.
- BITBUCKET_DATA_CENTER_CONFIG_NAME is the name of your Bitbucket Data Center 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/REGION/operations/OPERATION_ID
Where:
- PROJECT_NUMBER is your Google Cloud project number.
- PROJECT_ID is your Google Cloud project ID.
- REGION is the region associated with your Bitbucket Data Center configuration.
- OPERATION_ID is the ID of your Bitbucket Data Center configuration creation operation.
You may need to keep running the GetOperation API command until the response contains done: true
, which indicates the operation is completed. If the Bitbucket Data Center configuration is created successfully, you can see the configuration in the response.value
field. Otherwise, see the error
field for a detailed error report.
What's next
- Learn how to connect a Bitbucket Data Center repository.
- Learn how to perform blue/green deployments on Compute Engine.