Google Cloud Build Conditional Step

Harsh Manvar
Google Cloud - Community
2 min readMay 9, 2023

--

Conditional step in Google Cloud Build

Overview

You may be familiar as a developer with Google Cloud Build, a fully managed platform for CI/CD that enables you to create, test, and deploy services in the GCP. However, using it could bring up queries like conditional steps in Google Cloud Build.

I wanted to know how to set up conditional steps in Google Cloud Build so that specific build step could only be carried out under specific circumstances or based on the Branch filter.

You can easily achieve this by managing multiple Build triggers or multiple build config files in the repository. However, I was looking for managing a single Build config/trigger with a condition option.

GCP Cloud Build

Normal config cloudbuild.yaml

substitutions:
_CLOUDSDK_COMPUTE_ZONE: us-central1-a
_CLOUDSDK_CONTAINER_CLUSTER: $_CLOUDSDK_CONTAINER_CLUSTER
steps:
- name: gcr.io/$PROJECT_ID/sonar-scanner:latest
args:
- '-Dsonar.host.url=https://sonar.test.io'
- '-Dsonar.login=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
- '-Dsonar.projectKey=test-service'
- '-Dsonar.sources=.'
- id: 'build test-service image'
name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA', '.']
- id: 'push test-service image'
name…

--

--