我已经处理使用try catch块詹金斯管道步骤。我想手动抛出一个异常,对于某些情况。但它显示了以下错误。
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.io.IOException java.lang.String
我检查了scriptApproval部分并没有挂起的批准。
如果你想放弃的例外程序,你可以使用管道一步error
停止流水线执行一个错误。例如:
try {
// Some pipeline code
} catch(Exception e) {
// Do something with the exception
error "Program failed, please read logs..."
}
如果要停止与成功状态的管道,你可能希望有某种布尔表明您已经管线必须停止,e.g:
boolean continuePipeline = true
try {
// Some pipeline code
} catch(Exception e) {
// Do something with the exception
continuePipeline = false
currentBuild.result = 'SUCCESS'
}
if(continuePipeline) {
// The normal end of your pipeline if exception is not caught.
}
这就是我如何做到这一点的詹金斯2.x版本
注意:不要使用错误信号,它会跳过任何职位步骤。
stage('stage name') {
steps {
script {
def status = someFunc()
if (status != 0) {
// Use SUCCESS FAILURE or ABORTED
currentBuild.result = "FAILURE"
throw new Exception("Throw to stop pipeline")
// do not use the following, as it does not trigger post steps (i.e. the failure step)
// error "your reason here"
}
}
}
post {
success {
script {
echo "success"
}
}
failure {
script {
echo "failure"
}
}
}
}
似乎没有其他类型的异常比Exception
可以抛出。没有IOException
,没有RuntimeException
等。
这将工作:
throw new Exception("Something went wrong!")
但这些不会:
throw new IOException("Something went wrong!")
throw new RuntimeException("Something went wrong!")