Commit 72d9017c authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '425-npm-install-yarn-install-cache-have-issues' into 'latest'

Resolve "Npm install & yarn install cache have issues"

Closes #425

See merge request r2devops/hub!257
parents 39bbb795 cfdb4f10
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -39,27 +39,26 @@ This job installs `npm` dependencies listed in your `package-lock.json` and expo

### Variables

!!! note
    All paths defined in variables are relative and starts from the root of your
    repository.  
    
    ⚠️ It's mendatory to have `package-lock.json` in order to use `npm ci`

| Name | Description | Default |
| ---- | ----------- | ------- |
| `PROJECT_ROOT` | Path to the directory containing `package-lock.json`  | `.` |
| `PROJECT_ROOT` | Relative path to the directory containing `package.json` (**see warning below**)  | ` ` |
| `NPM_INSTALL_OPTIONS` | Additional options for `npm install` | ` ` |
| `NPM_USE_CI` | Enable usage of `npm ci` instead of classic `npm install` | `true` |

!!! warning
    In the case you are updating `PROJECT_ROOT` and you want to have a properly working cache,
    consider making this variable a global variable in the root of your `.gitlab-ci.yml`. Learn how
    easy it is [here](https://docs.gitlab.com/ee/ci/variables/#create-a-custom-cicd-variable-in-the-gitlab-ciyml-file).

### Cache

This job creates a global cache configuration. Regarding the configuration
applied, cache behavior is the following:

* Shared between all jobs and pipelines on the same branch
* Contains folder `$PROJECT_ROOT/node_modules`
* If `npm install` produces different result than the cached content
* Each branch has its own version
* Cached directory is `$PROJECT_ROOT/node_modules`
* If `package.json` or `package-lock.json` is edited, the cache is updated

More information on Gitlab caching mechanism in [Gitlab CI/CD caching
documentation](https://docs.gitlab.com/ee/ci/caching/index.html).
+14 −12
Original line number Diff line number Diff line
@@ -3,11 +3,11 @@
cache:
  key:
    files:
      - "${PROJECT_ROOT}/package-lock.json"
      - "${PROJECT_ROOT}/package.json"
      - "${CI_PROJECT_DIR}/${PROJECT_ROOT}/package-lock.json"
      - "${CI_PROJECT_DIR}/${PROJECT_ROOT}/package.json"
    prefix: "npm-${CI_COMMIT_REF_SLUG}"
  paths:
    - "${PROJECT_ROOT}/node_modules"
    - "${CI_PROJECT_DIR}/${PROJECT_ROOT}/node_modules"

npm_install:
  stage: .pre
@@ -15,16 +15,18 @@ npm_install:
    name: node:15.14-buster
    entrypoint: [""]
  variables:
    PROJECT_ROOT: "."
    NPM_INSTALL_OPTIONS: ""
    NPM_USE_CI: "true"
  script:
    # Working directory
    - cd $PROJECT_ROOT
    # NPM_USE_CI is true
    - if [ ${NPM_USE_CI} == "true" ]; then
    -   npm ci
    - else
    # NPM_USE_CI is false
    -   npm install $NPM_INSTALL_OPTIONS
    - fi
    # PROJECT_ROOT doesn't have a default value, as we want it to be a global CI variable.
    # See more about variable precedence here: https://shorturl.at/hirHY
    - |
      [ ! -z "${PROJECT_ROOT}" ] && cd ${CI_PROJECT_DIR}/${PROJECT_ROOT}
    # Run npm ci if package-lock.json is available & variable is truthy
    - |
      if [ -f "package-lock.json" ]; then
        [[ ${NPM_USE_CI} == "true" ]] && npm ci || npm install $NPM_INSTALL_OPTIONS
      else
        npm install $NPM_INSTALL_OPTIONS
      fi
+1 −0
Original line number Diff line number Diff line
* Fix cache relative paths
+8 −8
Original line number Diff line number Diff line
@@ -36,24 +36,24 @@ This job installs `yarn` dependencies listed in your `package.json` and exposes

### Variables

!!! note
    All paths defined in variables are relative and starts from the root of your
    repository.

| Name | Description | Default |
| ---- | ----------- | ------- |
| `PROJECT_ROOT` | Path to the directory containing `package.json`  | `.` |
| `PROJECT_ROOT` | Relative path to the directory containing `package.json` (**see warning below**)  | ` ` |
| `YARN_INSTALL_OPTIONS` | Additional options for `yarn install` | ` ` |

!!! warning
    In the case you are updating `PROJECT_ROOT` and you want to have a properly working cache, 
    consider making this variable a global variable in the root of your `.gitlab-ci.yml`. Learn how
    easy it is [here](https://docs.gitlab.com/ee/ci/variables/#create-a-custom-cicd-variable-in-the-gitlab-ciyml-file).

### Cache

This job creates a global cache configuration. Regarding the configuration
applied, cache behavior is the following:

* Shared between all jobs and pipelines on the same branch
* Contains folder `$PROJECT_ROOT/node_modules`
* If `yarn install` produces different result than the cached content
* Each branch has its own version
* Cached directory is `$PROJECT_ROOT/node_modules`
* If `package.json` or `yarn.lock` is edited, the cache is updated

More information on Gitlab caching mechanism in [Gitlab CI/CD caching
documentation](https://docs.gitlab.com/ee/ci/caching/index.html).
+2 −0
Original line number Diff line number Diff line
* Fix cache relative paths
* Add prefix to cache
Loading