Only 'npm install' in GitLab CI when package.json has been updated : <p>I'm using GitLab CI for a project and the first step of the process is <code>npm install</code>. I cache <code>node_modules</code> for quicker runs of the same job later on, and also define them as build artifacts in order to use them in later stages. However, even though I cache <code>node_modules</code> and it's up-to-date, calling <code>npm install</code> each time the <code>install_packages</code> job is run takes a long time, since the command goes through all of <code>package.json</code> and checks for updates of packages and such (I assume).</p>

<p>Is there any way to <strong>only</strong> run <code>npm install</code> in the <code>install_packages</code> job depending on some condition? More specifically (what I think would be the best solution), whether or not <code>package.json</code> has been changed since last build?</p>

<p>Below is the relevant part of my .gitlab-ci.yml file:</p>

<pre><code>image: node:6.9.1

stages:
  - install
  - prepare
  - deploy

install_packages:
  stage: install
  script:
    - npm prune
    - npm install
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - node_modules/
  artifacts:
    paths:
      - node_modules/
  only:
    - master
    - develop

build_and_test:
  stage: prepare
  script:
    #do_stuff...

deploy_production:
  stage: deploy
  #do_stuff...

deploy_staging:
  stage: deploy
  #do_stuff...
</code></pre>
