actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.39k stars 1.17k forks source link

Cache action doesn't restore most recently created cache item when restore-keys being used #1199

Closed ohaval closed 5 months ago

ohaval commented 1 year ago

Hey,

I am using the actions/cache/restore@v3 action as part of my own action. I am storing the cache items with a specific pattern, which doesn't allow me to get cache hits, so I'm counting on restore-keys to get me the last cached item based on a prefix search.

As the (docs)[https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key] say: "If there are multiple partial matches for a restore key, the action returns the most recently created cache."

With that being said, I am experiencing an issue when I receive a non-most recently create cache for a specific prefix. Is there any known problem with it? Is there any specific way needed to make it work?

Currently, my experiments are in a private organization, but if needed I can reproduce it in a test public repository.

This is my usage of the action inside the YML file:

    - uses: actions/cache/restore@v3
      with:
        path: ci/cache/docker/
        # Instead of attempting to get a cache hit, we just attempt to load the last version of the image from the cache.
        # If the image reference has updated, docker pull will verify that and pull the new version, then we will
        # save the new version to the cache
        key: 'null'
        restore-keys: ${{ steps.set-cache-file.outputs.cache_file }}@
        # Value example: registry.example.com__some-image:latest@

And this is how I store a new item in the cache storage:

    - uses: actions/cache/save@v3
      with:
        path: ci/cache/docker/${{ steps.set-cache-file.outputs.cache_file }}@${{ steps.set-image-id.outputs.image_id }}.tar
        key: ${{ steps.set-cache-file.outputs.cache_file }}@${{ steps.set-image-id.outputs.image_id }}.tar
        # Value example: registry.example.com__some-image:latest@sha256:abcdef.tar

Thanks

ohaval commented 1 year ago

I managed to fix the issue by settings the path argument in the cache/save@v3 action to ci/cache/docker. So it seems like before, when I attempted to retrieve the cache I used the directory as the value, and when I stored the cache I used a file reference. And because of that, the cache failed to be retrieved on the next execution.

deemp commented 1 year ago

@ohaval, there's an issue with path closed by https://github.com/actions/toolkit/pull/1378. There's a workaround https://github.com/actions/toolkit/pull/1378#issuecomment-1478388929 that can be applied in your fork of actions/cache

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 200 days with no activity. Leave a comment to avoid closing this issue in 5 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 5 days since being marked as stale.