zagd
zagd

Reputation: 371

How do I get my Azure DevOps Pipeline build to fail when my linting script returns an error?

I am using the Azure Pipelines GitHub add-on to ensure that pull requests pass my linting. However, I have just made a test pull request which fails my linting, but the Azure Pipeline succeeds.

Here is my azure-pipelines.yml

# Node.js with React
# Build a Node.js project that uses React.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

pool:
  vmImage: 'Ubuntu-16.04'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '8.x'
  displayName: 'Install Node.js'

- script: |
    npm install
    npm run lint # Mapped to `eslint src` in package.json
    npm run slint # `stylelint src` in package.json
    npm run build
  displayName: 'npm install and build'

And here is (part of) the output on a branch which I know fails on npm run lint

> [email protected] lint /home/vsts/work/1/s
> eslint src


/home/vsts/work/1/s/src/js/components/CountryInput.js
  26:45  error  'onSubmit' is missing in props validation  react/prop-types
  27:71  error  'onSubmit' is missing in props validation  react/prop-types

✖ 2 problems (2 errors, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] lint: `eslint src`
npm ERR! Exit status 1 # Exit status 1, yet the build succeeds?
npm ERR! 
npm ERR! Failed at the [email protected] lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vsts/.npm/_logs/2019-03-16T05_30_52_226Z-debug.log

> [email protected] slint /home/vsts/work/1/s
> stylelint src


> [email protected] build /home/vsts/work/1/s
> react-scripts build

Creating an optimized production build...
Compiled successfully.

# Truncated...

As you can see, linter runs nicely and catches my intentional error (I removed a prop type validation), and exits with code 1.

However the build just carries on its merry way.

What do I need to do to make such a linting error stop my build in its tracks and not return success?

Thank you in advance.

Upvotes: 16

Views: 32173

Answers (5)

BitByteDog
BitByteDog

Reputation: 3514

Set the "exit on unhandled error" option set -e, this will catch any command that returns non zero return in the script:

- script: |
    set -e
    npm install
    npm run lint # Mapped to `eslint src` in package.json
    npm run slint # `stylelint src` in package.json
    npm run build
  displayName: 'npm install and build'

Note: If the command is used as a condition in a condirional statement like if <cmd> ; then ...; fi , any error return of <cmd> is considered as handled, so will not cause the script to exit.

Upvotes: 0

Marco Jansen
Marco Jansen

Reputation: 61

I know the post is old, but I missed the best answer. Best way without having to jump through hoops in the script itself, is to use failOnStderr: true Add this on any multiline script task!

From https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-script?view=azure-pipelines

steps:
- script: string # Required as first property. An inline script.
  failOnStderr: string # Fail the task if output is sent to Stderr?

Upvotes: 0

PersonThing
PersonThing

Reputation: 388

Neither approach above worked for my specific scenario (windows build agent, wanted to run a custom linting script, didn't want to use a script in package.json)

If you just throw an error from a node script, the pipeline treats it as a failed step.

pipelines yml:

  - script: 'node lint-my-stuff'
    displayName: 'Lint my stuff'

lint-my-stuff.js

// run eslint, custom checks, whatever

if (/* conditions aren't met */)
  throw new Error('Lint step failed')

console.log('Lint passed')

Upvotes: 2

Mike Wright
Mike Wright

Reputation: 81

You could also use an npm task. The default is to fail the build when there is an error. I had the same problem and the below worked for me:

- task: Npm@1
  displayName: 'Lint'
  inputs:
    command: 'custom'
    customCommand: 'run lint'

From the documentation for tasks:

- task: string  # reference to a task and version, e.g. "VSBuild@1"
  condition: expression     # see below
  continueOnError: boolean  # 'true' if future steps should run even if this step fails; defaults to 'false'
  enabled: boolean          # whether or not to run this step; defaults to 'true'
  timeoutInMinutes: number  # how long to wait before timing out the task

Upvotes: 8

4c74356b41
4c74356b41

Reputation: 72191

this means your script "swallows" the exit code and exits normally. you need to add a check to your script that would catch the exit code of your npm run lint and exit with the same exit code, something like:

- script: |
    npm install
    npm run lint # Mapped to `eslint src` in package.json
    if [ $? -ne 0 ]; then
        exit 1
    fi
    npm run slint # `stylelint src` in package.json
    npm run build

Upvotes: 26

Related Questions