← Back to list

Working with Cloud Build

In this post, I’ll walk you through the key commands from the Google Cloud Skill Boost lab on Cloud Build. If you are into cloud or DevOps…

Suraj Bhattarai · 2025-09-17 09:32 · 50 claps · 2.2 min read
#cloud-build #google-cloud-platform #cloud-build-commands #cloud-engineering #google-cloud-skills-boost
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud 🥊 · Combat Sports

Handy Commands for Cloud & DevOps Engineers

Working with Cloud Build

Google Build Icon

Google Build Icon

In this post, I’ll walk you through the key commands from the Google Cloud Skill Boost lab on Cloud Build. If you are into cloud or DevOps, these commands will come up a lot.

This is not a copy of the lab. Instead, it’s a reference you can come back to. We’ll keep things simple.

Step 1. Enable the APIs

Cloud Build will not work if the right APIs are off.

gcloud services enable cloudbuild.googleapis.com
gcloud services enable artifactregistry.googleapis.com

That’s it. You are telling Google Cloud, “turn these on for my project.”

Step 2. Build a container with a Dockerfile

First we make a small script:

nano quickstart.sh

Inside the file:

#!/bin/sh
echo "Hello, world! The time is $(date)."

Save it. Then give it permission to run:

chmod +x quickstart.sh

Now the Dockerfile:

nano Dockerfile
FROM alpine
COPY quickstart.sh /
CMD ["/quickstart.sh"]

This means:

  • start from a tiny Linux image (Alpine).
  • copy our script inside.
  • run the script when the container starts.

Create a repository for Docker images

gcloud artifacts repositories create quickstart-docker-repo \
    --repository-format=docker \
    --location="REGION" \
    --description="Docker repository"

Replace REGION with something like us-central1.

Build and push the image

gcloud builds submit \
  --tag "REGION"-docker.pkg.dev/${DEVSHELL_PROJECT_ID}/quickstart-docker-repo/quickstart-image:tag1

This command does four things:

  1. Reads the Dockerfile.
  2. Builds the container.
  3. Tags it with the right name.
  4. Pushes it to Artifact Registry.

Step 3. Build with a config file

Instead of writing long commands, you can put instructions in a file. Here’s a file called cloudbuild.yaml:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1', '.' ]
images:
- 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'

Update the region:

export REGION="REGION"
sed -i "s/YourRegionHere/$REGION/g" cloudbuild.yaml

Run it:

gcloud builds submit --config cloudbuild.yaml

Now Cloud Build reads your YAML file and does the build.

Step 4. Add a test step

Let’s change the script so it can fail:

nano quickstart.sh
#!/bin/sh
if [ -z "$1" ]
then
  echo "Hello, world! The time is $(date)."
  exit 0
else
  exit 1
fi

This script succeeds if no arguments are passed, but fails if something is passed in.

The new config file

nano cloudbuild2.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1', '.' ]
- name: 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'
  args: ['fail']
images:
- 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'

Insert your region:

sed -i "s/YourRegionHere/$REGION/g" cloudbuild2.yaml

Run the build:

gcloud builds submit --config cloudbuild2.yaml

It will fail because the script exits with 1. Non-Zero code here means a Failure.

Check the exit code:

echo $?

You’ll see a non-zero number. That’s how scripts know a build failed.

Summary

We did four things:

  1. Turned on Cloud Build and Artifact Registry APIs.
  2. Built a Docker image from a Dockerfile and pushed it.
  3. Used a YAML file to run builds with less typing.
  4. Added a test step that shows how builds can succeed or fail.

This is the basic workflow for using Cloud Build. Start small, then expand with more steps like tests and deployments.

Thank you for reading so far. Follow for more Cloud (AWS, GCP & Azure) articles.


메타데이터
post_id
a1dbab278ff7
slug
working-with-cloud-build-a1dbab278ff7
url
https://medium.com/@immrbhattarai/working-with-cloud-build-a1dbab278ff7
canonical_url
https://medium.com/@immrbhattarai/working-with-cloud-build-a1dbab278ff7
author_url
https://medium.com/@immrbhattarai
status
ok
fetched_at
2026-09-03 03:15:59