添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more

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?

This will exit if the script reurns 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.

Doesn't ./build just break it because the folder doesn't exist and exit with code 1. Am I correct in thinking this is a workaround and not an inbuilt solution? – MyName Jul 26 '19 at 12:56 Doing something like your last example (placing code outside a stage) causes compilation errors for my pipeline (along the lines of "expecting stage"). And I'd like it to exit the entire job. – Nagev Nov 14 '19 at 15:02 Hi Nagev, Unfortenately at the time of writing this still worked. It is very likely that now it won't. Unfortunately I haven't been working with Jenkins for the last few years. Can you tell me what version you are using (of jenkins)?. Then I can investigate and see if I can help you out with this. – Rik Nov 20 '19 at 8:36

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
                With Jenkins 2.89.3 this is an error: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new hudson.AbortException java.lang.String
– Kevin Deenanauth
                Jan 27 '18 at 22:45
                Looks like Sandbox mode is enabled and forbids throwing that Exception. Kind of strange, but disabling Sandbox mode should do the trick.
– Jazzschmidt
                Jan 29 '18 at 10:15
                Alternatively you can permit the usage via Manage Jenkins > In-process Script Approval
– Jazzschmidt
                Jan 29 '18 at 10:16
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.4.9.36569