107

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.

Alex
  • 4,512
  • 6
  • 27
  • 47
  • 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
  • 1
    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
  • Could you not flip the logic around and if ($VALUE1 != $VALUE2) { /* rest of job */ } ? This should implicitly end the job (because there is no more code) if they are equal. – Aurora0001 Apr 13 '17 at 16:11
  • @Aurora0001 Nope, there's multiple stages left; this comes almost first thing in a 7 step job. – Alex Apr 13 '17 at 16:12

6 Answers6

96

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.

Gerold Broser
  • 171
  • 10
Alex
  • 4,512
  • 6
  • 27
  • 47
25

The Executor.interrupt(Result) method is the cleanest, most direct way I could find to stop a build prematurely and mark it as a success.

script {
    currentBuild.getRawBuild().getExecutor().interrupt(Result.SUCCESS)
    sleep(1)   // Interrupt is not blocking and does not take effect immediately.
}

Pros:

  • Works in a declarative pipeline just as well as a scripted one.
  • No try/catch or exceptions to handle.
  • Marks the calling stage and any successive stages as green/passing in the UI.

Cons:

  • Requires a number of in-process script approvals, including one that is considered insecure. Approve and use with caution.
Ben Amos
  • 371
  • 3
  • 6
  • For me it doesn't work - consecutive stages are being executed. – Łukasz K Oct 30 '19 at 07:55
  • Can you put this before a stage in a declarative pipeline? – peetasan Jan 16 '20 at 16:21
  • In a declarative pipeline, script blocks are valid only inside of a stage's steps block. If you need to stop execution as described before a given stage, you will need to execute the script during a preceding stage. – Ben Amos Jan 16 '20 at 22:30
  • 2
    org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild – Devaroop Feb 06 '20 at 08:32
  • 4
    Yes, that is what I referred to in my "Cons" section. – Ben Amos Feb 11 '20 at 00:16
  • If you need to stop the build with a particular variable result, you can replace Result.SUCCESS with the fromString static method (such as Result.fromString('UNSTABLE') ). (Warning: requires an additional in-process script approval.) – Cameron Hudson Aug 25 '20 at 22:45
21

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.

CSchulz
  • 311
  • 2
  • 3
  • 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
  • I could not get this to work with Jenkins 2.263.3. WorkflowScript: 23: Expected a stage at the line with the try and after that: WorkflowScript: 22: No stages specified – Peter V. Mørch Apr 26 '21 at 18:23
13

If it is acceptable to set the build status to ABORTED instead of SUCCESS, the following snippet may be used within a pipeline stage to short-circuit the build:

steps {
    script {
        currentBuild.result = 'ABORTED'
        error("Aborting the build.")
    }
}

I tested setting the result to SUCCESS, but doing so resulted in a failed build (using Jenkins 2.235.5). It seems that ABORTED is respected by the error step, while SUCCESS is overridden.

Gerold Broser
  • 171
  • 10
kgriffs
  • 231
  • 2
  • 4
3

I was almost able to accomplish this with declarative pipeline. This allows you to exit the before the pipeline starts based on the outcome of a shell script. I was not able to get this working within the pipeline itself (either within or between stages).

node('master'){
    env.status_code = sh(script: 'exit 0 # or 200 or other', returnStatus:true)
}
println 'status_code: ' + env.status_code
if(env.status_code == '200'){
    println 'Now is not the time to run the pipeline.'
    println 'Exiting without running subsequent stages.'
    currentBuild.result = 'SUCCESS'
    return
} else if(env.status_code == '0'){
    println 'Time to run. Let\'s go!'
} else {
    error 'Unable to check if it\'s time to run.'
}

pipeline {
  agent any
  stages {
      stage('Run only if exit status for was 0'){
          steps{
             sh 'echo hi'
          }
      }
  }
}

when directive may help if you only want to skip certain stages, not exit entirely.

https://www.jenkins.io/doc/book/pipeline/syntax/#when

1

Honestly you shouldn't need to use the 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

MrMesees
  • 111
  • 5
  • 2
    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 00: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
  • 2
    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
  • 4
    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 08:46