Vlad
Vlad

Reputation: 9481

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?

Upvotes: 9

Views: 6100

Answers (2)

portenez
portenez

Reputation: 1229

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
}

Upvotes: 8

amuniz
amuniz

Reputation: 3342

There is nothing to do at Jenkins side but at Gulp side. The call to gulp CLI needs to return a proper error value to have the sh step failing correctly. Jenkins just interprets what the shell is returning, so you juts need to make Gulp to return a fail when tests fail (see this blog post, it seems to achieve exactly that).

Upvotes: 0

Related Questions