Uploading a Docker image to GCR using Github Actions

Yannick Visbeek
MisterGreen Engineering
2 min readJun 2, 2020

At MisterGreen, we’re in the middle of moving all of our repositories from Bitbucket to Github. But, since we’re now using Bitbucket Pipelines to deploy our applications, we also need a new CI/CD tool. As our code will be hosted on Github, we chose Github Actions for this. If you’re using Docker to ship your code and you’re using a Google Cloud Platform service like Cloud Run, you might want to use the Google Container Registry to manage your container images. We are using the following workflow to upload a container image to GCR.

First, you should have:

  • a Google Cloud Project and a service account key
  • a Github repository
  • the id of the Google Cloud project, name of the service and Google Cloud project service account json set up as Github secrets
  • a Dockerfile that builds an image of your application

Below you can find the Build.yml file which should be in the .github/workflows directory of your repository.

The workflow gets triggered on every push, runs on Ubuntu and defines some variables that will be used more than once throughout the workflow. The following initialises the Google Cloud CLI:

- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
with:
service_account_key: ${{ secrets.GCR_DEVOPS_SERVICE_ACCOUNT_KEY }}
project_id: ${{ secrets.PROJECT_ID }}
export_default_credentials: true

Then, the docker image gets built:

- name: Image
run: |-
docker build --build-arg NPM_TOKEN=${{ secrets.NPM_TOKEN }} -t eu.gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA .

After that, the docker credential helper is added to the Docker configuration file, which will register gcloud as the credential helper for all Google-supported Docker registries and the image is pushed to the Google Container Registry.

# Configure docker to use the gcloud command-line tool as a credential helper
- run: |
gcloud auth configure-docker -q

# Push image to Google Container Registry
- name: Build
run: |-
docker push eu.gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA

It’s as easy as that! Once the image is in the Google Container Registry it’s easy to deploy the image to Cloud Run using the interface or using a deploy script, such as the one we are using at MisterGreen (check it out!).

--

--