Search code examples
npmgitlab-cinpx

How to let a Node script fail in a GitLab Pipeline?


We have a GitLab Pipeline in which we let run npm/npx commands/scripts but the issue is that the script doesn't expose an exit code 1 when it produces errors.

Here is the the pipeline:

stages:
  - validate

image: node:latest

verify:
  stage: validate
  script:
    - 'npm i'
    - 'npx orval --config ./orval.config.js'
  artifacts:
    when: always
    paths:
      - src/ 

Despite the fact that the last script npx orval --config ./orval.config.js produces an SyntaxError (see below) the pipeline is in the end successfull (exit code is 0) but from our perspective it should not (exit code is 1) because of the SyntaxError.

🍻 Start orval v6.15.0 - A swagger client generator for typescript
dispo-file: Cleaning output folder
⚠️  SyntaxError: Swagger schema validation failed.
  #/paths/~1disposition/get/parameters/0/schema/type must be equal to one of the allowed values
  #/paths/~1disposition/get/parameters/0/schema must have required property '$ref'
  #/paths/~1disposition/get/parameters/0/schema must match exactly one schema in oneOf
  #/paths/~1disposition/get/parameters/0 must have required property '$ref'
  #/paths/~1disposition/get/parameters/0 must match exactly one schema in oneOf

🎉 dispo-file - Your OpenAPI spec has been converted into ready to use orval!

FYI: Orval generates TypeScript classes for an OpenAPI spec.


Solution

  • According to self-description, orval(1) does not have proper exit status codes in all conditions.

    The procedure with it is to create an issue, describe the situation and ask if they would accept a pull request.

    They then normally confirm they do and then you file the pull request.

    Alternative to a Pull-Request

    Alternatively you could make it a pipeline:

    verify:
      stage: validate
      script:
        - 'npm i'
        - 'npx orval --config ./orval.config.js | grep -vqz "Swagger schema validation failed"'
    

    cf. grep(1).

    Pipeline Variant with the Stream Editor

    Pipeline variant passing the whole output and exiting early (or late with additional -z) and non-zero in case of the error:

    verify:
      stage: validate
      script:
        - 'npm i'
        - 'npx orval --config ./orval.config.js | sed "/Swagger schema validation failed/q1"
    

    cf. sed(1), q1 is a GNU extension (for the 1 as exit-code, which is required to make the script fail on error).


    Thanks to kladderradatsch for reminding in a comment that grep -v ... was wrong and grep -vqz ... the working variant (at least -q was missing for non-zero exit status).