<button value="1st-gen">1st gen</button> <button value="2nd-gen" default="">2nd gen</button>

This page explains how to connect a [GitHub Enterprise](https://docs.github.com/en/enterprise-server) repository to Cloud Build. To learn more about Cloud Build repositories, see [Cloud Build repositories](https://docs.cloud.google.com/build/docs/repositories).

## Before you begin

-


  Enable the Cloud Build and Secret Manager APIs, if any are 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 APIs](https://console.cloud.google.com/apis/enableflow?apiid=cloudbuild.googleapis.com,secretmanager.googleapis.com&redirect=https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github-enterprise)

<!-- -->

- Follow the instructions to [connect a GitHub Enterprise host](https://docs.cloud.google.com/build/docs/automating-builds/github/connect-host-github-enterprise).
- Ensure you have administrator access on repositories where you want to enable GitHub Enterprise triggers.

## Connect to a GitHub Enterprise repository

### Console

To connect a GitHub Enterprise repository to a host connection, complete 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 see the **Repositories** page.
2. At the top of the page, select the **2nd gen** tab.

3. Click **Link Repository** to link repositories from your connection.

   You see the **Link repositories** panel.
4. In the **Link 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.

      > [!NOTE]
      > **Note:** If you're a collaborator, you need administrator permissions in GitHub to see the repository.

5. Click **Link** to link your repository to your connection.

You have now successfully linked your GitHub Enterprise repository to your connection.

### gcloud

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

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

Where:

- <var translate="no">REPO_NAME</var> is the name of your repository.
- <var translate="no">REPO_URI</var> is the link to your GitHub repository.
- <var translate="no">CONNECTION_NAME</var> is the name given to the GitHub Enterprise host connection created in Cloud Build from [Connect to a GitHub Enterprise host](https://docs.cloud.google.com/build/docs/automating-builds/github/connect-host-github-enterprise#connecting_to_a_github_enterprise_host).
- <var translate="no">REGION</var> is the [region](https://docs.cloud.google.com/build/docs/locations) for your connection.

> [!NOTE]
> **Note:** If you're a collaborator, you need administrator permissions in GitHub to connect to the repository.

### Terraform

To add a GitHub Enterprise 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.my-connection.name
          remote_uri = "REMOTE_URI'
      }

Where:

- <var translate="no">PROJECT_ID</var> is your Google Cloud project ID.
- <var translate="no">REPO_NAME</var> is the name of your GitHub repository.
- <var translate="no">REGION</var> is the [region](https://docs.cloud.google.com/build/docs/locations) for your connection.
- <var translate="no">URI</var> is the remote URI of your repository. For example, `https://github.com/myuser/myrepo.git`.

> [!NOTE]
> **Note:** If you're a collaborator, you need administrator permissions in GitHub to connect to the repository.

## Next steps

- Learn how to [build repositories from GitHub Enterprise](https://docs.cloud.google.com/build/docs/automating-builds/github/build-repos-from-github-enterprise).
- Learn how to [build repositories from GitHub Enterprise in a private network](https://docs.cloud.google.com/build/docs/automating-builds/github/build-repos-from-github-enterprise-private-network).
- Learn how to [perform blue-green deployments on Compute Engine](https://docs.cloud.google.com/build/docs/deploying-builds/deploy-compute-engine).
- Learn how to [specify additional repositories](https://docs.cloud.google.com/build/docs/building/manage-dependencies) as dependencies to your build.