cache icon indicating copy to clipboard operation
cache copied to clipboard

If the target path doesn't match the source path, restore reports "Cache not found for input keys"

Open dylanbeattie opened this issue 1 year ago • 3 comments

Hey there,

I've been trying to use the save/restore actions from two separate jobs, and after six hours of head-scratching, worked out what's going on. If the path provided to the restore command doesn't exactly match the path that was provided to the save command, the restore action reports "Cache not found for input keys". Here's a repro case. Run this in any repo, then look at the action console output; the second fetch-stuff-from-cache job won't restore any files.

name: cache-test
on:
  push:
    branches: ["main"]
    paths:
      - '.github/workflows/caching-test.yml'
  workflow_dispatch:
jobs:
  cache-some-stuff:
    runs-on: ubuntu-latest
    steps:
      - name: Create some files
        run: |
          mkdir my-files
          echo "Hello" > my-files/file0001.txt
          echo "World" > my-files/file0002.txt
          ls -lR
      - uses: actions/cache/save@v4
        id: put-files-in-cache
        with:
          path: my-files
          key: my-cached-files-${{ github.run_id }}
  fetch-stuff-from-cache-success:
    runs-on: ubuntu-latest
    steps:
      - name: restore files from from cache
        uses: actions/cache/restore@v4
        with:
          path: my-files
          key: my-cached-files
      - name: List files
        run: ls -lR
  fetch-stuff-from-cache-failure:
    runs-on: ubuntu-latest
    steps:
      - name: restore files from from cache
        uses: actions/cache/restore@v4
        with:
          path: this-will-fail-because-the-path-is-not-the-same
          key: my-cached-files
      - name: List files
        run: ls -lR

It's not clear anywhere in the documentation or in the error message that you can only restore cached content to the same path it was originally cached from - presumably because if you're using the cache action rather than separate save/restore actions there's no way to specify a different path.

Is there any underlying technical reason I can't restore cached content to a different path than it was originally saved from? If not, this would make the save/restore combination far more flexible for caching files in complex build jobs - but if this isn't possible, could the restore error message be updated to make it explicit that the target path doesn't match and that's the reason it can't find anything?

Thanks,

Dylan

dylanbeattie avatar Aug 02 '24 16:08 dylanbeattie

Is there any underlying technical reason I can't restore cached content to a different path than it was originally saved from? If not, this would make the save/restore combination far more flexible for caching files in complex build jobs

That would be a huge advantage. +1 Probably with an optional extra parameter for the restore action this would be possible.

Sam13 avatar Nov 09 '24 06:11 Sam13

Although it doesn't report the Version hash during load, I believe that's what's causing the cache miss.

This would be a great feature! I ended up exploring this, as I get permission errors when restoring cached files to /usr/local/bin.

So an input for the load action with something like this would be great!:

- name: restore files from from cache
  uses: actions/cache/restore@v4
  with:
    path: /tmp/myfile
    save-path: /usr/local/bin/myfile
    key: my-cached-files

deitChi avatar Jan 29 '25 11:01 deitChi

I'd like to point out the incredibly deceptive error message here. It should clearly state the problem is the path, not the key!

daanzu avatar Sep 18 '25 07:09 daanzu