46

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.

  • 1
    Just exit 0... – Tensibai Apr 13 '17 at 15:33
  • I thought that marked the job a failure? If I'm wrong and you can show documentation, I'd be happy to accept that as an answer – Alex Apr 13 '17 at 15:35
  • Well, just bash scripts, exit 0 means success, exit non zero means failure... – Tensibai Apr 13 '17 at 15:36
  • This isn't within a bash script, this is the pipeline job itself, so Groovy. Does that change things? – Alex Apr 13 '17 at 15:36
  • In groovy I'd just try a return 0, all in all any end of the groovy code which doesn't throw an exception should do I think. I'll let someone with more background on jenkins 2 confirm or infirm – Tensibai Apr 13 '17 at 16:04
39

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 failing.

  • just returning should leave the build with a grayed out status and no result... so not quite the same as a failed build. – drewish Oct 28 '17 at 18:47
  • How do you return and skip all remaining stages? – Jess Bowers Nov 13 '17 at 22:30
  • 1
    @JessBowers it's all about where you put the snippet. If you do it node-level rather than stage-level it'll finish the entire job. – Alex Nov 14 '17 at 13:52
  • 2
    Please note that it works only for scripted pipeline, not for declarative – kagarlickij Mar 28 '18 at 11:01
  • @kagarlickij - Correct, declarative pipelines didn't exist when this answer was written! – Alex Mar 28 '18 at 12:34
8

You can also use error to exit the current stage, then you don't have to consider the current stage hierarchy and similar stuff:

def autoCancelled = false

try {
  stage('checkout') {
    ...
    if (your condition) {
      autoCancelled = true
      error('Aborting the build.')
    }
  }
} catch (e) {
  if (autoCancelled) {
    currentBuild.result = 'SUCCESS'
    // return here instead of throwing error to keep the build "green"
    return
  }
  // normal error handling
  throw e
}

But this would lead to a red stage, if the error occurs within a stage.

enter image description here

It depends on your requirements, which way you want to use.

  • If you're going to do it this way, make a new subclass of RuntimeException to throw instead of needing to catch all exceptions and check a flag – Michael Mrozek Oct 15 '18 at 16:24
1

Honestly you shouldn't need to use exit command specifically, but there is a Conditional BuildStep Plugin which may achieve the same end result (code that doesn't run).

I haven't come up against this yet, so haven't used the plugin.

There is also conditionals as found in this prior Stack Overflow post on Jenkins: Jenkins Pipeline Conditional Step/Stage

  • 1
    While your answer seems to be valid (haven't checked it live since I found another workaround), I don't think "Honestly you shouldn't exit" is a good way to start it; clearly the existence of these methods means it IS sometimes necessary to exit. – Alex Apr 15 '17 at 0:18
  • What would you suggest? Moving to an ending statement? Adding the qualifier "generally"? You've provided "feedback" but it's not easy for me to tell the intent or action it, please be less terse. – MrMesees Apr 16 '17 at 19:57
  • Hang on a bit. Is it your question? If it is, TBH you wouldn't want to hear you shouldn't exit, but neither method I've suggested provide exits, they circumvent code running... Directly supporting my position. – MrMesees Apr 16 '17 at 20:15
  • 1
    Ahh I see, I think I misinterpreted your statement as "doing something to end the job is bad" instead of "you shouldn't use the literal exit command" - if so I apologize, that's my misunderstanding. – Alex Apr 16 '17 at 23:34
  • 3
    I've tried to make it clearer, no need to apologize at all, language is a fickle beast, especially on the internet :) – MrMesees Apr 17 '17 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.