GitHub Action — Build, Push Docker Image to Artifact Registry

Jack P
3 min readNov 9, 2024

--

Photo by Luca Bravo on Unsplash

This blogpost is going to introduce a public GitHub action I wrote that builds and pushes a Docker Image to Google Cloud’s Artifact Registry. The custom action is located here on the GitHub Action Marketplace.

What is Artifact Registry?

In terms of Docker, Google Cloud Artifact Registry allows you to securely store and manage Docker container images, similar to Docker Hub or other container registries but with tight integration into GCP’s ecosystem. If you are developing in Google Cloud with Docker images/containers, then you will most likely want to utilize Google Cloud Artifact Registry.

Walkthrough of the GitHub Action Steps

The GitHub Action YAML is located here.

Next it logs into the Artifact Registry by using the docker/login-action action. This step ensures that we are able to push images to our inputted Artifact Registry repository. In order to sign in, we will utilize the input, gcp_credentials_json, which is the Service Account JSON Key for the Service Account pushing your Docker image.

This input, gcp_credentials_json, should be stored as a GitHub secret and passed through as a GitHub secret.

Next it utilizes the action, docker/setup-buildx-action. This action is a GitHub Action that sets up Docker Buildx in your GitHub Actions workflow. This has a lot of advanced features, and one of those features that is worth mentioning is that it improves build performance by using build cache more efficiently.

Note: This will utilize GitHub Action build cache, you may want to have a CRON GitHub action auto delete cache every so often. Here is a gist to get you started with cache handling.

The Inputs

  • gcp_credentials_json — Required, GCP Service Account Key JSON, it is recommended to utilize GitHub secrets in passing this value through.
  • docker_target — Required, This action is build for named stages, so pass the stage you want to build into here. Read more on multi-stage Docker builds here.
  • tags — Required, This is the Docker Image namewith tag you want to use. You will want this to be prefixed with the Artifact repository that you are pushing to, and end with a tag.
  • build_args — Not Required, pass any build arguments that you want passed in during the build here.

An Example of Usage

jobs:
deploy-to-artifact-registry:
environment: production
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: build_push_docker_artifact_registry
uses: japerry911/build-push-docker-gcp-artifact-registry-action@v2.2
with:
gcp_credentials_json: ${{ secrets.GCP_CREDENTIALS }}
docker_target: my_stage_that_I_build
tags: us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/my-repo/my-image:latest
build_args: |
BUILD_ARG_1=value_1
BUILD_ARG_2=value_2

In this example, I am pushing my Dockerfile, my_stage_that_I_build stage, to my GCP Artifact Registry repository, my-repo , with the image name my-image , and the tag latest .

Conclusion

In this blogpost, I introduced a custom GitHub Action that I developed to help build and push Docker images to Google Cloud’s Artifact Registry.

Happy Coding!

Photo by Daniel Eledut on Unsplash

--

--

Jack P
Jack P

Written by Jack P

Data Engineer | Software Engineer

Responses (1)