Build repositories from Bitbucket Server

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 Server, allowing you to execute builds in response to events such as commit pushes or pull requests associated with your Bitbucket Server repository.

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

Before you begin

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

    Enable the APIs

Creating a Bitbucket Server trigger

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

Console

To create a Bitbucket Server 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 1st generation as your source.

      • Repository: From the list of available repositories, select the desired repository. To connect a new repository, see Connect to a Bitbucket Server repository.

      • Branch or Tag: Specify a regular expression with the branch or tag value to match. For information on acceptable regular expression syntax, see RE2 syntax.

      • Comment control: If you selected Pull request (GitHub App only) as your Event, choose one of the following options to control whether a build will automatically be executed by the trigger:

        • Required except for owners and collaborators: When a pull request is created or updated by a repository owner or collaborator, builds will automatically be executed by the trigger. If an external contributor initiates the action, builds will only be executed after an owner or collaborator comments /gcbrun on the pull request.

        • Required: When a pull request is created or updated by any contributor, builds will only be executed after an owner or collaborator comments /gcbrun on the pull request. Builds are executed each time a change to a pull request is made.

        • Not required: When a pull request is created or updated by any contributor, builds will automatically be executed by triggers.

    • 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 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 Server trigger.

gcloud

To create a Bitbucket Server 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/REGION/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 Server project.
  • REPO_SLUG is the slug of your Bitbucket Server repository.
  • PROJECT_NUMBER is the project number of your Cloud project.
  • REGION is the region associated with your Bitbucket Server configuration.
  • 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 Server 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/REGION/bitbucketServerConfigs/ID"
      }
  }

Where:

  • REPO_SLUG is the slug of your Bitbucket Server repository.
  • PROJECT_KEY is the key of your Bitbucket Server project.
  • 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.
  • PROJECT_NUMBER is the project number of your Cloud project.
  • REGION is the region associated with your Bitbucket Server configuration.
  • ID is the ID of your BitbucketServerConfig.

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

Where:

  • PROJECT_NUMBER is your Cloud project number.
  • PROJECT_ID is your Cloud project ID.

Data sharing

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

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

  • Cloud project ID
  • Trigger name

Data sharing is automatically enabled for Bitbucket Server.

What's next