Abort current build from pipeline in Jenkins

seg picture seg · Mar 8, 2017 · Viewed 85.5k times · Source

I have a Jenkins pipeline which has multiple stages, for example:

node("nodename") {
  stage("Checkout") {
    git ....
  }
  stage("Check Preconditions") {
    ...
    if(!continueBuild) {
      // What do I put here? currentBuild.xxx ?
    }
  }
  stage("Do a lot of work") {
    ....
  }
}

I want to be able to cancel (not fail) the build if certain preconditions are not met and there is no actual work to be done. How can I do this? I know the currentBuild variable is available, but I can't find the documentation for it.

Answer

Christopher Orr picture Christopher Orr · Mar 8, 2017

You can mark the build as ABORTED, and then use the error step to cause the build to stop:

if (!continueBuild) {
    currentBuild.result = 'ABORTED'
    error('Stopping early…')
}

In the Stage View, this will show that the build stopped at this stage, but the build overall will be marked as aborted, rather than failed (see the grey icon for build #9):

Pipeline Stage View