0
0
GCPcloud~7 mins

Build triggers configuration in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Build triggers automatically start a build process when you change your code. This helps you test and deploy your app without doing it manually every time.
When you want your app to build automatically after pushing code to a repository.
When you need to run tests every time someone updates the code.
When you want to deploy your app only after a successful build.
When you want to save time by automating repetitive build tasks.
When you want to keep your app updated with the latest code changes.
Config File - cloudbuild-trigger.yaml
cloudbuild-trigger.yaml
apiVersion: cloudbuild.googleapis.com/v1
kind: BuildTrigger
metadata:
  name: example-trigger
spec:
  description: Trigger build on push to main branch
  triggerTemplate:
    projectId: example-project
    repoName: example-repo
    branchName: ^main$
  build:
    steps:
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t', 'gcr.io/example-project/example-app', '.']
    images:
    - 'gcr.io/example-project/example-app'

This file defines a build trigger for Google Cloud Build.

  • metadata.name: The name of the trigger.
  • spec.description: A short explanation of what the trigger does.
  • spec.triggerTemplate: Defines which repository and branch to watch for changes.
  • spec.build.steps: The steps to run when the trigger activates, here it builds a Docker image.
  • spec.build.images: The Docker image to create and push.
Commands
This command creates a build trigger in Google Cloud Build using the configuration file. It tells Cloud Build to watch the specified repo and branch.
Terminal
gcloud builds triggers create cloud-source-repositories --trigger-config=cloudbuild-trigger.yaml
Expected OutputExpected
Created trigger [example-trigger].
--trigger-config - Specifies the YAML file that defines the build trigger.
This command lists all build triggers in your project so you can verify the trigger was created.
Terminal
gcloud builds triggers list
Expected OutputExpected
NAME DESCRIPTION TRIGGER TEMPLATE example-trigger Trigger build on push to main branch example-repo @ main
This command manually starts the build trigger to test if it works correctly without pushing code.
Terminal
gcloud builds triggers run example-trigger
Expected OutputExpected
Build started with ID: 12345678-1234-1234-1234-123456789abc
This command shows the most recent build to check its status and confirm the trigger ran successfully.
Terminal
gcloud builds list --limit=1
Expected OutputExpected
ID STATUS START_TIME 12345678-1234-1234-1234-123456789abc SUCCESS 2024-06-01T12:00:00Z
--limit - Limits the number of builds shown to the most recent one.
Key Concept

If you remember nothing else from this pattern, remember: build triggers automate your build process by watching your code changes and starting builds automatically.

Common Mistakes
Using the wrong branch name pattern in the trigger configuration.
The trigger will not activate if the branch name does not match exactly.
Use a correct regular expression like ^main$ to match the exact branch name.
Not specifying the correct repository name in the trigger config.
Cloud Build cannot find the code to watch and will not trigger builds.
Double-check the repository name matches exactly what is in Cloud Source Repositories.
Forgetting to push code after creating the trigger to test it.
The trigger will not run automatically until a matching code change happens.
Push a commit to the watched branch or run the trigger manually to test.
Summary
Create a build trigger configuration file to define when and how builds start.
Use gcloud commands to create, list, and run build triggers.
Verify builds start automatically when code changes happen on the specified branch.