📖 Scenario: You work as a developer in a team that wants to automate the process of building and testing their application code whenever changes are made. Your team uses Google Cloud Platform (GCP) and wants to use Cloud Build to create a simple Continuous Integration/Continuous Deployment (CI/CD) pipeline.This project will guide you through setting up a basic Cloud Build configuration file that builds a Docker image and runs tests automatically.
🎯 Goal: Build a Cloud Build configuration file (cloudbuild.yaml) that defines steps to build a Docker image from source code, run tests, and push the image to Google Container Registry (GCR) automatically.
📋 What You'll Learn
Create a cloudbuild.yaml file with build steps
Define a build step to build a Docker image named gcr.io/my-project/my-app
Add a build step to run tests using a simple shell command
Add a build step to push the Docker image to Google Container Registry
Print the final build steps summary
💡 Why This Matters
🌍 Real World
Automating builds and tests helps teams deliver software faster and with fewer errors by catching problems early.
💼 Career
Knowing how to configure Cloud Build is essential for DevOps roles working with Google Cloud to implement CI/CD pipelines.
Progress0 / 4 steps
1
Create the initial Cloud Build configuration file
Create a file called cloudbuild.yaml and add a steps list with one step that uses the docker builder to build an image named gcr.io/my-project/my-app from the current directory (use dir: ".").
GCP
Hint
Use the docker builder image and the build command with the -t flag to tag the image.
2
Add a test step to the build configuration
Add a second step to the steps list that uses the gcr.io/cloud-builders/gcloud builder to run tests with the command sh -c "echo Running tests...".
GCP
Hint
Add a new step with the gcloud builder and use sh -c to run the echo command.
3
Add a push step to upload the Docker image
Add a third step to the steps list that uses the docker builder to push the image gcr.io/my-project/my-app to Google Container Registry.
GCP
Hint
Use the docker builder again with the push command and the image name.
4
Print the summary of build steps
Add a final step that prints Build steps completed successfully! using the gcr.io/cloud-builders/gcloud builder and the command sh -c "echo Build steps completed successfully!".
GCP
Hint
Use the gcloud builder with sh -c to print the success message.
Practice
(1/5)
1. What is the primary purpose of Google Cloud Build in a CI/CD pipeline?
easy
A. To store large amounts of data
B. To automate building, testing, and deploying code
C. To monitor network traffic
D. To manage user permissions
Solution
Step 1: Understand Cloud Build's role
Cloud Build automates the process of building, testing, and deploying code, which is essential in CI/CD pipelines.
Step 2: Eliminate unrelated options
Storing data, monitoring network traffic, and managing permissions are not primary functions of Cloud Build.
Final Answer:
To automate building, testing, and deploying code -> Option B
Thinking Cloud Build manages network or permissions
Assuming Cloud Build only builds code without testing or deploying
2. Which of the following is the correct way to specify a build step in a Cloud Build YAML file?
easy
A. actions:
- image: 'docker'
run: ['build', '-t', 'image', '.']
B. build_steps:
- container: 'gcr.io/cloud-builders/docker'
commands: ['build', '-t', 'image', '.']
C. steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/my-project/my-image', '.']
D. commands:
- use: 'docker'
execute: ['build', '-t', 'image', '.']
Solution
Step 1: Review Cloud Build YAML syntax
The correct key for build steps is 'steps', and each step uses 'name' for the builder image and 'args' for commands.
Step 2: Compare options to syntax
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/my-project/my-image', '.'] matches the correct syntax. Other options use incorrect keys like 'build_steps', 'actions', or 'commands'.
The step uses the 'ubuntu' image and runs the command 'echo Hello, Cloud Build!'. This command prints the message to the output.
Step 2: Determine expected output
Since 'echo' prints text, the output will be 'Hello, Cloud Build!'. There is no syntax error or missing image because 'ubuntu' is a valid public image.
Final Answer:
Hello, Cloud Build! -> Option D
Quick Check:
Echo command prints text output [OK]
Hint: Echo commands print their arguments as output [OK]
Common Mistakes:
Assuming build fails due to image name
Thinking no output is produced by echo
Confusing YAML syntax errors with valid syntax
4. You wrote this Cloud Build YAML but the build fails immediately. What is the error?
A. Missing the build context (dot) at the end of args
B. Incorrect builder image name
C. YAML indentation error
D. args should be a string, not a list
Solution
Step 1: Check Docker build command syntax
The Docker build command requires a build context, usually '.' at the end, to specify where to find the Dockerfile.
Step 2: Identify missing argument
The args list misses the '.' context argument, causing the build to fail immediately.
Final Answer:
Missing the build context (dot) at the end of args -> Option A
Quick Check:
Docker build needs context path '.' [OK]
Hint: Always end docker build args with '.' for context [OK]
Common Mistakes:
Assuming image name is wrong
Thinking YAML indentation is the issue
Using string instead of list for args
5. You want to create a Cloud Build pipeline that builds a Docker image, runs tests, and then deploys only if tests pass. Which YAML structure correctly represents this workflow?