Using of Github Actions Caches for Docker Builds
Short definition of GHA:
GitHub Actions(GHA) is a cloud-based continuous integration and deployment (CI/CD) service provided by GitHub. It allows developers to automate workflows, such as building, testing, and deploying code, directly from their GitHub repositories. Using GitHub Actions can help developers automate and streamline their software development process, reducing the amount of time spent on repetitive tasks and improving the overall efficiency of the development cycle.
I believe that all readers of this article are aware what is docker. But if now please start immediately => https://docs.docker.com/get-started/
So, for those who stayed. Let's imagine, that' s you are definitely not interesting to watch out for long docker builds and wait of the end of it. Fortunately, Github Actions can help us to reduce this waste of time with help of "Caches" option.
So, without wasting of your time, let's dive in practice. It will be special case for using Github Actions Cache and save this cache directly in GHA.
But what about official documentation - yes, it exists (here - https://docs.docker.com/build/cache/backends/gha/), but it very short and you need to went further of it in understanding by yourself.
The easiest way to start using gha caches for docker builds is docker/build-push-action@v4. More info here: https://github.com/marketplace/actions/build-and-push-docker-images
Imagine that's we have github action workflow with next global environment variables:
env:
REGISTRY: "my-registry"
IMAGE_NAME: "my-app"
ARG_1: "FIRST-DOCKER-BUILD-ARGUMENT"
ARG_2: "SECOND-DOCKER-BUILD-ARGUMENT"
ARG_3: "THIRD-DOCKER-BUILD-ARGUMENT"
This article also guess that you are aware how to write Dockerfiles, we won't stop on it because Internet is full of examples.
First of all we need to create cache storage backend, which requires using a different driver than the default?docker?driver. Let's do it in separate gh action:
领英推荐
- name: Create Docker Cacha Storage Backend
? run: |
? docker buildx create --use --driver=docker-container
Further. After the creation of cache storage backend we can start use it.
- name: Build docker
? uses: docker/build-push-action@v4
? with:
? context: .
? ? load: true
? ? build-args: |
? ? ? "ARG_1=${{ env.ARG_1 }}"
? ? ? "ARG_2=${{ env.ARG_2 }}"
? ? ? "ARG_3=${{ env.ARG_3 }}"
? ? tags: |
? ? ? "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}"
? ? push: false
? ? cache-from: type=gha
? ? cache-to: type=gha,mode=max
If you use to create from time to time gha workflow, you automatically use yaml/json format for it, but the main thing in this case that you need to imagine that's you write simple docker build command for docker build:
docker build \
--build-arg ARG_1=$ARG_1 \
--build-arg ARG_2=$ARG_2 \
--build-arg ARG_3=$ARG_3 \
-t $REGISTRY/$IMAGE_NAME:$GITHUB_SHA .
"push" option is used for after-build pushing to registry. If true - pushing to registry If false - image can be used for example for vulnerabilities scanning and following pushing. But in case of false you need to be able to manipulate with ready docker image after the build. For this purpose is used "load: true" option.
"--cache-from" option defines source of cache for following builds. "--cache-to" option define cache storage where we save cache after the build
After the build you will have chache for your future builds.
Also you will be able to find some "CACHED" steps during the build.
You will be absolutely right to say that GHA docker cache is more than content of this article, but i also want to leave it for your own experiments. See you soon!