Reputation: 1463
I am working on the performance tuning for Gitlab pipeline using cache
.
This is a nodejs project using npm
for the dependency management. I have put the node_modules
folder into cache for subsequent stages with following setting:
build:
stage: build
only:
- develop
script:
- npm install
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- node_modules/
Could I make the cache available for pipeline triggered next time? Or the cache is accessible in single pipeline?
If I can access that within multiple pipeline, could I recache the node module only when we change package.json?
Upvotes: 1
Views: 2148
Reputation: 2546
First, put the cache on the global level. This will make sure, that the jobs share the same cache.
Second, you can use cache:key:files
introduced with GitLab 12.5 to only recreate the cache when the package.json changes.
cache:
key:
files:
- package.json
paths:
- node_modules/
build:
stage: build
only:
- develop
script:
- npm install
Further information:
https://docs.gitlab.com/ee/ci/yaml/#cachekeyfiles
Additional hints:
package-lock.json
instead of package.json
.npm install
, you can also skip this step when the node_modules
folder was recreated from cache. Following bash addition to your npm install will only run the command, if the node_modules folder doesn't exist.build:
stage: build
only:
- develop
script:
- if [ ! -d "node_modules" ]; then npm install; fi
Upvotes: 2