Build repositories from Bitbucket Server

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: 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 a 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.

      • Comment control: If you selected Pull request as your Event, you can choose settings to control whether non-comment events require additional interaction to trigger builds. 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 a user with write permissions, builds will automatically be executed by the trigger. If an external contributor initiates the action, builds will only be executed after an owner or a user with write permissions comments /gcbrun on the pull request. See COMMENTS_ENABLED_FOR_EXTERNAL_CONTRIBUTORS_ONLY.

        • Required: If a user with write permissions creates a pull request and includes /gcbrun in the pull request description, the build runs upon pull request creation. When a pull request is created or updated by any other contributor, builds will only be executed after an owner or a user with write permissions comments /gcbrun on the pull request. See COMMENTS_ENABLED.

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

        In Bitbucket Server, non-comment events include actions such as opening, modifying, and approving pull requests.

        Comment events, which include adding, editing, and deleting comments, will only trigger builds if the comment is from a user with write permissions or higher, and the comment contains /gcbrun.

        For more information on Bitbucket Server event types, see Bitbucket documentation on managing webhooks.

    • 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 builds triggers create bitbucketserver command in your terminal:

gcloud 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. PROJECT_KEY is case sensitive.
  • REPO_SLUG is the slug of your Bitbucket Server repository. For more information, see Bitbucket's guide on repository slugs.
  • 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 a regular expression that matches your branch if you want to set your trigger to build certain branches.
  • TAG_NAME is a regular expression that matches 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"
        "comment_control": "COMMENT_SETTING"
    }
}

Where:

  • REPO_SLUG is the slug of your Bitbucket Server repository. For more information, see Bitbucket's guide on repository slugs.
  • PROJECT_KEY is the key of your Bitbucket Server project. PROJECT_KEY is case sensitive.
  • BRANCH_NAME is the regular expression of your branch if you want to set your trigger to build certain branches.
  • TAG_NAME is the regular expression 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.
  • COMMENT_SETTING is the setting to control whether build triggers require /gcbrun in a comment for the build to run. For more information, see commentControl.

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 Google Cloud project number.
  • PROJECT_ID is your Google 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 shared between Cloud Build and Bitbucket Server:

  • Google Cloud project ID
  • Trigger name

Data sharing is automatically enabled for Bitbucket Server.

What's next