Sub topic 11
Reusing cache between CI runs
This chapter assumes you know how Docker layers are cached and can read a GitHub Actions workflow. Both are covered in earlier chapters.
Why CI loses the cache
On your laptop, Docker keeps built layers on disk, so the next build reuses them. A hosted CI runner is a fresh virtual machine for every job. When the job ends, the layers are gone, and the next run rebuilds everything, including the npm ci step.
The fix is to export the build cache to storage that outlives the job, then import it at the start of the next run. BuildKit, the build engine behind Buildx, does this with cache backends.
The GitHub Actions cache backend
The gha backend stores cache in the GitHub Actions cache service. It needs no extra credentials or registry setup. Set it on docker/build-push-action:
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
cache-from: type=gha
cache-to: type=gha,mode=maxcache-fromlists where BuildKit looks for existing cache.cache-tosays where to write cache after the build.
The setup-buildx-action step is required. The default Docker builder cannot export cache with these backends.
Choosing a cache mode
The mode setting controls which layers are exported.
| Mode | What is exported | Typical use |
|---|---|---|
| `min` (default) | Layers of the final image only | Single-stage Dockerfiles |
| `max` | Layers of all stages, including intermediate ones | Multi-stage Dockerfiles |
With a multi-stage Dockerfile, the expensive npm ci and npm run build steps live in the build stage. That stage is not part of the final image. With min, those layers are never exported, so the next run rebuilds them. Use mode=max for multi-stage builds.
Other cache backends
| Backend | Stored in | Notes |
|---|---|---|
| `gha` | GitHub Actions cache | Easiest on GitHub. Total size is limited per repository, and old entries are evicted. |
| `registry` | A tag in a container registry | Works on any CI system. Shared by all runners that can reach the registry. |
| `inline` | Inside the pushed image | Only exports the final image's layers, like `min`. |
| `local` | A directory | Pair it with your CI's own directory caching. |
A registry cache looks like this:
cache-from: type=registry,ref=ghcr.io/example-org/example-app:buildcache
cache-to: type=registry,ref=ghcr.io/example-org/example-app:buildcache,mode=maxThe cache is stored under its own tag, here buildcache. Keep it separate from your release tags so nobody deploys it by mistake.
Common errors
- No speedup at all. A step early in the Dockerfile changes on every run, such as
COPY . .beforenpm ci. That invalidates every later layer. Copy the manifests first. - Cache never hits on multi-stage builds. You used
mode=min. Switch tomax. - Cache export fails on pull requests from forks. Those runs have a read-only token. Registry caches need write access, so skip
cache-tothere. - Cache is stale or huge.
ghaevicts entries once the repository limit is reached. Frequent branch builds can push out themaincache.
Exercise
Your multi-stage Dockerfile builds in 30 seconds locally with a warm cache, but takes 3 minutes in CI on every run, even though cache-from: type=gha and cache-to: type=gha are set. What is the most likely fix?
Show answer
Set mode=max on cache-to (type=gha,mode=max). The default min mode only exports layers of the final stage, so the dependency install and build steps in the build stage are never cached.