Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 45 ยท Operations and Delivery

Cloud Build, Cloud Deploy and CI/CD

Build a CI/CD pipeline on Google Cloud: cloudbuild.yaml steps, triggers, Artifact Registry, Cloud Run deploys and Cloud Deploy rollouts, with a pipeline runner stand-in.

Continuous integration and delivery (CI/CD) means that every change to your code is built, tested and, if it passes, shipped automatically. People stop deploying from laptops, releases become small and boring, and a bad change is caught by a machine before a customer sees it.

Google Cloud has two main services for this. Cloud Build runs your build and test steps. Cloud Deploy manages the delivery of a built release through stages such as dev, staging and production. This lesson shows how a typical pipeline from Git to Cloud Run fits together, then lets you run a miniature pipeline in your browser.

The overall flow

From commit to production

Trigger A push or pull request starts a build.
Build and test Steps run in containers.
Publish The image goes to Artifact Registry.
Deploy Cloud Run or GKE gets the new version.

Each stage only runs if the one before it succeeded.

1 / 2
flowchart LR
  G["Git push"] --> T["Cloud Build trigger"]
  T --> B["Build and test steps"]
  B --> A["Artifact Registry"]
  A --> D["Cloud Run revision"]

A typical path from a Git push to a running service.

cloudbuild.yaml

A build is a list of steps. Each step runs inside a container image (called a builder) and receives your source code in a shared folder named /workspace. Because steps are just containers, you can run any tool that has an image. Here is a complete pipeline, read-only because it needs a Google Cloud project (the image path and service names are examples):

YAML
steps:
  # 1. Run the tests
  - name: node:22
    entrypoint: npm
    args: ["ci"]
  - name: node:22
    entrypoint: npm
    args: ["test"]

  # 2. Build the container image
  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - -t
      - europe-west1-docker.pkg.dev/$PROJECT_ID/apps/shop:$SHORT_SHA
      - .

  # 3. Push it to Artifact Registry
  - name: gcr.io/cloud-builders/docker
    args:
      - push
      - europe-west1-docker.pkg.dev/$PROJECT_ID/apps/shop:$SHORT_SHA

  # 4. Deploy it to Cloud Run
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    entrypoint: gcloud
    args:
      - run
      - deploy
      - shop
      - --image=europe-west1-docker.pkg.dev/$PROJECT_ID/apps/shop:$SHORT_SHA
      - --region=europe-west1

images:
  - europe-west1-docker.pkg.dev/$PROJECT_ID/apps/shop:$SHORT_SHA

options:
  logging: CLOUD_LOGGING_ONLY

A few things to notice:

  • $PROJECT_ID and $SHORT_SHA (the first seven characters of the commit) are substitutions that Cloud Build fills in. You can define your own, which must start with an underscore, such as _REGION.
  • Steps run in order, and the build stops at the first step that fails. That is why the tests come first.
  • Tagging images with the commit hash, not just latest, tells you exactly which code is running.
  • The images field lists images Cloud Build should push and record in the build results.

You can start a build by hand from a folder that has this file:

Terminal
gcloud builds submit --config=cloudbuild.yaml .

Triggers

A trigger starts a build automatically. Common triggers fire on a push to a branch, on a new tag, or on a pull request. You connect a repository (GitHub, GitLab, Bitbucket or Cloud Source Repositories) and choose the event and branch pattern:

Terminal
gcloud builds triggers create github \
  --name=shop-main \
  --repo-owner=my-org \
  --repo-name=shop \
  --branch-pattern='^main$' \
  --build-config=cloudbuild.yaml

Use a separate trigger for pull requests that only runs tests and never deploys. Flag names and the available repository hosts change over time, so compare with the current gcloud builds triggers create --help.

Permissions: who does the deploy?

Every build runs as a service account. To deploy to Cloud Run, that account needs permission to deploy services and to act as the runtime service account of the service (IAM and service accounts). Grant only the roles the pipeline needs. A build service account with project-wide Owner is a serious risk, because anyone who can change cloudbuild.yaml can then run commands as it.

Never put passwords or API keys in cloudbuild.yaml or in substitutions. Read them from Secret Manager using the availableSecrets field, so they never appear in the repository.

Run a pipeline in miniature

The stand-in below is not Cloud Build. It runs on Node.js on our server, with no containers: each "step" is a plain JavaScript function. What it does copy is the behavior you rely on: ordered steps, substitutions in step arguments, and stopping at the first failure.

Node.js Stand-in: a pipeline runner with substitutions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In build 2 the tests fail, so nothing is built, pushed or deployed. That "fail early, deploy nothing" rule is the main reason to put tests in the pipeline.

Cloud Deploy and staged rollouts

Cloud Build is good at producing a release. Cloud Deploy is built for delivering it. You describe a delivery pipeline with an ordered list of targets, for example dev, staging and prod. Creating a release deploys to the first target, and you promote it to the next. A target can require approval before a rollout starts, which gives you a human gate before production.

Terminal
gcloud deploy releases create rel-2025-01 \
  --delivery-pipeline=shop-pipeline \
  --region=europe-west1 \
  --images=shop=europe-west1-docker.pkg.dev/my-project/apps/shop:a1b2c3d

Cloud Deploy supports Cloud Run, GKE and some other targets, and can run strategies such as canary (send a small share of traffic first) when the target supports it. Release names and flags are examples; check the current documentation for the exact skaffold and pipeline files it needs.

A staged promotion stand-in

This stand-in models only the idea of promotion with an approval gate. It is plain JavaScript and has no connection to the real service.

Node.js Stand-in: promote a release through stages

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Good habits

  • Build the image once and promote the same image through environments; do not rebuild for each stage.
  • Keep the pipeline definition in the same repository as the code.
  • Provision the infrastructure itself with Terraform, and keep application deploys in the pipeline.
  • Watch builds with Cloud Logging, and set an alert for failures on the main branch.
  • Keep builds fast: use small builder images and cache dependencies, because long builds slow everyone down and cost more.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In cloudbuild.yaml, what happens when a step fails?

    Steps run in order and the build fails at the first failing step, which is why tests belong before the deploy.
  2. 2. What is the main job of Cloud Deploy compared with Cloud Build?

    Cloud Build produces the artifacts. Cloud Deploy promotes a release through targets such as dev, staging and prod.
  3. 3. Why tag images with $SHORT_SHA instead of only latest?

    A commit-based tag points to one exact build, while latest moves and hides what is deployed.
  4. 4. Where should an API key used during a build be stored?

    Secret Manager keeps the secret out of the repository and build configuration.

Summary

  • CI/CD builds, tests and ships every change automatically; Cloud Build runs ordered container steps defined in cloudbuild.yaml.
  • Triggers start builds from pushes, tags or pull requests, and builds run as a service account that should have minimal roles.
  • A common path is Git, Cloud Build, Artifact Registry, then Cloud Run or GKE.
  • Cloud Deploy promotes one release through targets with optional approvals and canary rollouts.
  • The stand-ins show ordered steps with substitutions and a gated promotion; the real services run containers in Google's infrastructure.

Next: Vertex AI and the Gemini API