You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Cloud Build triggers automate your CI/CD pipeline by starting builds in response to source code changes. When a developer pushes a commit, opens a pull request, or creates a tag, a trigger can automatically run your build pipeline — testing, building, and deploying your application without manual intervention.
A trigger is a Cloud Build configuration that watches a source repository for changes and starts a build when specific conditions are met. Triggers connect your source code to your build pipeline.
| Component | Description |
|---|---|
| Source | The repository to watch (GitHub, GitLab, Bitbucket, Cloud Source Repos) |
| Event | What triggers the build (push, pull request, tag) |
| Filter | Branch or tag patterns to match |
| Build config | The cloudbuild.yaml to execute (or inline steps) |
| Substitutions | Variables passed to the build |
| Service account | Identity used to run the build |
gcloud builds triggers create github \
--name="deploy-on-push" \
--repo-name=my-repo \
--repo-owner=my-org \
--branch-pattern="^main$" \
--build-config=cloudbuild.yaml \
--service-account=projects/my-project/serviceAccounts/cloud-build@my-project.iam.gserviceaccount.com
gcloud builds triggers create github \
--name="pr-checks" \
--repo-name=my-repo \
--repo-owner=my-org \
--pull-request-pattern="^main$" \
--build-config=cloudbuild-pr.yaml \
--comment-control=COMMENTS_ENABLED_FOR_EXTERNAL_CONTRIBUTORS_ONLY
gcloud builds triggers create github \
--name="release-on-tag" \
--repo-name=my-repo \
--repo-owner=my-org \
--tag-pattern="^v[0-9]+\.[0-9]+\.[0-9]+$" \
--build-config=cloudbuild-release.yaml
Substitutions allow you to parameterise builds. Cloud Build provides built-in substitutions and you can define custom ones.
| Variable | Description |
|---|---|
$PROJECT_ID | GCP project ID |
$BUILD_ID | Unique build ID |
$COMMIT_SHA | Full commit SHA |
$SHORT_SHA | Short commit SHA (first 7 characters) |
$BRANCH_NAME | Branch name |
$TAG_NAME | Tag name (for tag triggers) |
$REPO_NAME | Repository name |
$REPO_FULL_NAME | Full repository name (owner/repo) |
$TRIGGER_NAME | Name of the trigger |
gcloud builds triggers create github \
--name="deploy-staging" \
--repo-name=my-repo \
--repo-owner=my-org \
--branch-pattern="^main$" \
--build-config=cloudbuild.yaml \
--substitutions=_ENVIRONMENT=staging,_REGION=europe-west1
Use substitutions in your build config:
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'run'
- 'deploy'
- 'my-app'
- '--image=gcr.io/$PROJECT_ID/my-app:$SHORT_SHA'
- '--region=$_REGION'
- '--set-env-vars=ENVIRONMENT=$_ENVIRONMENT'
substitutions:
_ENVIRONMENT: 'development'
_REGION: 'europe-west1'
Only trigger builds when specific files change:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.