Build triggers configuration in GCP - Time & Space Complexity
When setting up build triggers in Google Cloud, it's important to understand how the number of triggers affects the time it takes to process build events.
We want to know how the system's work grows as we add more triggers.
Analyze the time complexity of the following operation sequence.
gcloud beta builds triggers create github \
--name="trigger-1" \
--repo-name="my-repo" \
--branch-pattern="^main$" \
--build-config="cloudbuild.yaml"
gcloud beta builds triggers create github \
--name="trigger-2" \
--repo-name="my-repo" \
--branch-pattern="^dev$" \
--build-config="cloudbuild.yaml"
This sequence creates two build triggers that start builds when code is pushed to specific branches.
- Primary operation: Creating a build trigger via API call.
- How many times: Once per trigger created.
Each new trigger requires one API call to create it. So, as the number of triggers grows, the total API calls grow at the same rate.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The number of API calls increases directly with the number of triggers.
Time Complexity: O(n)
This means the time to create triggers grows linearly with how many triggers you add.
[X] Wrong: "Creating multiple triggers happens all at once, so time stays the same no matter how many triggers."
[OK] Correct: Each trigger creation is a separate API call, so more triggers mean more calls and more time overall.
Understanding how operations scale with input size helps you design efficient cloud workflows and shows you can think about system behavior beyond just writing code.
"What if we batch create triggers in a single API call? How would the time complexity change?"
Practice
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 CQuick Check:
Build trigger = auto build start [OK]
- Confusing triggers with manual deployment
- Thinking triggers monitor servers
- Assuming triggers create repositories
Solution
Step 1: Identify correct branch filter syntax
Cloud Build uses regex in branchName to filter branches.Step 2: Match regex syntax for main branch
"^main$" matches exactly the main branch name.Final Answer:
trigger: branchName: "^main$" -> Option AQuick Check:
branchName uses regex = trigger: branchName: "^main$" [OK]
- Using branch instead of branchName
- Not using regex anchors ^ and $
- Using incorrect field like branchFilter
trigger:
branchName: "^release-.*$"
build:
steps:
- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/my-project/my-app:$SHORT_SHA", "."]What happens when a commit is pushed to branch
release-v1.2?Solution
Step 1: Check branchName regex against branch
Regex "^release-.*$" matches any branch starting with 'release-', so 'release-v1.2' matches.Step 2: Understand build step and tagging
The build uses $SHORT_SHA to tag the image, so image is tagged with commit SHA.Final Answer:
The build starts and tags the image with the commit SHA -> Option BQuick Check:
Branch matches regex and tags with SHA = The build starts and tags the image with the commit SHA [OK]
- Assuming branch does not match regex
- Thinking image tags as 'latest'
- Confusing syntax error with correct YAML
trigger:
branchName: "main"
build:
steps:
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]But the build never starts when you push to
main. What is the likely error?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 DQuick Check:
branchName needs regex = branchName should be a regex like "^main$" [OK]
- Using plain branch name without regex anchors
- Confusing branchName with tagName
- Thinking substitutions are mandatory
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?Solution
Step 1: Identify correct tag filter syntax
Use tagName with regex "^v.*$" to match tags starting with 'v'.Step 2: Verify build steps order and commands
First step builds Docker image tagged with $TAG_NAME, second step deploys app.Step 3: Check for errors in other options
trigger: branchName: "^v.*$" build: steps: - name: "gcr.io/cloud-builders/docker" args: ["build", "-t", "gcr.io/my-project/my-app:$TAG_NAME", "."] - name: "gcr.io/cloud-builders/gcloud" args: ["app", "deploy"] uses branchName instead of tagName, C misses Docker build step, D tags image as latest without deploy step.Final Answer:
trigger: tagName: "^v.*$" build: steps: - name: "gcr.io/cloud-builders/docker" args: ["build", "-t", "gcr.io/my-project/my-app:$TAG_NAME", "."] - name: "gcr.io/cloud-builders/gcloud" args: ["app", "deploy"] -> Option AQuick Check:
tagName regex and two steps build+deploy = trigger: tagName: "^v.*$" build: steps: - name: "gcr.io/cloud-builders/docker" args: ["build", "-t", "gcr.io/my-project/my-app:$TAG_NAME", "."] - name: "gcr.io/cloud-builders/gcloud" args: ["app", "deploy"] [OK]
- Using branchName instead of tagName for tags
- Missing one of the required build steps
- Incorrect tagName regex syntax
Ready to prove it?
Pass the test and this pattern moves unknown
