Commit 55996825 authored by totara-thib's avatar totara-thib
Browse files

New job npm_scripts

parent 3f3005aa
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
## Objective

This job will permits users to run several scripts from their `package.json` file using `npm`. Scripts can be used with args.


## 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 predefined command in the `scripts` object
2. If you want the job to run scripts make sure to add them inside the `variable` `NPM_SCRIPTS` and separate every command with `;`
3. 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_scripts.yml'
    ```
4. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
5. You are done, the job is ready to use ! 😉


## Job details

* Job name: `npm_scripts`
* Default stage: `others`
* 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` | Path to the directory containing `package.json`  | `.` |
| `NPM_SCRIPTS` | Value of the differents scripts specified in `package.json` that can be separated by `;` | ` ` |
| `NPM_OUTPUT` | Path to the output send by script specified in `package.json` | ` ` |


### Cache

To cache `node_modules` folder for other `npm` jobs, take a look at [`npm_install`](/jobs/others/npm_install/#cache)
+13 −0
Original line number Diff line number Diff line
name: npm_scripts
description: A ready to use npm job that run predefined scripts specified in package.json
default_stage: others
icon: 💡
maintainer: totara-thib
license: MIT
images:
    "node": "16.8-buster"
tools:
labels:
    - GitLab
    - Utilities
    - NPM
+36 −0
Original line number Diff line number Diff line
  # Job from R2Devops hub --> r2devops.io


stages:
  - others


npm_scripts:
  stage: others
  image:
    name: node:16.8-buster
    entrypoint: [""]
  variables:
    PROJECT_ROOT: "."
    NPM_SCRIPTS: ""
    NPM_OUTPUT: ""
  script:
    # Working directory
    - cd $PROJECT_ROOT
    # Save default IFS
    - oldIFS=$IFS
    # Modifying IFS
    - export IFS=";"
    # Loop for launching any script specified in $NPM_SCRIPTS separated by ;
    - for script in $NPM_SCRIPTS; do
        # Restore to default IFS
    -   IFS=$oldIFS
        # Run the script
    -   npm run $script
        # Modifying IFS for the next loop
    -   export IFS=";"
    - done
  artifacts:
    paths:
      # Output directory
      - $NPM_OUTPUT
+1 −0
Original line number Diff line number Diff line
* Initial version