This page explains how to connect a [Bitbucket Cloud](https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-cloud/) repository
to Cloud Build.

## Before you begin

-


  Enable the Cloud Build API, if it is not already enabled.


  **Roles required to enable APIs**


  To enable APIs, you need the `serviceusage.services.enable` permission. If you
  created the project, then you likely already have this permission through the
  Owner role (`roles/owner`). Otherwise, you can get this permission through the
  Service Usage Admin role (`roles/serviceusage.serviceUsageAdmin`).
  [Learn how to grant roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access).

  [Enable the API](https://console.cloud.google.com/apis/enableflow?apiid=cloudbuild.googleapis.com&redirect=https://cloud.google.com/build/docs/automating-builds/bitbucket/connect-repo-from-bitbucket-cloud)

<!-- -->

- Follow instructions in [Connect to a Bitbucket Cloud host](https://docs.cloud.google.com/build/docs/automating-builds/bitbucket/connect-host-bitbucket-cloud).

## Connect to Bitbucket Cloud repositories

### Console

Connect Cloud Build to your Bitbucket Cloud repository by
doing the following steps:

1. Open the **Repositories** page in the Google Cloud console.

   [Open the Repositories page](https://console.cloud.google.com/cloud-build/repositories)

   You will see the **Repositories** page.
2. In the project selector, select your Google Cloud project.

3. Select the **2nd gen** tab.

4. Click **Link repository** to connect a new host to Cloud Build.

   You will see the **Link repositories** side panel.
5. **Connection**: Select a host connection from the drop-down menu.

6. **Repository**: Select a repository from the drop-down menu.

7. **Repository Resource Name**: This step creates resource names for the
   linked repositories.

   - **Generated**: Select this option for Cloud Build to automatically generate repository resource names on your behalf for selected repositories.
   - **Manual**: Select this option to manually specify repository resource names.

   If you select **Manual** , then you can modify the names for your
   selected repositories in the **Repository names** section.
8. Click **Link**.

### gcloud

Connect to your Bitbucket Cloud repository:

    gcloud builds repositories create REPO_NAME \
        --remote-uri=https://bitbucket.org/WORKSPACE/REPOSITORY.git \
        --connection=CONNECTION_NAME --region=REGION --project=PROJECT_ID

Where:

- <var translate="no">REPO_NAME</var> is the name of your repository.
- <var translate="no">WORKSPACE</var> is the workspace ID for your repository.
- <var translate="no">REPOSITORY</var> is the name of the repository.
- <var translate="no">REGION</var> is the [region](https://docs.cloud.google.com/build/docs/locations) for your connection.
- <var translate="no">CONNECTION_NAME</var> is the name given to the Bitbucket Cloud host connection created in Cloud Build from [Connect to a Bitbucket Cloud host](https://docs.cloud.google.com/build/docs/automating-builds/bitbucket/connect-host-bitbucket-cloud#connect-host).
- <var translate="no">PROJECT_ID</var> is your Google Cloud [project ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects#get_an_existing_project).

### Terraform

To add a Bitbucket Cloud repository to your connection, add the following
code snippet to your Terraform configuration:

        resource "google_cloudbuildv2_repository" "my-repository" {
           project = "PROJECT_ID"
           name = "REPO_NAME"
           location = "REGION"
           parent_connection = google_cloudbuildv2_connection.RESOURCE_NAME.name
           remote_uri = "https://bitbucket.org/WORKSPACE_ID/REPO_NAME.git"
        }

Where:

- <var translate="no">PROJECT_ID</var> is your Google Cloud [project ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects#get_an_existing_project).
- <var translate="no">WORKSPACE_ID</var> is the workspace ID for your repository.
- <var translate="no">REPO_NAME</var> is the name of your repository.
- <var translate="no">REGION</var> is the [region](https://docs.cloud.google.com/build/docs/locations) for your connection.
- <var translate="no">RESOURCE_NAME</var> is the name of the `google_cloudbuildv2_connection` resource. For example, `my-connection` from the Terraform example in [Connect to a Bitbucket Cloud host](https://docs.cloud.google.com/build/docs/automating-builds/bitbucket/connect-host-bitbucket-cloud#terraform).

## What's next

- Learn how to [build repositories from a Bitbucket Cloud](https://docs.cloud.google.com/build/docs/automating-builds/bitbucket/build-repos-from-bitbucket-cloud).
- Learn how to [view build results](https://docs.cloud.google.com/build/docs/view-build-results).
- Learn how to [perform blue/green deployments on Compute Engine](https://docs.cloud.google.com/build/docs/deploying-builds/deploy-compute-engine).