How to cache node_modules for all pipelines in a project
I would like to avoid running the yarn install
part if the yarn.lock file has not changed for all subsequent pipeline runs for the project. For this I would need to cache the node_modules and some how to NOT run the yarn install part if the yarn.lock file hasn’t changed. If anyone can please help and explain how to set up the cache globally? Below (after the snippet)i have included another snippet of what my interpretation of how to configure the cache would look like after reading some of the documentation.
Here is my current snippet of my .gitlab-ci.yml
*
stages:
- bootstrap
- package:deploy
.install-lint-test: &install-lint-test
- yarn install
# --quiet hides all the warnings
- npx eslint --quiet './packages/**/*.js' './packages/**/*.jsx'
- npx eslint --quiet './widgets/**/*.js' './widgets/**/*.jsx'
- npx eslint --quiet './components/**/*.js' './components/**/*.jsx'
- npx eslint --quiet './tools/**/*.js' './tools/**/*.jsx'
# tests
- npm run test:ci
bootstrap:feat:
stage: bootstrap
image: rbuenavida360/node10-chromeheadless-alpine:latest
script:
- *install-lint-test
only:
- branches
artifacts:
paths:
- dist/
except:
- tags
- master
- development
- integration
bootstrap:dev:
stage: bootstrap
image: rbuenavida360/node10-chromeheadless-alpine:latest
extends:
- .build_cache
script:
- *install-lint-test
# Build assets
- npx lerna exec -- npm version prerelease --preid=${CI_PIPELINE_IID}
- echo "--> Build scripts and packages"
- npx lerna run --concurrency 4 build -- --env=dev & npx lerna run --concurrency 4 build -- --env=prod
- npm run pack:all
- npm run copy:all -- -- -- --skipDocs --env=dev --env=prod
only:
- development
except:
- tags
artifacts:
expire_in: 2h
paths:
- dist/
...etc
Based on what i’m interpreting, the cache should be defined before the stages part like so:
cache:
key:
files:
- yarn.lock
paths:
- node_modules/
- "packages/**/node_modules/"
- "components/**/node_modules/"
- "widgets/**/node_modules/"
- "tools/**/node_modules/"
policy: pull
1 - Is this correct?
2 - I need help defining the missing part in my gitlab ci to run the yarn install
and push to the global cache only if the yarn.lock file has changed.
Any help would be greatly appreciated