Sub topic 12
Choosing between registry, gha, and S3
This chapter assumes you have used --cache-from and --cache-to with a docker-container builder. It also assumes you know the difference between mode=min and mode=max, and that you have seen the three backends configured. The goal here is to pick one for a given project.
What differs between the backends
All three backends export the same BuildKit cache. They differ in where it lives, who can reach it, and what limits apply.
| Factor | registry | gha | s3 |
|---|---|---|---|
| Runs where | Any CI system or laptop | GitHub Actions runners only | Any runner with AWS credentials |
| Extra infrastructure | A registry you already use | None | A bucket and an IAM role |
| Size limit | Your registry's quota | 10 GB per repository by default, with least recently used eviction | None, you pay for storage |
| Expiry | Manual, or registry retention rules | Entries unused for 7 days are removed | Bucket lifecycle rules |
| Cost | Registry storage and transfer | Included with GitHub | Storage, requests, and data transfer |
| Access control | Registry push and pull rights | Workflow permissions and branch rules | IAM policy |
The gha limit and expiry values are GitHub's defaults at the time of writing. Check your plan before relying on them.
Reading the trade-offs
Registry is the most portable choice. Developers can import the same cache on a laptop, and you can move between CI providers without changing the backend. Its cost is that every build pushes cache layers to a registry that may also hold your release images. Use a dedicated tag such as buildcache to keep them apart. Some registries need image-manifest=true and oci-mediatypes=true on --cache-to, as in the earlier example.
gha is the least work to set up. You add two lines to build-push-action and no credentials are involved. The limits matter more than the setup. With mode=max, a large image can fill the 10 GB budget by itself. Eviction then removes caches from other workflows in the same repository, including ones that were still useful. It also cannot be used outside GitHub Actions.
S3 removes the size ceiling and works from any CI system. The costs are an IAM role to maintain and the S3 bill. Keep runners and the bucket in the same AWS region. Data transfer and latency then stay low, and import speed is usually good.
A decision path
Two practical rules follow from it:
- Start with
ghaif you build only on GitHub Actions and the cache is small. Move to another backend when eviction starts causing misses. - Choose
registrywhen people outside CI need the cache, or when you want no new cloud resources.
Switching is a small change
The Dockerfile and the rest of the build command stay the same when you change backends. Only the cache flags change.
# registry
--cache-from type=registry,ref=registry.example.com/myapp:buildcache
--cache-to type=registry,ref=registry.example.com/myapp:buildcache,mode=max
# gha
--cache-from type=gha,scope=myapp
--cache-to type=gha,scope=myapp,mode=max
# s3
--cache-from type=s3,region=us-east-1,bucket=my-build-cache,name=myapp
--cache-to type=s3,region=us-east-1,bucket=my-build-cache,name=myapp,mode=maxBecause of this, you can trial a second backend on one branch and compare build times using --progress=plain and the CACHED lines.
Exercise
Your team builds on GitHub Actions and on a self-hosted Jenkins server. The image cache is about 6 GB with mode=max. Which backend fits best, and why is gha a poor choice?
Show answer
Registry or S3. gha only works on GitHub Actions runners, so Jenkins builds could not use it. Both of the other backends work from any CI system. Registry needs no new infrastructure if a registry is already in use. S3 suits teams that already have AWS and want to avoid filling the registry with cache layers.