How do you cache Docker layers in GitHub Actions?
IntermediateConceptCaching, Artifacts, Runners & ARC
Answer
Use BuildKit's GitHub cache backend through docker/build-push-action:
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE }}:sha-${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
mode=max caches intermediate stages too (important for multi-stage builds). It counts toward the repo's cache quota; for big images or self-hosted runners, type=registry,ref=<repo>:buildcache is often better. Use a scope per image in monorepos so images do not evict each other.
More on Caching, Artifacts, Runners & ARC
How does `actions/cache` match keys and restore keys?IntermediateWhich caches can a branch restore from?AdvancedWhat are the cache size limits and eviction rules?BeginnerWhen should you use setup-* built-in caching instead of `actions/cache`?BeginnerWhy cache the package manager's download cache rather than `node_modules`?AdvancedHow do `actions/cache/restore` and `actions/cache/save` help?Advanced