Build and Deploy · Chapter 1 of 4 · 3 min

Cloud Build, Step by Step

The complete build file this site uses: test, build, push, deploy. What each step does, why the order matters, and where builds go wrong.

What Cloud Build is

Cloud Build is Google Cloud's managed build service. You give it a list of steps; it runs each one in its own container, in order, on a fresh machine, and stops at the first failure. A trigger connects it to the GitHub repository: on every push to main, a build starts.

The whole build file

steps:
  # Run the tests first. If any test fails, nothing is deployed.
  - name: 'node:22-slim'
    entrypoint: bash
    args: ['-c', 'npm ci --omit=dev --ignore-scripts && npm test']

  # Build the image
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'asia-south1-docker.pkg.dev/$PROJECT_ID/cloud-run-source-deploy/iamravi-site:$SHORT_SHA', '.']

  # Push it to Artifact Registry
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'asia-south1-docker.pkg.dev/$PROJECT_ID/cloud-run-source-deploy/iamravi-site:$SHORT_SHA']

  # Deploy it to Cloud Run
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args: ['run', 'deploy', 'iamravi-site',
           '--image=asia-south1-docker.pkg.dev/$PROJECT_ID/cloud-run-source-deploy/iamravi-site:$SHORT_SHA',
           '--region=asia-south1', '--platform=managed', '--quiet',
           '--update-env-vars=RELEASE_SHA=$SHORT_SHA']

images:
  - 'asia-south1-docker.pkg.dev/$PROJECT_ID/cloud-run-source-deploy/iamravi-site:$SHORT_SHA'

options:
  logging: CLOUD_LOGGING_ONLY

(The real file writes each args list one item per line; it is condensed here.)

Step 1: test, again

The tests already passed on the pull request and on main in GitHub Actions. They run once more here, in node:22-slim, the same image family the server is built from. This is the last gate before production, and it is deliberately independent of GitHub: if a change reaches main without passing checks, this step still refuses to ship it.

The && matters: if npm ci fails, the tests do not run, and the step fails.

Step 2: build the image

The Docker builder runs docker build with the repository as context. The image name has four parts:

Part Meaning
asia-south1-docker.pkg.dev Artifact Registry in the Mumbai region, next to the service
$PROJECT_ID Filled in by Cloud Build with the current project
cloud-run-source-deploy/iamravi-site Repository and image name
$SHORT_SHA The first seven characters of the commit ID

Tagging with the commit ID, rather than latest, means every image says exactly which code it contains. latest tells you nothing and changes under your feet.

Step 3: push

The image is uploaded to Artifact Registry, Google's private store for images. Keeping the registry in the same region as the service keeps pulls fast and avoids cross-region transfer.

Step 4: deploy

gcloud run deploy tells Cloud Run to create a new revision from this exact image. Two flags deserve attention:

Logging

logging: CLOUD_LOGGING_ONLY sends build logs to Cloud Logging instead of also keeping them in a storage bucket. Logs are searchable alongside everything else, and there is no extra bucket to secure.

Where builds go wrong

Reading build history

gcloud builds list --project=PROJECT_ID --limit=20 --format="table(id,status,createTime,finishTime)"

Always pass --project: the default project on a machine may not be the one you think (see gcloud Without Surprises). If the list is empty, the trigger may be regional; add --region=REGION.

Try it yourself

Read your own build file and answer: does it test before it builds, does it tag images with the commit, and does it update environment variables or replace them? Those three answers decide whether a bad build is stopped, traceable and harmless.