Build repositories from Bitbucket Data Center

Stay organized with collections Save and categorize content based on your preferences.

Cloud Build enables you to create triggers to build from repositories hosted on Bitbucket Data Center, allowing you to execute builds in response to events such as commit pushes or pull requests associated with your Bitbucket Data Center repository.

This page explains how you can enable trigger functionality on a Bitbucket Data Center instance.

Before you begin

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

    Enable the APIs

Creating a Bitbucket Data Center trigger

This sections explains how you can connect your Bitbucket Data Center repositories to Cloud Build and create a trigger to automatically invoke builds on your connected repositories. If you want to use Bitbucket Data Center triggers in a private network, see Building repositories from Bitbucket Data Center in a private network for further instructions.

Console

To create a Bitbucket Data Center trigger using the Google Cloud console:

  1. Open the Triggers page:

    Open the Triggers page

  2. Select your project from the top of the page and click Open.

  3. Click Create trigger.

  4. Enter the following trigger settings:

    • Name: A name for your trigger.

    • Region: Select the region for your trigger.

      • If you select global as the region, Cloud Build uses the default pool to run your build.
      • If you select a non-global region and the build config file associated with the trigger specifies a private pool, Cloud Build uses the private pool to run your build. In this case, the region you specify in your trigger must match the region where you created your private pool.
      • If you select a non-global region and the build config file associated with the trigger does not specify a private pool, Cloud Build uses the default pool to run your build in the same region as your trigger.
    • Description (Optional): A description for your trigger.

    • Event: Select the repository event to invoke your trigger.

      • Push to a branch: Set your trigger to start a build on commits to a particular branch.

      • Push new tag: Set your trigger to start a build on commits that contain a particular tag.

      • Pull request (Cloud Source Repositories not supported): Set your trigger to start a build on commits to a pull request.

    • Source: Select the repository to build when the Bitbucket Data Center trigger runs.

    • Revision: Select the branch or tag to build when the Bitbucket Data Center trigger runs.

    • Branch: Set a trigger to build this branch.

    • Tag: Set a trigger to build this tag.

    • Configuration: Select the build config file located in your repository or configure your build inline on the trigger.

    • Type: Select the type of configuration to use for your build.

      • Cloud Build configuration file (yaml or json): Use a build config file for your configuration.
      • Dockerfile: Use a Dockerfile for your configuration.
    • Location: Specify the location for your configuration.

      • Repository: If your config file is in located in your repository, provide the location of your build config file or the Dockerfile directory and a name for the resulting image. If your configuration is a Dockerfile, you can optionally provide a timeout for your build. When you've provided the Dockerfile and image name, you'll see a preview of the docker build command that your build will execute.

      • Inline: If you selected Cloud Build configuration file (yaml or json) as your configuration option, you can specify your build config inline. Click Open Editor to write your build config file in the Google Cloud console using YAML or JSON syntax. Click Done to save your build config.

  5. Click Create to create your Bitbucket Data Center trigger.

gcloud

To create a Bitbucket Data Center trigger using gcloud commands, you will need to run the following gcloud alpha builds triggers create bitbucketserver command in your terminal:

gcloud alpha builds triggers create bitbucketserver
--name=TRIGGER_NAME \
--project-key=PROJECT_KEY \
--repo-slug=REPO_SLUG \
--bitbucket-server-config-resource=projects/PROJECT_NUMBER/locations/global/bitbucketServerConfigs/ID \
--branch-pattern=BRANCH_NAME \ # --tag-pattern=TAG_NAME
--build-config=BUILD_CONFIG

Where:

  • TRIGGER_NAME is the name of your trigger.
  • PROJECT_KEY is the key of your Bitbucket Data Center project.
  • REPO_SLUG is the slug of your Bitbucket Data Center repository.
  • PROJECT_NUMBER is the project number of your Cloud project.
  • ID is the ID of your BitbucketServerConfig.
  • BRANCH_NAME is the regex of your branch if you want to set your trigger to build certain branches.
  • TAG_NAME is the regex of your tag if you want to set your trigger to build certain tags.
  • BUILD_CONFIG is the path to your build config file.

API

To create a Bitbucket Data Center trigger with the API, use the following JSON template.

    {
      "filename": "cloudbuild.yaml",
      "name": "curl-trigger",
      "description": "curl trigger",
      "bitbucket_server_trigger_config": {
          "repo_slug": REPO_SLUG,
          "project_key": PROJECT_KEY,
          "push": {
             "branch": BRANCH_NAME # "tag": TAG_NAME
          },
          "bitbucket_server_config_resource": "projects/PROJECT_NUMBER/locations/global/bitbucketServerConfigs/ID"
      }
  }

Where:

  • PROJECT_KEY is the key of your Bitbucket Data Center project.
  • REPO_SLUG is the slug of your Bitbucket Data Center repository.
  • PROJECT_NUMBER is the project number of your Cloud project.
  • ID is the ID of your BitbucketServerConfig.
  • BRANCH_NAME is the regex of your branch if you want to set your trigger to build certain branches.
  • TAG_NAME is the regex of your tag if you want to set your trigger to build certain tags.

Enter the following curl command in your terminal, where PROJECT_ID is your Cloud project ID:

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/triggers -d @trigger.json

Data sharing

The data sent to Bitbucket Data Center from Cloud Build helps you identify triggers by name and see build results on Bitbucket Data Center.

The following data is currently shared between Cloud Build and Bitbucket Data Center:

  • Cloud project ID
  • Trigger name

Data sharing is automatically enabled for Bitbucket Data Center.

What's next