Commit 4165543e authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '350-update-job-npm-install-should-use-package-lock-json' into 'latest'

Resolve "[Update job] Npm install should use package-lock.json"

Closes #350

See merge request r2devops/hub!210
parents 6a80a52d a390bae7
Loading
Loading
Loading
Loading
+13 −7
Original line number Diff line number Diff line
## Objective

This job installs `npm` dependencies listed in your `package.json` and exposes
This job installs `npm` dependencies listed in your `package-lock.json` and exposes
`node_modules` as cache to other jobs of your pipeline. It allows you to run
`npm install` only once in your pipeline.


## How to use it

!!! info
    If you don't have `package-lock.json`, the job will look for `package.json` and use it instead

1. Ensure that your project have
   [`package.json`](https://docs.npmjs.com/cli/v6/configuring-npm/package-json){:target="_blank"}
   [`package-lock.json`](https://docs.npmjs.com/cli/v6/configuring-npm/package-lock-json){:target="_blank"}
   file which contains the requirements  
1. Add this job URL inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
2. Add this job URL inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
    ```yaml
      - remote: 'https://jobs.r2devops.io/latest/npm_install.yml'
    ```
3. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
4. Well done, your job is ready to work ! 😀
4. Well done, your job is ready to be used ! 😀


## Job details
@@ -40,10 +43,13 @@ This job installs `npm` dependencies listed in your `package.json` and exposes
    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.json`  | `.` |
| `PROJECT_ROOT` | Path to the directory containing `package-lock.json`  | `.` |
| `NPM_INSTALL_OPTIONS` | Additional options for `npm install` | ` ` |
| `NPM_USE_CI` | Enable usage of `npm ci` instead of classic `npm install` | `true` |


### Cache
+9 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
cache:
  key:
    files:
      - "${PROJECT_ROOT}/package-lock.json"
      - "${PROJECT_ROOT}/package.json"
    prefix: "npm-${CI_COMMIT_REF_SLUG}"
  paths:
@@ -17,8 +18,14 @@ npm_install:
  variables:
    PROJECT_ROOT: "."
    NPM_INSTALL_OPTIONS: ""
    NPM_USE_CI: "true"
  script:
    # Working directory
    - cd $PROJECT_ROOT
    # Run npm install
    # 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
+2 −0
Original line number Diff line number Diff line
* Give the priority to `package-lock.json` to fetch package dependencies
* The possibility to use `npm ci` instead of `npm install`
 No newline at end of file
+1 −1

File changed.

Contains only whitespace changes.