Abort current build from pipeline in Jenkins

时间秒杀一切 提交于 2019-12-17 10:33:29

问题


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.


回答1:


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):




回答2:


After some testing I came up with the following solution:

def autoCancelled = false

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

This will result into following stage view:

failed stage

If you don't like the failed stage, you have to use return. But be aware you have to skip each stage or wrapper.

def autoCancelled = false

try {
  stage('checkout') {
    ...
    if (your condition) {
      autoCancelled = true
      return
    }
  }
  if (autoCancelled) {
    error('Aborting the build to prevent a loop.')
    // return would be also possible but you have to be sure to quit all stages and wrapper properly
    // return
  }
} catch (e) {
  if (autoCancelled) {
    currentBuild.result = 'ABORTED'
    echo('Skipping mail notification')
    // return here instead of throwing error to keep the build "green"
    return
  }
  // normal error handling
  throw e
}

The result:

custom error as indicator

You can also use a custom message instead of a local variable:

final autoCancelledError = 'autoCancelled'

try {
  stage('checkout') {
    ...
    if (your condition) {
      echo('Aborting the build to prevent a loop.')
      error(autoCancelledError)
    }
  }
} catch (e) {
  if (e.message == autoCancelledError) {
    currentBuild.result = 'ABORTED'
    echo('Skipping mail notification')
    // return here instead of throwing error to keep the build "green"
    return
  }
  // normal error handling
  throw e
}



回答3:


Following this documentation from Jenkins, you should be able to generate an error to stop the build and set the build result like this:

currentBuild.result = 'ABORTED'

Hope that helps.




回答4:


I handled in a declarative way as shown below:

Based on catchError block it will execute post block. If post result falls under failure category, the error block will be executed to stop upcoming stages like Production, PreProd etc.

pipeline {

agent any

stages{
    stage('Build') {
        steps{
             catchError {
                sh '/bin/bash path/To/Filename.sh'
               }
         }
         post {
            success {
                echo 'Build stage successful'
            }
            failure {
                echo 'Compile stage failed'
                error('Build is aborted due to failure of build stage')

             }
    }
   }
  stage ('Production'){
        steps {
                sh '/bin/bash path/To/Filename.sh'              
              }
        }
   }

}




回答5:


The thing that we use is:

try {
 input 'Do you want to abort?'
} catch (Exception err) {
 currentBuild.result = 'ABORTED';
 return;
}

The "return" at the end makes sure that no further code is executed.



来源:https://stackoverflow.com/questions/42667600/abort-current-build-from-pipeline-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!