HiveBrain v1.2.0
Get Started
← Back to all entries
patternCritical

Cleanest way to prematurely exit a Jenkins Pipeline job as a success?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
successjenkinswaycleanestprematurelyexitjobpipeline

Problem

I have a job that will create files, unless one of the values being fed to it matches an older value. What's the cleanest way in Jenkins to abort or exit the job, without it being FAILED? It exiting is the correct behavior so I want the build marked SUCCESS.

It'll end up in an if statement like so;

stage ('Check value') {

     if( $VALUE1 == $VALUE2 ) {
       //if they do match exit as a success, else continue with the rest of the job 
    }

}


I don't want to throw an error code unless that can somehow translate into it being marked a successful build.

Solution

Please note: The following works for scripted pipelines only. For a solution for declarative pipelines see @kgriffs' answer.

---

Figured it out. Outside of any stages (otherwise this will just end the particular stage as a success) do the following;

if( $VALUE1 == $VALUE2 ) {
   currentBuild.result = 'SUCCESS'
   return
}


return will stop the stage or node you're running on which is why running it outside of a stage is important, while setting the currentBuild.result prevents it from failing.

Code Snippets

if( $VALUE1 == $VALUE2 ) {
   currentBuild.result = 'SUCCESS'
   return
}

Context

StackExchange DevOps Q#885, answer score: 96

Revisions (0)

No revisions yet.