GCP DevOps Project

Sprint 04

Cloud Build trigger

Cloud Build triggers let you automatically start build jobs in Google Cloud Build whenever specific events occur in your GitHub repository—just like webhooks in Jenkins. By connecting your repo to Cloud Build through a trigger, you can ensure consistent, repeatable builds for your CI/CD pipeline.

Why Use a Cloud Build Trigger?

In Jenkins, you configure webhooks to detect pushes or pull requests in GitHub. Cloud Build provides the same capability natively with Cloud Build triggers, which listen for repository events and kick off builds defined in your cloudbuild.yaml.

Note

Before you begin, make sure you’ve granted Cloud Build access to your GitHub repository. See Create and Manage Triggers for detailed steps.

The image is a flow diagram showing a process from GitHub to Cloud Build via a Cloud Build Trigger.

Common Trigger Events

When creating a trigger, you specify which events should start a build. Typical events include:

Event TypeDescription
Push to main or masterIdeal for deploying from the primary branch
Push to a specific branchBuild feature or release branches on demand
Pull request creation/updateTest code before merging changes into protected branches

The image shows a document icon with three location markers and text stating that any push on the main/master branch will trigger a Cloud Build.

How It Works

  1. Define the trigger
    In the Cloud Console or via gcloud, link your GitHub repo and select the event and branch filters.

  2. Provide your build configuration
    Cloud Build looks for a cloudbuild.yaml at the repo root. Each step runs in its own container image, in sequence:

    steps:
      - name: 'gcr.io/cloud-builders/docker'
        args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app:$COMMIT_SHA', '.']
      - name: 'gcr.io/cloud-builders/docker'
        args: ['push', 'gcr.io/$PROJECT_ID/my-app:$COMMIT_SHA']
    images:
      - 'gcr.io/$PROJECT_ID/my-app:$COMMIT_SHA'
    
  3. Trigger execution
    When GitHub detects your specified event (e.g., push to main), it notifies Cloud Build, which then runs your pipeline automatically.

Warning

Ensure your cloudbuild.yaml is valid and located at the repository root. Otherwise, triggers will fail with a configuration error.

Next Steps

  • Configure and test your Cloud Build trigger.
  • Monitor build history in the Cloud Console under Cloud Build > History.
  • Integrate additional notifications or approvals as needed.

References

Watch Video

Watch video content

Previous
Cloudbuild in detail