I'm using npm to install Node.js dependencies in my project. I want to cache Node.js packages (node_modules
) globally to speed up jobs in pipelines when deploying to Heroku. An example from the official docs of GitLab:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- .npm/
before_script:
- npm ci --cache .npm --prefer-offline
And here is another example in GitLab:
cache:
paths:
- node_modules/
Found some articles (Deploy Node.js App with GitLab CI/CD, Continuous Integration with Node.js, Heroku and GitLab CI/CD -Part 2) that used the second configuration above. I did give it a shot and I was able to deploy my app to Heroku with these settings successfully. But I'm not sure caching mechanism is working properly.
What is the difference between these configurations? Which one is the most convenient way to cache Node.js packages?
My current setup for gitlab-ci.yml
file:
image: node:latest
cache:
paths:
- node_modules/
stages:
- build
- deploy
build:
stage: build
script:
- npm i
- npm i -g gulp-cli
- gulp build
deploy:
image: ruby:latest
stage: deploy
script:
- apt-get update -qy
- apt-get install -y ruby-dev
- gem install dpl
- dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_API_KEY
only:
- master
Not sure if I'm doing it the right way.
npm i
andnpm ci
. But please look at my current setup (gitlab-ci.yml
file), which one is the most convenient in my situation? Or do I even need to cache it? Because I have only 2 stages. I usenode_modules
in the first stage. – Hornpipe