reproducible-containers / buildkit-cache-dance

Save `RUN --mount=type=cache` caches on GitHub Actions ( Forked from https://github.com/overmindtech/buildkit-cache-dance )
Apache License 2.0
143 stars 29 forks source link

Having separate inject steps causes caches to be overwritten #39

Open Redmega opened 2 months ago

Redmega commented 2 months ago

I have two separate caches (one for dependencies, and one for next build caches within a monorepo) that I want to use in my docker build.

The problem is the cache-dance action seems to use the same source folder when extracting the caches. When I added my next build cache to the workflow, it seems that now it overwrites the mounted directed from the first inject step and now the yarn dependencies do not get detected by the docker build.

If I combine all my caches together I'll lose the ability to skip extraction on the dependencies which change much less frequently. I will waste time extracting my yarn cache despite it not changing.

      - name: Set up Yarn build cache
        id: yarn-cache
        uses: actions/cache@v4
        with:
          path: yarn-build-cache
          key: ${{ matrix.platform }}-yarn-${{ hashFiles('yarn.lock') }}
          restore-keys: |
            ${{ matrix.platform }}-yarn-
      - name: Set up Next build cache
        id: next-cache
        uses: actions/cache@v4
        with:
          path: |
            next-build-cache
            nx-build-cache
          key: ${{ matrix.platform }}-next-${{ matrix.app.name }}-${{ hashFiles('yarn.lock') }}-${{ hashFiles(format('apps/{0}/**', matrix.app.name)) }}
          restore-keys: |
            ${{ matrix.platform }}-next-${{ matrix.app.name }}-${{ hashFiles('yarn.lock') }}-
      - name: Inject caches into Docker
        uses: reproducible-containers/buildkit-cache-dance@v3
        with:
          cache-map: |
            {
              "yarn-build-cache": "/fe/.yarn/cache"
            }
          skip-extraction: ${{ steps.yarn-cache.outputs.cache-hit }}
      - name: Inject next cache into Docker
        uses: reproducible-containers/buildkit-cache-dance@v3
        with:
          cache-map: |
            {
              "next-build-cache": "/fe/apps/${{ matrix.app.name }}/.next/cache",
              "nx-build-cache": "/fe/.nx"
            }
          skip-extraction: ${{ steps.next-cache.outputs.cache-hit }}
insanitybit commented 1 week ago

I think I'm seeing the same behavior, but unsure. Regardless, it seems that just changing the cache-dance directory such that it's configurable would let this be fixed, or having it be something like /var/hash(paths).