Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stored cache is empty #33

Open
sylver opened this issue Jun 2, 2024 · 5 comments
Open

Stored cache is empty #33

sylver opened this issue Jun 2, 2024 · 5 comments

Comments

@sylver
Copy link

sylver commented Jun 2, 2024

I am probably missing something, but doing the same as the example results in an empty cache :

      - name: Cache apk
        uses: actions/cache@v4
        id: cache-apk
        with:
          path: |
            var-cache-apk
          key: ${{ runner.os }}-apk-cache-${{ hashFiles('Dockerfile') }}
          save-always: true
          restore-keys: |
            ${{ runner.os }}-apk-cache-

      - name: Inject apk cache into Docker
        uses: reproducible-containers/buildkit-cache-dance@v3.1.1
        with:
          cache-map: |
            {
              "var-cache-apk": {
                "target": "/var/cache/apk",
                "id": "apk-cache"
              }
            }
          save-always: true
          skip-extraction: ${{ steps.cache-apk.outputs.cache-hit }}
Screenshot 2024-06-02 at 18 18 38

Dockerfile snippet using this cache :

RUN --mount=type=cache,id=apk-cache,target=/var/cache/apk \
<<EOT
set -e

echo "@edge http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories

apk update
apk upgrade
apk add ${PACKAGES}
EOT

I have the same issue with other caches I tried (pnpm store, build dist folders, etc), so that's not related specifically to this path but to the way I implement the action (according to the documentation).

@yunhaoww
Copy link

same here.

@rkarp
Copy link

rkarp commented Aug 13, 2024

I cannot reproduce this, but there are a few reasons why this can happen:

  • buildkit-cache-dance may use a different buildx builder than your actual Docker build.
  • Options to --mount=type=cache are different. Using a different id, uid, gid or mode will result in getting a new empty cache. This doesn't seem to be the case here, though.

@mrfelton
Copy link

mrfelton commented Aug 31, 2024

I'm also experiencing this issue. Some relevant code...

Relevant parts of GitHub Actions Workflow:

      - run: docker context create builders

      - uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # pin@v3.3.0
        with:
          endpoint: builders

      - name: Ensure cache directories exists
        run: |
          mkdir -p ~/.nuget/packages
          mkdir -p ~/dist

      - name: 'Cache intermediate build artifacts'
        id: cache
        uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # pin@v4
        with:
          path: |
            ~/.nuget/packages
            ~/dist
          key: ${{ runner.os }}-intermediate-${{ matrix.project }}-${{ hashFiles('src/**/Directory.Packages.props') }}

      - name: Show cache contents
        run: |
          ls -al ~/.nuget/packages
          ls -al ~/dist

      - name: inject cache into docker build
        uses: reproducible-containers/buildkit-cache-dance@5b6db76d1da5c8b307d5d2e0706d266521b710de # v3.1.2
        with:
          cache-map: |
            {
              "~/.nuget/packages": {
                "target": "/root/.nuget/packages",
                "id": "nuget"
              },
              "~/dist": {
                "target": "/dist",
                "id": "dist"
              }
            }
          skip-extraction: ${{ steps.cache.outputs.cache-hit }}

      - name: Build and push
        uses: docker/build-push-action@v6.7.0
        with:
          context: ./src
          cache-from: type=gha
          cache-to: type=gha,mode=max
          file: src/Location/ZapMiddleware.Location.Api/Dockerfile
          push: false
          tags: test

Relevant parts of Dockerfile:

###################################################
#                   Build image                   #
###################################################
FROM mcr.microsoft.com/dotnet/sdk:8.0.201-bookworm-slim AS build
WORKDIR /src

# Check cache mounts
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages ls -al /root/.nuget/packages
RUN --mount=type=cache,id=dist,target=/dist ls -al /dist

# Copy csproj and restore as distinct layers
COPY ["Location/ZapMiddleware.Location.Api/ZapMiddleware.Location.Api.csproj", "Location/ZapMiddleware.Location.Api/"]
COPY "Directory.Build.props" .
COPY "Directory.Packages.props" .
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
	--mount=type=cache,id=dist,target=/dist \
	dotnet restore "Location/ZapMiddleware.Location.Api"

# Check cache mounts
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages ls -al /root/.nuget/packages
RUN --mount=type=cache,id=dist,target=/dist ls -al /dist/intermediates/src

# Copy everything else and build app
COPY . .
WORKDIR "/src/Location/ZapMiddleware.Location.Api"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
	--mount=type=cache,id=dist,target=/dist \
	dotnet publish -c Release -o /app
RUN rm /app/appsettings*.json

# Check cache mounts
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages ls -al /root/.nuget/packages
RUN --mount=type=cache,id=dist,target=/dist ls -al /dist/intermediates/src
image

Relevant parts of docker build logs: The cache directories are populated with the expected package and build stage artefacts

...
2024-08-31T08:17:01.3530775Z #14 [build  2/19] WORKDIR /src
2024-08-31T08:17:01.3532385Z #14 extracting sha256:c7c43fea98428ca37da3bb3c9e267aba534255e5e587f604811e51ff3adf99a6 0.0s done
2024-08-31T08:17:01.3533141Z #14 DONE 10.6s
2024-08-31T08:17:01.3534565Z #15 [build  3/19] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages ls -al /root/.nuget/packages
2024-08-31T08:17:01.3535742Z #15 0.063 total 12
2024-08-31T08:17:01.3536341Z #15 0.063 drwxr-xr-x 2 root root 4096 Aug 31 08:16 .
2024-08-31T08:17:01.3537024Z #15 0.063 drwxr-xr-x 1 root root 4096 Aug 31 08:17 ..
2024-08-31T08:17:01.3537760Z #15 0.063 -rw-r--r-- 1 root root   24 Aug 31 08:16 buildstamp
2024-08-31T08:17:08.1395103Z #15 DONE 7.0s
2024-08-31T08:17:08.2698944Z #16 [build  4/19] RUN --mount=type=cache,id=dist,target=/dist ls -al /dist
2024-08-31T08:17:08.2700519Z #16 0.070 total 12
2024-08-31T08:17:08.2701584Z #16 0.070 drwxr-xr-x 2 root root 4096 Aug 31 08:16 .
2024-08-31T08:17:08.2702859Z #16 0.070 drwxr-xr-x 1 root root 4096 Aug 31 08:17 ..
2024-08-31T08:17:08.2704233Z #16 0.070 -rw-r--r-- 1 root root   24 Aug 31 08:16 buildstamp
2024-08-31T08:17:08.2705144Z #16 DONE 0.1s
2024-08-31T08:17:08.2706732Z #17 [build  5/19] COPY [Location/ZapMiddleware.Location.Api/ZapMiddleware.Location.Api.csproj, Location/ZapMiddleware.Location.Api/]
2024-08-31T08:17:08.2708241Z #17 DONE 0.0s
2024-08-31T08:17:08.5006252Z #18 [build  6/19] COPY Directory.Build.props .
2024-08-31T08:17:08.5007137Z #18 DONE 0.0s
2024-08-31T08:17:08.5007706Z #19 [build  7/19] COPY Directory.Packages.props .
2024-08-31T08:17:08.5008310Z #19 DONE 0.0s
2024-08-31T08:17:08.5010318Z #20 [build  8/19] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages 	--mount=type=cache,id=dist,target=/dist 	dotnet restore "Location/ZapMiddleware.Location.Api"
2024-08-31T08:17:09.1028290Z #20 0.753   Determining projects to restore...
...
2024-08-31T08:17:14.7344732Z #20 6.384   Restored /src/Location/ZapMiddleware.Location.Api/ZapMiddleware.Location.Api.csproj (in 5.24 sec).
2024-08-31T08:17:15.4331872Z #20 DONE 7.1s
2024-08-31T08:17:15.5401299Z #21 [build  9/19] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages ls -al /root/.nuget/packages
2024-08-31T08:17:15.5402742Z #21 0.076 total 164
2024-08-31T08:17:15.5403791Z #21 0.076 drwxr-xr-x 40 root root 4096 Aug 31 08:17 .
2024-08-31T08:17:15.5405044Z #21 0.076 drwxr-xr-x  1 root root 4096 Aug 31 08:17 ..
2024-08-31T08:17:15.5406376Z #21 0.076 -rw-r--r--  1 root root   24 Aug 31 08:16 buildstamp
2024-08-31T08:17:15.5407828Z #21 0.076 drwxr-xr-x  3 root root 4096 Aug 31 08:17 humanizer.core
2024-08-31T08:17:15.5409709Z #21 0.076 drwxr-xr-x  3 root root 4096 Aug 31 08:17 microsoft.bcl.asyncinterfaces
...
2024-08-31T08:17:15.5459341Z #21 DONE 0.1s
2024-08-31T08:17:15.7726542Z #22 [build 10/19] RUN --mount=type=cache,id=dist,target=/dist ls -al /dist/intermediates/src
2024-08-31T08:17:15.7727581Z #22 0.067 total 12
2024-08-31T08:17:15.7728241Z #22 0.067 drwxr-xr-x 3 root root 4096 Aug 31 08:17 .
2024-08-31T08:17:15.7728995Z #22 0.067 drwxr-xr-x 3 root root 4096 Aug 31 08:17 ..
2024-08-31T08:17:15.7730578Z #22 0.067 drwxr-xr-x 3 root root 4096 Aug 31 08:17 Location
2024-08-31T08:17:15.7731171Z #22 DONE 0.1s
2024-08-31T08:17:15.7731589Z #23 [build 11/19] COPY . .
2024-08-31T08:17:19.8166214Z #23 DONE 4.2s
2024-08-31T08:17:19.9693387Z #24 [build 12/19] WORKDIR /src/Location/ZapMiddleware.Location.Api
2024-08-31T08:17:20.0403629Z #24 DONE 0.2s
2024-08-31T08:17:20.1930552Z #25 [build 13/19] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages 	--mount=type=cache,id=dist,target=/dist 	dotnet publish -c Release -o /app
2024-08-31T08:17:20.2861940Z #25 0.243 MSBuild version 17.9.4+90725d08d for .NET
2024-08-31T08:17:20.8896105Z #25 0.847   Determining projects to restore...
...
2024-08-31T08:18:07.1248271Z #25 DONE 47.1s
2024-08-31T08:18:07.3202472Z #26 [build 14/19] RUN rm /app/appsettings*.json
2024-08-31T08:18:07.3205622Z #26 DONE 0.1s
2024-08-31T08:18:07.3208466Z #27 [build 15/19] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages ls -al /root/.nuget/packages
2024-08-31T08:18:07.3209947Z #27 0.098 total 1128
2024-08-31T08:18:07.3210553Z #27 0.098 drwxr-xr-x 277 root root 20480 Aug 31 08:17 .
2024-08-31T08:18:07.3211282Z #27 0.098 drwxr-xr-x   1 root root  4096 Aug 31 08:18 ..
...
2024-08-31T08:18:07.3214067Z #27 0.098 drwxr-xr-x   3 root root  4096 Aug 31 08:17 asp.versioning.mvc
2024-08-31T08:18:07.3215010Z #27 0.098 drwxr-xr-x   3 root root  4096 Aug 31 08:17 asp.versioning.mvc.apiexplorer
2024-08-31T08:18:07.3215881Z #27 0.098 -rw-r--r--   1 root root    24 Aug 31 08:16 buildstamp
2024-08-31T08:18:07.3216753Z #27 0.098 drwxr-xr-x   3 root root  4096 Aug 31 08:17 communitytoolkit.diagnostics
...
2024-08-31T08:18:07.4769263Z #27 DONE 0.1s
2024-08-31T08:18:07.4770284Z #28 [build 16/19] RUN --mount=type=cache,id=dist,target=/dist ls -al /dist/intermediates/src
2024-08-31T08:18:07.4771487Z #28 0.088 total 28
2024-08-31T08:18:07.4772414Z #28 0.088 drwxr-xr-x  7 root root 4096 Aug 31 08:17 .
2024-08-31T08:18:07.4773753Z #28 0.088 drwxr-xr-x  3 root root 4096 Aug 31 08:17 ..
2024-08-31T08:18:07.4774639Z #28 0.088 drwxr-xr-x  6 root root 4096 Aug 31 08:17 Location
2024-08-31T08:18:07.4775681Z #28 0.088 drwxr-xr-x 23 root root 4096 Aug 31 08:17 Shared
2024-08-31T08:18:07.4776973Z #28 0.088 drwxr-xr-x  3 root root 4096 Aug 31 08:17 ZapMiddleware.AppCommon.Queues
2024-08-31T08:18:07.4778776Z #28 0.088 drwxr-xr-x  3 root root 4096 Aug 31 08:17 ZapMiddleware.Intl
2024-08-31T08:18:07.4780501Z #28 0.088 drwxr-xr-x  3 root root 4096 Aug 31 08:17 ZapMiddleware.Util
2024-08-31T08:18:07.5510474Z #28 DONE 0.1s

Extracting cache: Extraction seems to happen, but the resulting cache that gets uploaded to GHA is empty

image

@mrfelton
Copy link

mrfelton commented Sep 2, 2024

I resolved my issue by ensuring that the local directory doesn't exist before running the cache dance, and changing the path to something relative to the checkout.

@kynefuk
Copy link

kynefuk commented Sep 25, 2024

I'm also experiencing same issue.
We daily create cache that is the packages installed by bundle install.
About once every 3-4 days, the mount-cache cannot be extracted by buildkit-cache-dance/extract and so cache is saved as empty cache.

Relevant parts of GitHub Actions Workflow:

jobs:
  create-bundle-cache:
    timeout-minutes: 45
    runs-on:
      labels: Ubuntu-8core
    steps:
      - uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: docker-build-push-action
        uses: docker/build-push-action@v5.3.0
        with:
          context: .
          file: Dockerfile
          platforms: linux/amd64
          target: production
          no-cache: true
          push: false
          provenance: false
          tags: |
            bundle-cache-main
      - name: mkdir for buildkit-cache-dance/extract
        run: |
          mkdir -p var-cache-bundle scratch
      - name: extract var-cache-bundle from build cache
        uses: reproducible-containers/buildkit-cache-dance/extract@v1.0.1
        with:
          cache-source: 'var-cache-bundle'
          cache-target: '/var/cache/bundle'
          scratch-dir: scratch

      # save local mount-cache to gha
      - name: Cache var-cache-bundle
        id: var-cache-bundle
        uses: actions/cache/save@v4
        with:
          path: var-cache-bundle
          key: var-cache-bundle-main-${{ github.run_id }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants