Eric Tan
Eric Tan

Reputation: 1463

Gitlab pipeline: How to recache node modules only when dependency changed?

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/

Upvotes: 1

Views: 2148

Answers (1)

René Stalder
René Stalder

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:

  • You might want to check on package-lock.json instead of package.json.
  • I recommend reading the cache mismatch chapter in the documentation to make sure you don't run into common problems where the cache might not be restored.
  • Instead of simply adding 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

Related Questions