Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Halt a jenkins pipeline job early

In our Jenkins Pipeline job we have a couple of stages, and what I would like is if any of the stages fail, then to have the build stop and not continue on to the further stages.

Here's an example of one of the stages:

stage('Building') {
    def result = sh returnStatus: true, script: './build.sh'
    if (result != 0) {
        echo '[FAILURE] Failed to build'
        currentBuild.result = 'FAILURE'
    }
}

The script will fail, and the build result will update, but the job continues on to the next stages. How can I abort or halt the job when this happens?

like image 468
ecnepsnai Avatar asked Dec 01 '16 19:12

ecnepsnai


People also ask

How do I stop a Jenkins pipeline from running?

Pipeline jobs can be stopped by sending an HTTP POST request to URL endpoints of a build. BUILD ID URL/stop - aborts a Pipeline. BUILD ID URL/term - forcibly terminates a build (should only be used if stop does not work). BUILD ID URL/kill - hard kill a pipeline.

Can we pause Jenkins pipeline?

Using the Jenkins CLI you can pause and play the jobs. If you would not like to go with CLI mode means, please ignore it. This may help for who would like to pause the job using Jenkins CLI. But you can resume the job from both UI and CLI.

How do I stop Jenkins pipeline if stage fails?

Alternatively you can call error(String message) step to stop the pipeline and set its status to FAILED . For example, if your stage 1 calls error(msg) step like: stage("Stage 1") { steps { script { error "This pipeline stops here!" } } }

How do I stop Jenkins scheduled job?

In order to prevent Jenkins from executing any jobs, you need to put it in "quiet down" mode when it starts up. There are two ways you can do this. You can install and use the Quiet Start plugin. This will give you a UI option to restart Jenkins and have it be in "quiet down" mode when it starts up.


3 Answers

Basically that is what the sh step does. If you do not capture the result in a variable, you can just run:

sh "./build"

This will exit if the script returns a non-zero exit code.

If you need to do stuff first, and need to capture the result, you can use a shell step to quit the job

stage('Building') {
    def result = sh returnStatus: true, script: './build.sh'
    if (result != 0) {
        echo '[FAILURE] Failed to build'
        currentBuild.result = 'FAILURE'
        // do more stuff here

        // this will terminate the job if result is non-zero
        // You don't even have to set the result to FAILURE by hand
        sh "exit ${result}"  
    }
}

But the following will give you the same, but seems more sane to do

stage('Building') {
    try { 
         sh './build.sh'
    } finally {
        echo '[FAILURE] Failed to build'
    }
}

It is also possible to call return in your code. However if you are inside a stage it will only return out of that stage. So

stage('Building') {
   def result = sh returnStatus: true, script: './build.sh'
   if (result != 0) {
      echo '[FAILURE] Failed to build'
      currentBuild.result = 'FAILURE'
      return
   }
   echo "This will not be displayed"
}
echo "The build will continue executing from here"

wont exit the job, but

stage('Building') {
   def result = sh returnStatus: true, script: './build.sh'
}
if (result != 0) {
  echo '[FAILURE] Failed to build'
  currentBuild.result = 'FAILURE'
  return
}

will.

like image 60
Rik Avatar answered Oct 19 '22 18:10

Rik


Since Jenkins v2 this should also work

error('Failed to build')

The Job will end with:

ERROR: Failed to build
Finished: ERROR
like image 32
sobi Avatar answered Oct 19 '22 18:10

sobi


Another way to achieve this behavior is to throw an Exception. In fact, this is just what Jenkins itself does. That way, you can also set the build status either to ABORTED or FAILURE. This example aborts the build:

stage('Building') {
    currentBuild.rawBuild.result = Result.ABORTED
    throw new hudson.AbortException('Guess what!')
    echo 'Further code will not be executed'
}

Output:

[Pipeline] stage
[Pipeline] { (Building)
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: Guess what!
Finished: ABORTED
like image 10
Jazzschmidt Avatar answered Oct 19 '22 19:10

Jazzschmidt