Connect to a GitHub repository (original) (raw)

This page explains how to connect a GitHub repository to Cloud Build. To learn more about Cloud Build repositories, see Cloud Build repositories.

Before you begin

Console

To connect your GitHub repository to Cloud Build:

  1. Open the Repositories page in the Google Cloud console.
    Open the Repositories page
    You see the Repositories page.
  2. In the project selector in the top bar, select your Google Cloud project.
  3. At the top of the page, select the 2nd gen tab.
  4. Click Create host connection to connect a new host to Cloud Build.
  5. On the left panel, select GitHub as your source provider.
  6. In the Configure Connection section, enter the following information:
    1. Region: Select a region for your connection.
    2. Name: Enter a name for your connection.
  7. Click Connect.
    After you click the Connect button, you are asked to authorize the Cloud Build GitHub App to access your GitHub account. You may revoke access to the app by uninstalling or deleting the App from your host at any time.
    Cloud Build requests authorization of your GitHub user account and stores the resulting authorization token as a secret in Secret Manager in your project. The authorization token is used to validate access of your user account for the installation of the Cloud Build GitHub App and to the repositories linked. The Cloud Build Service Agent account (service-{projectNumber}@gcp-sa-cloudbuild.iam.gserviceaccount.com) is used to access your secret. To view your secret, see List secrets and view secret details.
    After authorizing the Cloud Build GitHub App, you are redirected to the Cloud Build Repositoriespage.

gcloud

To connect your GitHub host using gcloud, complete the following steps:

  1. Enter the following command to initiate a connection to your GitHub repository:
 gcloud builds connections create github CONNECTION_NAME --region=REGION  

Where:

  1. Log into your github.com account.
  2. Follow the link to authorize the Cloud Build GitHub App.
    After authorizing the app, Cloud Build stores an authentication token as a secret in Secret Manager in your Google Cloud project. You can view your secrets on the **Secret Manager**page.
  3. Install the Cloud Build GitHub App in your account or in an organization you own.
    Permit the installation using your GitHub account and select repository permissions when prompted.
  4. Verify the installation of your GitHub connection by running the following command:
 gcloud builds connections describe CONNECTION_NAME --region=REGION  

Where:

Connect to a GitHub host programmatically

Terraform

You can connect your GitHub host to Cloud Build using the Google Terraform provider by completing the following steps:

  1. Install the Cloud Build GitHub Appon your GitHub account or in an organization you own.
  2. Create a personal access token.
    Make sure to set your token to have no expiration date and select the following permissions when prompted in GitHub: repo and read:user. If your app is installed in an organization, make sure to also select the read:org permission.
    After you generate your personal access token, save your generated token in a secure place. You will use the generated token in the following steps.

In the following example, the code snippet does the following:

Where:

gcloud

To connect your GitHub host using an existing token and installation ID obtained from a previous connection, complete the following steps:

  1. Install the Cloud Build GitHub Appon your GitHub account or in an organization you own.
  2. Create a personal access token.
    Make sure to set your token to have no expiration date and select the following permissions when prompted in GitHub: repo and read:user. If your app is installed in an organization, make sure to also select the read:org permission.
    After you generate your personal access token, save your generated token in a secure place. You will use the generated token in the following steps.
  3. Store your token in Secret Manager in your Google Cloud project by running the following command:
    echo -n TOKEN | gcloud secrets create SECRET_NAME --data-file=-  

Where:

  1. Grant access to the Cloud Build Service Agent on the secret, whereSECRET_NAME is the name of your secret as stored in Secret Manager:
    PROJECT_ID=$(gcloud config list --format="value(core.project)") PN=$(gcloud projects describe ${PROJECT_ID} --format="value(projectNumber)") CLOUD_BUILD_SERVICE_AGENT="serviceiam.gserviceaccount.com-${PN}@gcp-sa-cloudbuild." gcloud secrets add-iam-policy-binding SECRET_NAME \ --member="serviceAccount:${CLOUD_BUILD_SERVICE_AGENT}" \ --role="roles/secretmanager.secretAccessor"
  2. Create your GitHub connection:
  gcloud builds connections create github CONNECTION_NAME \  
    --authorizer-token-secret-version=projects/PROJECT_ID/secrets/SECRET_NAME/versions/1  \  
    --app-installation-id=INSTALLATION_ID --region=REGION  

Where:

Connect a GitHub repository

Console

To connect a GitHub repository to a host connection, complete the following steps:

  1. Open the Repositories page in the Google Cloud console.
    Open the Repositories page
    You see the Repositories page.
  2. At the top of the page, select the 2nd gen tab.
  3. Click Link Repositories to link repositories from your connection.
    You see the Connect Repositories panel.
  4. In the Connect Repositories panel, enter the following information:
    1. Connection: Select a connection from the drop-down menu.
    2. Repository: Select a repository to link to your connection.
    3. Repository Name: Enter a name for your repository.
      1. Generated: Select this option for Cloud Build to automatically generated repository names on your behalf for selected repositories.
      2. Manual: Select this option to manually specify names for your selected repositories.
        If you select Manual, you can modify the names for your selected repositories in the Repository names section.
  5. Click Link to link your repository to your connection.

gcloud

To add a GitHub repository to your connection, enter the following command:

   gcloud builds repositories create REPO_NAME \
     --remote-uri=REPO_URI \
     --connection=CONNECTION_NAME --region=REGION

Where:

Terraform

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

resource "google_cloudbuildv2_repository" "my_repository" { project = "PROJECT_ID" location = "REGION" name = "REPO_NAME" parent_connection = google_cloudbuildv2_connection.my_connection.name remote_uri = "URI" }

Where:

What's next