0
0
GcpHow-ToBeginner · 4 min read

How to Deploy Using Cloud Build on Google Cloud Platform

To deploy using Cloud Build, create a cloudbuild.yaml file that defines build steps and triggers deployment commands. Then run gcloud builds submit to start the build and deployment process on Google Cloud Platform.
📐

Syntax

The cloudbuild.yaml file defines the steps Cloud Build will run. Each step uses a Docker image and commands to build or deploy your app. Use gcloud builds submit to upload your source and start the build.

  • steps: List of build steps.
  • name: Docker image to run.
  • args: Commands to execute.
  • images: Optional, images to push after build.
yaml
steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy']
💻

Example

This example deploys an App Engine app using Cloud Build. It runs the gcloud app deploy command inside a Cloud Build step.

yaml
steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy', '--quiet']
Output
Starting Step #0 Step #0: Already have image (with digest): gcr.io/cloud-builders/gcloud Step #0: Services to deploy: Step #0: descriptor: app.yaml Step #0: Beginning deployment... Step #0: Deployed service [default] to [https://PROJECT_ID.REGION_ID.r.appspot.com] Step #0: Step #0: You can stream logs from the command line by running: Step #0: gcloud app logs tail -s default Finished Step #0 BUILD SUCCESS
⚠️

Common Pitfalls

Common mistakes include missing the --quiet flag which causes builds to wait for user input, forgetting to specify the correct Docker image, or not having proper permissions set for Cloud Build to deploy.

Also, ensure your cloudbuild.yaml file is in the root of your source directory when running gcloud builds submit.

yaml
steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy']

# Wrong: Missing --quiet causes build to hang waiting for confirmation

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy', '--quiet']

# Right: --quiet flag automates deployment without prompts
📊

Quick Reference

  • cloudbuild.yaml: Defines build and deploy steps.
  • gcloud builds submit: Uploads source and triggers build.
  • --quiet: Avoids interactive prompts during deploy.
  • Permissions: Cloud Build service account needs deploy rights.

Key Takeaways

Create a cloudbuild.yaml file with deployment steps to automate deployment.
Use gcloud builds submit to start the build and deployment process.
Add the --quiet flag to avoid manual confirmation during deployment.
Ensure Cloud Build has the right permissions to deploy your app.
Keep cloudbuild.yaml in your source root for successful builds.