Search code examples
jenkins-workflow

Stop the pipeline when stage is unstable


I have a Jenkins build pipeline created using workflow plugin. At the beginning the pipeline runs a gulp build inside of the docker container and then archives test results using the following code

step([$class: 'JUnitResultArchiver', testResults: 'build/test-results/*.xml'])

In the following steps I package up the artifacts and ship them to the binary repository.

When unit tests are not passing Jenkins understands the build is unstable and marks it yellow. However it still continues with subsequent steps in the pipeline. Is there any way make the pipeline stop when unit tests are failing?


Solution

  • the JUnitResultArchiver will cause this condition to be true when the build is unstable:

    currentBuild.result != null.

    If I remember correctly it sets it to UNSTABLE, but it is enough to check that is different than null.

    So you could do something like

    step([$class: 'JUnitResultArchiver', testResults: 'build/test-results/*.xml'])
    if (currentBuild.result == null) {
        //contintue with your pipeline
    } else {
        //notify that the build is unstable. //or just do nothing
    }