📖 Scenario: You are setting up an automated build process in Google Cloud Platform (GCP). This process will start a build whenever code is pushed to a specific branch in your repository.
🎯 Goal: Create a build trigger configuration that listens to pushes on the main branch of a GitHub repository and starts a build using a specified build configuration file.
📋 What You'll Learn
Create a dictionary named trigger_config with the exact keys and values for the build trigger.
Add a branch_name variable set to main to specify which branch triggers the build.
Use the trigger_config dictionary to set the trigger's branch filter using the branch_name variable.
Complete the build trigger configuration with the required fields for GitHub repository and build configuration file.
💡 Why This Matters
🌍 Real World
Automating builds on code changes helps teams deliver software faster and with fewer errors.
💼 Career
Understanding build triggers is essential for roles in DevOps, cloud engineering, and software development automation.
Progress0 / 4 steps
1
Create initial build trigger dictionary
Create a dictionary called trigger_config with these exact keys and values: 'name' set to 'my-build-trigger', 'description' set to 'Trigger for main branch builds', and 'github' set to an empty dictionary {}.
GCP
Hint
Use a dictionary with keys 'name', 'description', and 'github'. Set 'github' to an empty dictionary.
2
Add branch name variable
Create a variable called branch_name and set it to the string 'main'.
GCP
Hint
Assign the string 'main' to the variable named branch_name.
3
Set branch filter in trigger configuration
Inside the trigger_config dictionary, set the github key to a dictionary with a key 'push' that contains another dictionary with the key 'branch' set to the variable branch_name.
GCP
Hint
Update the 'github' key to include a nested dictionary for 'push' with 'branch' set to branch_name.
4
Complete build trigger with repo and build config
Add to trigger_config['github'] the keys 'owner' set to 'example-owner', 'name' set to 'example-repo', and add a key 'build' with a dictionary containing 'build_config' set to 'cloudbuild.yaml'.
GCP
Hint
Add the repository owner and name, and specify the build configuration file inside the 'github' dictionary.
Practice
(1/5)
1. What is the main purpose of a build trigger in Google Cloud Build?
easy
A. To monitor server health
B. To manually deploy an application
C. To automatically start a build when code changes occur
D. To create a new repository
Solution
Step 1: Understand build triggers role
Build triggers watch for code changes and start builds automatically.
Step 2: Compare options with purpose
Only To automatically start a build when code changes occur describes automatic build start on code changes.
Final Answer:
To automatically start a build when code changes occur -> Option C
Quick Check:
Build trigger = auto build start [OK]
Hint: Build triggers start builds automatically on code changes [OK]
Common Mistakes:
Confusing triggers with manual deployment
Thinking triggers monitor servers
Assuming triggers create repositories
2. Which of the following is the correct syntax to specify a branch filter in a Cloud Build trigger YAML?
easy
A. trigger:
branchName: "^main$"
B. trigger:
branchName: "main"
C. trigger:
branch: "main"
D. trigger:
branchFilter: "main"
Solution
Step 1: Identify correct branch filter syntax
Cloud Build uses regex in branchName to filter branches.
But the build never starts when you push to main. What is the likely error?
medium
A. The build steps are missing the Docker build command
B. The trigger YAML is missing the substitutions field
C. The trigger must specify a tagName instead of branchName
D. branchName should be a regex like "^main$"
Solution
Step 1: Check branchName format in trigger
Cloud Build expects branchName as a regex pattern, not plain string.
Step 2: Correct branchName to regex
Changing branchName to "^main$" matches exactly the main branch and triggers build.
Final Answer:
branchName should be a regex like "^main$" -> Option D
Quick Check:
branchName needs regex = branchName should be a regex like "^main$" [OK]
Hint: branchName must be regex, not plain text [OK]
Common Mistakes:
Using plain branch name without regex anchors
Confusing branchName with tagName
Thinking substitutions are mandatory
5. You want to create a build trigger that starts only when a tag matching v* is pushed, and the build should run two steps: first build a Docker image, then deploy it. Which YAML snippet correctly configures this trigger?