docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.28k stars 548 forks source link

GHA cache gets overridden when building multiple images in a single workflow #867

Closed MartyWind closed 1 year ago

MartyWind commented 1 year ago

Behaviour

I build multiple parts of my application in separate jobs in a github action workflow. They all look like this, one for frontend one for worker and backend respectively:

  frontend:
    runs-on: ubuntu-latest
      [...]
      - uses: docker/build-push-action@v4.0.0
        with:
          context: frontend/
          file: frontend/.docker/Dockerfile
          push: ${{ inputs.push }}
          tags: |
            ${{ env.FRONTEND_IMAGE_NAME }}:latest
            ${{ env.FRONTEND_IMAGE_NAME }}:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

The problem is, that only the job that finished last in the previous run gets a cache hit. All other images are getting built fully. On the next run this changes obviously, as the freshly build image finished last.

I tried to specify a ref like this (different for all images):

          cache-from: type=gha,ref=frontend-${{ github.ref_name }}
          cache-to: type=gha,mode=max,ref=frontend-${{ github.ref_name }}

But this seems to get ignored, as the behavior is unchanged.

Steps to reproduce this issue

  1. Build multiple images in a single workflow
  2. Cache them (cache-from and cache-to) with type=gha

Expected behaviour

All images should hit their respective caches.

Actual behaviour

Only the lastly built image will get a cache hit.

Attachments

Nothing changed between these runs: 1. grafik

2. grafik

3. grafik

crazy-max commented 1 year ago

type=gha,ref=frontend-${{ github.ref_name }}

There's no such attribute called ref for gha cache exporter. Guess what you want is scope. See https://docs.docker.com/build/cache/backends/gha/ for more info.