0
0
GCPcloud~5 mins

Cloud Build for CI/CD in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Cloud Build helps you automatically build and test your code when you make changes. It solves the problem of manually running tests and deployments by automating these steps in a safe and repeatable way.
When you want to automatically test your code every time you save changes.
When you want to build and deploy your app without doing it by hand.
When you want to make sure your app works the same way every time you update it.
When you want to share your build process with your team so everyone uses the same steps.
When you want to connect your code repository to a cloud service that runs your builds.
Config File - cloudbuild.yaml
cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/my-project/my-app', '.']
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/my-project/my-app']
images:
- 'gcr.io/my-project/my-app'

This file tells Cloud Build what to do step-by-step.

steps: Each step runs a container to do a task. Here, the first step builds a Docker image from your app code. The second step pushes that image to Google Container Registry.

images: This lists the images that Cloud Build will create and store.

Commands
This command sends your code and the cloudbuild.yaml file to Cloud Build. It starts the build process defined in the file.
Terminal
gcloud builds submit --config cloudbuild.yaml .
Expected OutputExpected
Creating temporary tarball archive of 5 files before upload... Uploading tarball of [.] to [gs://my-project_cloudbuild/source/...]... Created [https://console.cloud.google.com/cloud-build/builds/12345678-1234-1234-1234-123456789abc?project=my-project]. Logs are available at [https://console.cloud.google.com/cloud-build/builds/12345678-1234-1234-1234-123456789abc?project=my-project].
--config - Specifies the build configuration file to use.
This command shows the last three builds that ran in your project. It helps you check if your build succeeded or failed.
Terminal
gcloud builds list --limit=3
Expected OutputExpected
ID STATUS START_TIME DURATION 12345678-1234-1234-1234-123456789abc SUCCESS 2024-06-01T12:00:00Z 2m30s 87654321-4321-4321-4321-cba987654321 FAILURE 2024-05-31T15:00:00Z 1m10s abcdef12-3456-7890-abcd-ef1234567890 SUCCESS 2024-05-30T09:00:00Z 3m5s
--limit - Limits the number of builds shown.
This command shows detailed information about a specific build, including logs and status.
Terminal
gcloud builds describe 12345678-1234-1234-1234-123456789abc
Expected OutputExpected
id: 12345678-1234-1234-1234-123456789abc status: SUCCESS createTime: '2024-06-01T12:00:00Z' steps: - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/my-project/my-app', '.'] status: SUCCESS - name: 'gcr.io/cloud-builders/docker' args: ['push', 'gcr.io/my-project/my-app'] status: SUCCESS images: - 'gcr.io/my-project/my-app'
Key Concept

If you remember nothing else from this pattern, remember: Cloud Build runs your build steps automatically so you don't have to do them by hand.

Common Mistakes
Not specifying the correct build config file with --config flag.
Cloud Build will use the default config or fail if it can't find one, causing your build to not run as expected.
Always use --config cloudbuild.yaml to point to your build steps file.
Not checking build status after submitting the build.
You might think your build succeeded but it could have failed, causing deployment issues later.
Use gcloud builds list and gcloud builds describe to verify build success.
Summary
Create a cloudbuild.yaml file to define your build steps.
Run gcloud builds submit with the config file to start the build.
Check recent builds with gcloud builds list to see their status.
Use gcloud builds describe to get detailed info about a specific build.