Created
March 30, 2021 15:01
-
-
Save rupeshtiwari/44ebec690f2c01bf1df9b1d215a0e723 to your computer and use it in GitHub Desktop.
cache node_modules in github workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Caching npm packages | |
on: push | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Cache node modules | |
id: cache-nodemodules | |
uses: actions/cache@v2 | |
env: | |
cache-name: cache-node-modules | |
with: | |
# caching node_modules | |
path: node_modules | |
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-build-${{ env.cache-name }}- | |
${{ runner.os }}-build- | |
${{ runner.os }}- | |
- name: Install Dependencies | |
if: steps.cache-nodemodules.outputs.cache-hit != 'true' | |
run: npm ci | |
- name: Build | |
run: npm build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The example works well if the primary
key
has a cache hit andnpm ci
is skipped. I would removerestore-keys
though. Runningnpm ci
will remove anynode_modules
folder. If there is a cache entry that matchesrestore-keys
but notkey
, GitHub will restore it, but thencache-hit
will befalse
andnpm ci
will immediately delete the restored cache before installing dependencies from scratch. A minor thing but will save some redundant work.