Build and promote containers with Cloud Build and Cloud Deploy
Build an image with Cloud Build, promote it with Cloud Deploy, and compare a keyless GitHub Actions pipeline with a rollback path.
By Invicly Tech, , in Google Cloud Engineering and Services. About 8 minutes to read.
In short
Cloud Build runs your build steps and pushes a container image to Artifact Registry. Cloud Deploy then promotes that same image through targets such as dev, staging and prod. You can also deploy from GitHub Actions without keys by using workload identity federation.
Explained simply
Delivery means getting code from Git to running software. Cloud Build does the first part. You give it a list of steps in a file, and it runs them in containers. A common set is: build a Docker image, test it, and push it.
The image goes to Artifact Registry, Google Cloud's store for container images. Each image has an address with a project, a repository, a name and a tag. Tag by the Git commit so you always know what is inside.
Cloud Deploy takes over after that. You describe a pipeline with targets, such as dev, staging and prod. A release is one version of the app. You promote the release from target to target. The image stays the same, which is the point.
Many teams already use GitHub Actions. That can deploy to Google Cloud too. The safe way is workload identity federation: GitHub proves who it is with a short-lived token, and Google Cloud lets it act as a service account. No key file is stored.
Plan for failure. Keep old images and releases, and practise a rollback before you need one.
Press Play on a diagram to walk through it one step at a time.
- Developer to Cloud Build: Push code or submit build1Push code or submit build
- Cloud Build to Artifact Registry: Push image with a tag2Push image with a tag
- Cloud Build to Cloud Deploy: Create a release3Create a release
- 4Roll out to dev targetCloud Deploy to Cloud Deploy: Roll out to dev target
- Developer to Cloud Deploy: Promote to prod (approval)5Promote to prod (approval)
- Cloud Deploy to Artifact Registry: Pull the same image6Pull the same image
- Artifact Registry
- Repository apps
- Image orders:3f2a9c1
- Cloud Build
- Trigger on main
- Build service account
- Cloud Deploy
- Pipeline orders
- Targets dev, staging, prod
- Cloud Run
- Service orders-dev
- Service orders
In depth
Writing a cloudbuild.yaml
A build has steps. Each step has a name, which is the builder image to run, and args. Built-in values like PROJECT_ID and SHORT_SHA are filled in for you. The images field lists images to push when all steps pass. You can run a build by hand with gcloud builds submit, or on a trigger tied to a Git branch. Builds run as a service account. Give that account only what it needs: push to Artifact Registry, and deploy only if you use it for that.
- steps run in order, each in a container
- $PROJECT_ID and $SHORT_SHA are built in
- images lists what to push
- Use a dedicated build service account
Promotion with Cloud Deploy
Cloud Deploy uses a delivery pipeline file and target files. Targets can be Cloud Run services, GKE clusters and others. Creating a release renders the manifests and rolls out to the first target. You then promote to the next. A target can require approval, which is a good guard on prod. Cloud Deploy uses Skaffold under the hood to render and deploy. If a rollout fails, you can roll the target back to its previous release.
- Pipeline = ordered targets
- Release = one version, promoted unchanged
- Approval can gate prod
- Rollback returns a target to the previous release
- 1Release created
- 2Dev target
Automatic
- 3Staging target
Promote
- 4Approval
- 5Prod target
GitHub Actions with federation
Set up a workload identity pool and provider that trusts your GitHub repository. Allow a service account to be used by identities from that repo. In the workflow, give the job id-token write permission and call google-github-actions/auth with the provider name and service account email. Then use gcloud or a deploy action. Restrict the trust to one repository and ideally one branch. Compared with Cloud Build and Cloud Deploy, this keeps everything in GitHub, while Cloud Deploy adds release tracking and approvals on the Google side.
- Pool and provider trust one repo
- Job needs id-token: write
- No JSON key stored
- Limit to a branch where possible
- Runs inside Google Cloud
- Release history and promotion
- Built-in approvals
- Rollback command
- Pipeline lives in GitHub
- Keyless auth
- Familiar to many teams
- Rollback is a redeploy you script
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Write a cloudbuild.yaml~35m
- 2Push to Artifact Registry~35m
- 3Promote through Cloud Deploy targets~35m
- 4Deploy from GitHub Actions with federation~35m
- 5Roll back to a previous release~35m
Key points
- A cloudbuild.yaml lists steps. Each step runs in a container, such as docker build and docker push.
- Artifact Registry stores images at addresses like us-docker.pkg.dev/PROJECT/REPO/IMAGE:TAG.
- Cloud Deploy defines a delivery pipeline and targets. A release is promoted from one target to the next with the same image.
- GitHub Actions can deploy using google-github-actions/auth with workload identity federation, so no long-lived key is stored.
- Rollback means redeploying a known good release. Keep old images and releases, and do not overwrite tags.
Key terms
Tap a card to flip it. 0/5 known
Examples
steps:
- name: gcr.io/cloud-builders/docker
args: ["build", "-t", "us-docker.pkg.dev/$PROJECT_ID/apps/orders:$SHORT_SHA", "."]
images:
- us-docker.pkg.dev/$PROJECT_ID/apps/orders:$SHORT_SHAMinimal cloudbuild.yaml. Region and names are placeholders.
Build and push an image with Cloud Build
Prerequisites: Cloud Build and Artifact Registry APIs enabled, a Dockerfile in the current folder, and a Docker repository named apps. Cloud Build has a free daily allowance of build minutes; check current limits. Steps: create the repo, run the build. Expected evidence: gcloud artifacts docker images list shows the image with your tag. Cleanup: delete the image or the repository.
gcloud artifacts repositories create apps \
--repository-format=docker --location=us
gcloud builds submit --config=cloudbuild.yaml \
--substitutions=SHORT_SHA=manual1
gcloud artifacts docker images list us-docker.pkg.dev/PROJECT_ID/apps
# cleanup
gcloud artifacts repositories delete apps --location=usPromote and roll back a release
Assume a pipeline named orders with targets dev and prod already exists. Create a release from the image, promote it, and if prod misbehaves, roll back. Expected evidence: gcloud deploy releases list shows the release, and the rollback restores the previous one on prod. Cleanup: delete the pipeline and targets with gcloud deploy delete. Names below are placeholders.
gcloud deploy releases create rel-001 \
--delivery-pipeline=orders --region=us-central1 \
--images=orders=us-docker.pkg.dev/PROJECT_ID/apps/orders:3f2a9c1
gcloud deploy releases promote --release=rel-001 \
--delivery-pipeline=orders --region=us-central1
gcloud deploy targets rollback prod \
--delivery-pipeline=orders --region=us-central1Quick reference
- cloudbuild.yaml: steps run in containers; images pushes results
- SHORT_SHA tags an image with the Git commit
- Image address: REGION-docker.pkg.dev/PROJECT/REPO/IMAGE:TAG
- Cloud Deploy: pipeline, targets, release, promote, rollback
- Promote the same image; do not rebuild per environment
- GitHub to Google Cloud: use workload identity federation
- Never store service account JSON keys in Git or CI secrets
- Keep old images so you can roll back
Common mistakes
- Tagging images as latest only. You cannot tell what is running or what to roll back to.
- Building a new image for each environment. Test the exact image you ship.
- Storing a service account JSON key in GitHub secrets when federation avoids it.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the address of an image named orders with tag 3f2a9c1 in repo apps, US multi-region, project example-prod.
- Exercise 2medium
Write a cloudbuild.yaml that builds and pushes an image tagged with the short commit id.
- Exercise 3hard
Compare the two delivery options for a small team and recommend one, with the rollback path.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do I need Cloud Deploy?
- No. It helps when you promote one release through several environments with approvals. For a single service, a simple pipeline may be enough.
- Is Container Registry still used?
- Use Artifact Registry for new work. It is the current image store on Google Cloud.
- Why does a tag need to be unique?
- A unique tag such as the commit id tells you exactly what runs and lets you go back to it.