cache icon indicating copy to clipboard operation
cache copied to clipboard

Allow caching by github.ref AND github.sha

Open Mahoney opened this issue 3 years ago • 3 comments

I would like to use the cache action in two currently incompatible ways:

  1. Cache by branch name:
      - uses: actions/cache@v2
        with:
          path: cached.txt
          key: mycache-${{ github.ref }}-${{ github.run_number }}
          restore-keys: |
            mycache-${{ github.ref }}-
            mycache-

Here a subsequent push to a branch benefits from the cache generated by the previous push to that branch.

  1. Cache by commit hash:
      - uses: actions/cache@v2
        with:
          path: cached.txt
          key: mycache-${{ github.sha }}

Here tagging a previously built commit gets exactly the cache generated by the build of that specific commit on any branch.

I can think of two ways this could be implemented:

  1. Allow a cache to be stored under multiple keys:
      - uses: actions/cache@v2
        with:
          path: cached.txt
          keys: |
            mycache-${{ github.ref }}-${{ github.run_number }}
            mycache-${{ github.sha }}
          restore-keys: |
            mycache-${{ github.ref }}-
            mycache-

this wouldn't require storing the actual blob multiple times of course, it would just be two pointers to the same blob.

  1. Allow glob expressions in restore-keys:
      - uses: actions/cache@v2
        with:
          path: cached.txt
          key: mycache-${{ github.ref }}-${{ github.sha }}
          restore-keys: |
            **-${{ github.sha }}
            mycache-${{ github.ref }}-
            mycache-

This would keep the single key behaviour, but would allow a cache with primary key mycache-refs/heads/somebranch-2be41deebcd2d46926eff0989c6992229aa6d6d3 to be matched first by mycache-refs/tags/0.1.0-2be41deebcd2d46926eff0989c6992229aa6d6d3 rather than falling back on whatever was last cached under mycache-.

Mahoney avatar Feb 14 '22 11:02 Mahoney

👋 @Mahoney Thanks for reaching out to us. You are right. Currently, we don't support saving a single cache by multiple keys which is in your case can be either ref OR sha. I am not seeing we have any plan to support this in near future. @bishal-pdMSFT / @N-Usha any thoughts you want to share here on supporting glob expression in restore keys

tiwarishub avatar Feb 14 '22 17:02 tiwarishub

  1. Cache by branch name:
      - uses: actions/cache@v2
        with:
          path: cached.txt
          key: mycache-${{ github.ref }}-${{ github.run_number }}
          restore-keys: |
            mycache-${{ github.ref }}-
            mycache-

This should be possible already unless restore-keys does not support context expression which I don't think should the case. What behavior are you seeing here?

  1. Cache by commit hash:
      - uses: actions/cache@v2
        with:
          path: cached.txt
          key: mycache-${{ github.sha }}

Here tagging a previously built commit gets exactly the cache generated by the build of that specific commit on any branch.

This won't be possible as cache cannot be shared across branches even for same commit. https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache

bishal-pdMSFT avatar Sep 07 '22 08:09 bishal-pdMSFT

Cache by branch name absolutely works, it was the ability to combine multiple ways of caching and retrieving I wanted.

I didn't know about caches not being shared across branches - does that include a tags? So if commit abcde on branch main populates a cache with key mycache-abcde, and I then tag it as release-1 prompting a new build, a restore-key of mycache-abcde will not find that cache? That seems a shame.

Mahoney avatar Sep 07 '22 13:09 Mahoney

Version 3.2 and above now support distinct actions/cache/save and actions/cache/restore capabilities.

Therefore, you can now create multiple caches, each with a distinct name.

Yes, this does result in wasted space, as each duplicate name will use the same space in the cache as the original.

[[ edit: fix version that first provides distinct save/restore actions as 3.2, not 3.0 ]]

henrygab avatar Dec 31 '22 21:12 henrygab

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] avatar Jul 20 '23 08:07 github-actions[bot]

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

github-actions[bot] avatar Jul 25 '23 08:07 github-actions[bot]