Cloud Build enables you to create triggers to build from repositories
hosted on [GitLab Enterprise Edition](https://about.gitlab.com/enterprise/),
allowing you to execute builds in response to events such as commit pushes or
merge requests associated with your GitLab Enterprise Edition repository.

This page explains how you can enable trigger functionality on a GitLab
Enterprise Edition instance if your instance is hosted in a private network.

## Before you begin

-


  Enable the Cloud Build, Secret Manager, Compute Engine, and Service Networking 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,compute.googleapis.com,servicenetworking.googleapis.com&redirect=https://cloud.google.com/build/docs/automating-builds/gitlab/build-repos-from-gitlab-enterprise-edition-private-network)

<!-- -->

- Follow the instructions to [connect a GitLab Enterprise Edition host](https://docs.cloud.google.com/build/docs/automating-builds/gitlab/connect-host-gitlab-enterprise-edition).
- Follow the instructions to [connect a GitLab Enterprise Edition repository](https://docs.cloud.google.com/build/docs/automating-builds/gitlab/connect-repo-gitlab-enterprise-edition).

## Build repositories from GitLab Enterprise Edition in a private network

If your GitLab Enterprise Edition instance is only accessible within a
VPC network, you need to set up a
Service Directory service and build using private pools. The
project containing your VPC network can exist in a different
project than the one containing your Service Directory service.
Use the following instructions to ensure your instance is reachable prior to
creating triggers:

1. Enable the
   [Service Directory API](https://console.cloud.google.com/apis/library/servicedirectory.googleapis.com).

2. Ensure you have the **Project IAM Admin** role granted to the
   Google Cloud project you intend to create your
   Service Directory service in. To learn how to grant
   IAM roles, see
   [Configuring access to Cloud Build resources](https://docs.cloud.google.com/build/docs/securing-builds/configure-access-to-resources).

3. Set up a Service Directory service by completing the following steps:

   1. [Configure a namespace](https://docs.cloud.google.com/service-directory/docs/configuring-service-directory#configure_a_namespace)
      for your Google Cloud project.

      The region you specify in your namespace **must** match the region you
      specify in your Cloud Build host connection.
   2. [Configure a service](https://docs.cloud.google.com/service-directory/docs/configuring-service-directory#configure_a_service)
      in your namespace.

   3. [Configure an endpoint](https://docs.cloud.google.com/service-directory/docs/configuring-service-directory#configure_an_endpoint)
      for your registered service.

      When configuring an endpoint, you **must** use an internal IP address and
      specify an HTTPS port number in order for Cloud Build to reach your
      service.

   To learn more about private network access configuration, see
   [Configure private network access](https://docs.cloud.google.com/service-directory/docs/configuring-private-network-access).
   Service Directory also provides integration with services
   such as load balancers and Google Kubernetes Engine (GKE).
   To learn more, see
   [Service Directory and load balancing overview](https://docs.cloud.google.com/service-directory/docs/sd-lb-overview)
   or [Service Directory for GKE overview](https://docs.cloud.google.com/service-directory/docs/sd-gke-overview).
4. Grant Service Directory access to the Cloud Build service agent:

       export PROJECT_NUMBER=$(gcloud projects describe PROJECT_ID --format="value(projectNumber)")
       export CLOUD_BUILD_SERVICE_AGENT="service-$PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com"
       gcloud projects add-iam-policy-binding  PROJECT_ID_CONTAINING_SERVICE_DIRECTORY \
          --member="serviceAccount:$CLOUD_BUILD_SERVICE_AGENT" \
          --role="roles/servicedirectory.viewer"

   Replace the following:
   - <var translate="no">PROJECT_ID</var> is your Cloud Build project ID.
   - <var translate="no">PROJECT_ID_CONTAINING_SERVICE_DIRECTORY</var> is the ID of your Google Cloud project that contains your Service Directory.

   > [!NOTE]
   > **Note:** If you receive a `service account not found` error when granting the IAM role, you might need to [manually create the service agent](https://docs.cloud.google.com/iam/docs/create-service-agents).

5. Grant VPC network resource access to the Cloud Build service agent:

       export PROJECT_NUMBER=$(gcloud projects describe PROJECT_ID --format="value(projectNumber)")
       export CLOUD_BUILD_SERVICE_AGENT="service-$PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com"
       gcloud projects add-iam-policy-binding PROJECT_ID_CONTAINING_NETWORK_RESOURCE \
          --member="serviceAccount:$CLOUD_BUILD_SERVICE_AGENT" \
          --role="roles/servicedirectory.pscAuthorizedService"

   Replace the following:
   - <var translate="no">PROJECT_ID</var> is your Cloud Build project ID.
   - <var translate="no">PROJECT_ID_CONTAINING_NETWORK_RESOURCE</var> is the ID of your Google Cloud project that contains your network resource.
6. Use [private pools](https://docs.cloud.google.com/build/docs/private-pools/private-pools-overview) to run
   your builds. If you have not created a private pool, see [create a new
   private pool](https://docs.cloud.google.com/build/docs/private-pools/create-manage-private-pools).

7. Follow the instructions to
   [create a GitLab Enterprise Edition trigger](https://docs.cloud.google.com/build/docs/automating-builds/gitlab/build-repos-from-gitlab-enterprise-edition)
   to build repositories hosted on a GitLab Enterprise Edition instance.

   If you include a self-signed or private certificate when connecting your
   GitLab Enterprise Edition host to Cloud Build, you **must** set the
   host URI as the Subject Alternative Name (SAN) of your certificate.

Your GitLab Enterprise Edition trigger will now automatically invoke builds on
your GitLab Enterprise Edition instance based on your configuration.

## Use Service Directory to reach hosts outside Google Cloud

Service Directory uses the IP address range `35.199.192.0/19` to
connect your host outside of Google Cloud. You must add this range to
an allowlist in your firewall. Additionally, your private network needs to be
configured to route this range through the Cloud VPN or Cloud Interconnect
connection.

If your connection uses a Cloud Router, you can configure your connection to
[communicate](https://docs.cloud.google.com/network-connectivity/docs/router/how-to/advertising-custom-ip)
the range to your private network.

To learn more, see [Configure private network access](https://docs.cloud.google.com/service-directory/docs/configuring-private-network-access).

### Use Cloud Load Balancing to reach hosts outside Google Cloud

If your network configuration does not allow you to route the
Service Directory IP address range `35.199.192.0/19` to the
Cloud VPN or Cloud Interconnect, you can
[create a load balancer](https://docs.cloud.google.com/load-balancing/docs/l7-internal) using
Cloud Load Balancing that directs traffic to your host.

When you create the Service Directory endpoint, make sure to use
the IP address of the forwarding rule of the load balancer instead of the IP
address of your host. You can use an
[internal HTTPS load balancer](https://docs.cloud.google.com/load-balancing/docs/l7-internal/setting-up-int-https-hybrid)
or an
[internal transmission control protocol (TCP) load balancer](https://docs.cloud.google.com/load-balancing/docs/tcp/set-up-int-tcp-proxy-hybrid)
when creating your endpoint.

When creating your TCP load balancer, consider the following:

- Only a hybrid connectivity network endpoint group (NEG) is required to reach your host.
- The TCP load balancer does not require the unencrypted private key for your SSL certificate.
- Your Cloud VPN setup needs to use Cloud Router with global dynamic routing. If your Cloud VPN uses static routing, you can use a proxy that uses Cloud Service Mesh instead. To learn more, see [Set up network
  edge services for hybrid
  deployments](https://docs.cloud.google.com/traffic-director/tutorials/network-edge-services-multi-environment).

To learn more about creating an HTTPS load balancer, see
[Set up an internal Application Load Balancer with hybrid connectivity](https://docs.cloud.google.com/load-balancing/docs/l7-internal/setting-up-int-https-hybrid).
To learn more about creating a TCP load balancer, see
[Set up a regional internal proxy Network Load Balancer with hybrid connectivity](https://docs.cloud.google.com/load-balancing/docs/tcp/set-up-int-tcp-proxy-hybrid).

## Data sharing

The data sent to GitLab Enterprise Edition from Cloud Build helps you
identify triggers by name and see build results on your GitLab Enterprise
Edition repositories.

The following data is shared between Cloud Build and GitLab
Enterprise Edition:

- Google Cloud project ID
- Trigger name

## What's next

- Learn how to [manage build triggers](https://docs.cloud.google.com/build/docs/automating-builds/create-manage-triggers).
- Learn how to [perform blue/green deployments on Compute Engine](https://docs.cloud.google.com/build/docs/deploying-builds/deploy-compute-engine).