Commit b1ff8f07 authored by GridexX's avatar GridexX Committed by Protocole
Browse files

Resolve "[New job] - npm lint"

parent c905e222
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
## Objective

This job will run a predefined `lint` script in your `package.json` which will check your code quality.


## How to use it

1. Make sure that your project has 
   [`package.json`](https://docs.npmjs.com/cli/v6/configuring-npm/package-json){:target="_blank"}
   file which contains the required `lint` command in the `scripts` object
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_lint.yml'
    ```
3. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
4. You are done, the job is ready to use ! 😉


## Job details

* Job name: `npm_lint`
* Default stage: `static_tests`
* Docker image: [`node:16.8-buster`](https://hub.docker.com/_/node){:target="_blank"}
* When: `always`


## Variables

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

| Name | Description | Default |
| ---- | ----------- | ------- |
| `PROJECT_ROOT` | Relative path to the directory containing `package.json` (**see warning below**)  | ` ` |
| `NPM_INSTALL_OPTIONS` | Additional options for `npm install` | ` ` |
| `NPM_LINT_OPTIONS` | Additional options for `npm run lint` | ` ` |

!!! 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:

* 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).
 No newline at end of file

jobs/npm_lint/job.yml

0 → 100644
+15 −0
Original line number Diff line number Diff line
name: npm_lint
description: A ready-to-use linter, to help validate the quality of your source code. It uses available node_modules if there are any from npm_install job. 
default_stage: static_tests
icon: 🔍
maintainer: GridexX
license: MIT
images:
  "node": "16.8-buster"
tools:
labels:
  - GitLab
  - Linter
  - Quality
  - NPM
  - Utilities
+25 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io
stages: 
- static_tests

cache:
  key:
    files:
      - "${CI_PROJECT_DIR}/${PROJECT_ROOT}/package-lock.json"
      - "${CI_PROJECT_DIR}/${PROJECT_ROOT}/package.json"
    prefix: "npm-${CI_COMMIT_REF_SLUG}"
  paths:
    - "${CI_PROJECT_DIR}/${PROJECT_ROOT}/node_modules"

npm_lint:
  stage: static_tests
  image: 
      name: node:16.8-buster
      entrypoint: [""]
  variables:
    NPM_INSTALL_OPTIONS: ""
    NPM_LINT_OPTIONS: ""
  script:
    - cd ${CI_PROJECT_DIR}/${PROJECT_ROOT}
    - npm install ${NPM_INSTALL_OPTIONS}
    - npm run lint ${NPM_LINT_OPTIONS}
+1 −0
Original line number Diff line number Diff line
* Initial version
 No newline at end of file