Hello! I need cache to be restored and saved again every time the job ends. How to do this?
Hi @ghost6482 ,
When cache-hit value is false, the cache will be restored and saved again. Code sample as below:
- name: Cache node modules
id: cachetest
uses: actions/cache@v1
env:
cache-name: cache-node-modules
with:
path: npm # npm cache files are stored in `npm` on Linux/macOS
key: ${{ runner.os }}-build-${{ env.cache-name }}-${ github.sha }}-${{ hashFiles('**/package.json') }} # Add githu.sha to make the key different each time
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
I added ${{githu.sha}} to make sure the key is different for each commit, which will cause ‘cache-hit’ value is ‘false’.
Cache restored: https://github.com/weide-zhou/github-actions-for-packages/runs/620991567?check_suite_focus=true#step:3:13
Cache saved: https://github.com/weide-zhou/github-actions-for-packages/runs/620991567?check_suite_focus=true#step:9:3
Hope it’s clear and helpful.
3 Likes