Setting Up Automated Container Builds with GCP Cloud Build

0
222

Continuous Deployment and Integration is the process of automatically running tests and builds whenever source changes are pushed. You can set up Google Cloud Platform (GCP) to run automated Docker builds whenever you make updates to your container, and deploy to Cloud Run or Google Kubernetes Engine (GKE).

Overall, the configuration for this is fairly easy—Dockerfiles already handle most of the build configuration, so all that’s required from you is linking a few services together, and configuring Cloud Build to deploy updates to Cloud Run or GKE.

Create a Cloud Source Repository

Cloud Build pulls code from Cloud Source, Google’s own managed Git solution. You won’t have to transition from your current provider though, as you can simply link your GitHub or Bitbucket account, or just set it up as a separate release remote.

Head over to the Cloud Source console, and create a new repo. Give it a name, and sign in with your account to select a repository.

If you’re adding it as a separate remote, you can add the repo with the given git remote add command, which will make it available under the remote name “google.” To push to this branch, all you have to do is fully qualify the remote and branch name when running git push, replacing the default origin with google:

git push –all google

Setting Up Cloud Build

Head over to the Cloud Build Console, and set up a new build trigger:

This trigger will run in response to events in the source repository. Give it a name, and select whether you want to build in response to commits to a specific branch, or when new tags are pushed, or when pull requests are made to GitHub.

Below that, select “Dockerfile” as the build configuration, rather than Google’s own YAML config. This is the benefit of working with containers; you’ve already handled the build process in the Docker config, so you can simply select it here.

If your Dockerfile is named “Dockerfile” and placed at the root of the repository, you can leave the first two fields blank. For the image name, it will be pushed to GCR, Google’s own container registry.

gcr.io/$PROJECT_ID/imagename:$COMMIT_SHA

Once created, you can click “Run Trigger” to manually start the build. Because Cloud Build just uses your Dockerfile, as long as it builds locally, you shouldn’t have any issues with the build.

If you head over to the GCR console, you should see the newly built image.

Configuring Automatic Deployments to Cloud Run

You can manually update your deployments, but if you want do automatic updates, you’ll need a bit more configuration.

Instead of choosing just the Dockerfile, you can choose to use a traditional YAML file, which will allow you to run some commands after the build process. In this case, running gcloud run deploy and passing it a ref to the newly created image in GCR.

steps:
# build the container image
– name: ‘gcr.io/cloud-builders/docker’
args: [‘build’, ‘-t’, ‘gcr.io/$PROJECT_ID/[SERVICE-NAME]:$COMMIT_SHA’, ‘.’]
# push the container image to Container Registry
– name: ‘gcr.io/cloud-builders/docker’
args: [‘push’, ‘gcr.io/$PROJECT_ID/[SERVICE-NAME]:$COMMIT_SHA’]
# Deploy container image to Cloud Run
– name: ‘gcr.io/cloud-builders/gcloud’
args:
– ‘run’
– ‘deploy’
– ‘[SERVICE-NAME]’
– ‘–image’
– ‘gcr.io/$PROJECT_ID/[SERVICE-NAME]:$COMMIT_SHA’
– ‘–region’
– ‘[REGION]’
– ‘–platform’
– ‘managed’
images:
– ‘gcr.io/$PROJECT_ID/[SERVICE-NAME]:$COMMIT_SHA’

You’ll also need to give Cloud Build permission to access Cloud Run from the service account settings.