Courseiva
Question 483 of 964
Building and testing applicationsmediumMultiple ChoiceObjective-mapped

Deploy Only Main Branch with Cloud Build Substitution Variables

A team uses Cloud Build to build a Go application and deploy it to Cloud Run. The build triggers from a GitHub repository. The team wants to ensure that only commits to the 'main' branch trigger a production deployment, while other branches trigger a build but not a deployment. How should they configure this?

Quick Answer

The correct approach is to use a single Cloud Build trigger with a substitution variable for the branch name, and include a conditional step that runs deployment only when the variable equals 'main'. This works because Cloud Build automatically injects the triggering branch into the predefined substitution variable `$_BRANCH`, allowing you to write a simple `if` condition in your `cloudbuild.yaml` that gates the deploy step while still running the build step for all branches. On the Google Professional Cloud Developer exam, this scenario tests your understanding of Cloud Build triggers and substitution variables as a way to avoid duplicating triggers or using external filtering services. A common trap is to create separate triggers for each branch, which violates the principle of single-source configuration and adds maintenance overhead. Remember the memory tip: “One trigger, one build, conditional deploy—let `$_BRANCH` be the gatekeeper.”

⚠ Common exam trap

The PCD exam often tests the distinction between using a single trigger with conditional logic versus multiple triggers. Candidates may incorrectly assume that multiple triggers are required or that GitHub can filter events at the source, when in fact Cloud Build handles branch filtering through the predefined substitution variable $BRANCH_NAME (not $_BRANCH) and conditional steps.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use a single Cloud Build trigger with a substitution variable for the branch name, and include a conditional step that runs deployment only when the variable equals 'main'.

Cloud Build provides the predefined substitution variable $BRANCH_NAME, which automatically receives the branch name from the trigger event. By using a conditional step in cloudbuild.yaml that checks if $BRANCH_NAME equals 'main', you can run the deployment step only for main branch commits, while still building on all branches. This approach keeps a single trigger and avoids unnecessary duplication or external filtering.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Configure the GitHub repository to only send push events from the main branch to Cloud Build.

    Why it's wrong here

    Cloud Build receives events from GitHub; branch filtering is done at trigger level.

  • Use a conditional step in cloudbuild.yaml that checks the $_BRANCH variable and skips deployment if not main.

    Why it's wrong here

    $_BRANCH is not a predefined variable; $BRANCH_NAME is.

  • Use a single Cloud Build trigger with a substitution variable for the branch name, and include a conditional step that runs deployment only when the variable equals 'main'.

    Why this is correct

    Use $BRANCH_NAME and condition in build config.

  • Create two separate Cloud Build triggers: one for main branch with deployment step, and one for all branches without deployment step.

    Why it's wrong here

    This works but is not the most efficient; also possible with a single trigger.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on PCD

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer is setting up a Cloud Build configuration file for a Node.js application. They want to ensure that build steps are executed only when changes are pushed to the 'main' branch. What is the correct approach?

easy
  • A.Use a script in the build step to check the branch name
  • B.Use Cloud Scheduler to trigger builds based on time intervals
  • C.Use a condition in the build config file
  • D.Use a build trigger with a branch filter

Why D: Cloud Build triggers can be configured with a branch filter (e.g., `^main$`) that ensures builds are only initiated when changes are pushed to the specified branch. This is the native, declarative way to control build execution based on Git branch events, without requiring custom scripting or external scheduling.

Variation 2. A team is setting up a CI/CD pipeline using Cloud Build for a Node.js application. They want to ensure that only code from the main branch is deployed to production. Which TWO practices should they implement?

medium
  • A.Store secrets in Cloud Build and use them in build steps.
  • B.Use Cloud Build substitutions to inject environment variables.
  • C.Use branch triggers to run tests only on push to main.
  • D.Use Cloud Build's inverted match with branch pattern to exclude non-main branches.
  • E.Use a manual approval step in Cloud Deploy before promoting to production.

Why C: Using a branch trigger that runs only on push to main ensures that only main branch code triggers the pipeline. Adding a manual approval step in Cloud Deploy before promoting to production adds a gate to prevent automatic deployment of untested code. Storing secrets or using substitutions are good practices but do not specifically restrict deployment to the main branch.

Last reviewed: Jun 25, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This PCD practice question is part of Courseiva's free Google Cloud certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCD exam.