Sub topic 7
The GitHub Actions backend
This chapter assumes you can write a GitHub Actions workflow, know what docker buildx is, and have read the earlier chapters on cache-from, cache-to, and builder drivers. It covers the gha cache backend: how it stores cache, how to configure it, and the errors people hit most often.
What the backend does
The gha backend stores BuildKit cache in the GitHub Actions cache service. You don't need a registry or a cloud account. The runner already has the credentials, and docker/build-push-action passes them to BuildKit.
Because it talks to that service directly, it needs a builder that supports cache export. Use docker/setup-buildx-action, which creates a docker-container builder. The default docker driver cannot export to gha.
A working workflow
name: build
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: myapp:ci
cache-from: type=gha,scope=myapp
cache-to: type=gha,scope=myapp,mode=maxThe workflow does three things:
setup-buildx-actioncreates thedocker-containerbuilder.cache-fromimports cache entries stored under the scopemyapp.cache-toexports the cache after the build.mode=maxexports layers from all stages, not only the final image, so multi-stage builds get more hits.
How scopes work
A scope is a label for a set of cache entries. Two builds with the same scope read and write the same cache. Two builds with different scopes are isolated.
GitHub also limits cache access by branch. A workflow run can read caches created on its own branch, on the base branch, and on the default branch. It cannot read caches from sibling branches. So a pull request can reuse the cache that main wrote, but its own writes stay invisible to other pull requests.
flowchart TDThe diagram below shows this lookup order.
Choosing scope names
Use a distinct scope for each image when one repository builds several. If two jobs share a scope, they overwrite each other's entries, and both lose hits.
- uses: docker/build-push-action@v6
with:
context: ./api
cache-from: type=gha,scope=api
cache-to: type=gha,scope=api,mode=max- uses: docker/build-push-action@v6
with:
context: ./worker
cache-from: type=gha,scope=worker
cache-to: type=gha,scope=worker,mode=maxA matrix build can use the matrix value in the scope, for example scope=app-${{ matrix.platform }}.
Limits and retention
The GitHub Actions cache has a size cap per repository, which is 10 GB by default. When the cap is reached, GitHub evicts the least recently used entries. Entries that go unused for seven days are also removed.
mode=max stores more data than mode=min. On a large multi-stage image it can fill the cap quickly and push out other caches. If you see hits disappear over time, check the repository's cache usage under Actions, then Caches. Consider mode=min, or fewer scopes.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about the cache exporter not being supported | The default `docker` driver is in use | Add `docker/setup-buildx-action` before the build |
| Every build is a full miss | Scope differs between `cache-from` and `cache-to` | Use the same `scope` value in both |
| Hits on `main`, none on a pull request | Cache was written on an unrelated branch | Make sure `main` builds and writes cache |
| Hits vanish after a few days | Eviction by size or age | Check cache usage and reduce `mode=max` footprint |
Exercise
Two jobs in one repository build different images, but both use cache-to: type=gha,scope=myapp,mode=max. Builds keep missing the cache. What is wrong and how do you fix it?
Show answer
Both jobs write to the same scope, so each overwrites the other's cache entries. Give each image its own scope, such as scope=api and scope=worker, and use the matching value in cache-from and cache-to.