Interview Prep/GitHub Actions/Caching, Artifacts, Runners & ARC

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.

All GitHub Actions questions Roadmap: GitHub Actions

More on Caching, Artifacts, Runners & ARC