我已经使用try catch块处理了Jenkins流水线步骤。 我想在某些情况下手动抛出异常,但它显示以下错误。
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.io.IOException java.lang.String
我检查了scriptApproval部分,没有待批准的内容。我已经使用try catch块处理了Jenkins流水线步骤。 我想在某些情况下手动抛出异常,但它显示以下错误。
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..."
}
如果您想以成功的状态停止流水线,则可能需要一些布尔值,指示必须停止您的流水线,例如:
如果你想要使用成功状态来停止你的pipeline,你可能需要一个布尔变量来指示必须停止pipeline,例如:
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.
}
以下是我在Jenkins 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
等。 - Marco RoycurrentBuild.result = "SUCCESS"
对我来说不起作用;它仍然会进入 post.failure
块。 - Christopher Runggroovy.lang.MissingMethodException: No signature of method: java.lang.Exception.call() is applicable for argument types: : (org.codehaus.groovy.runtime.GStringImpl)
- jaques-sam似乎没有任何类型的异常可以被抛出,除了Exception
。没有IOException
,没有RuntimeException
等。
这样做可以:
throw new Exception("Something went wrong!")
但这些不会:
throw new IOException("Something went wrong!")
throw new RuntimeException("Something went wrong!")
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.Exception
的错误信息。 - cowlinator我可以像这样使其以ABORTED状态退出:
node('mynode') {
stage('Some stage') {
currentBuild.result = 'ABORTED'
error('Quitting')
}
}
但是,当将currentBuild.result设置为“SUCCESS”时,同样的方法不起作用。为了解决这个问题,您需要使用类似Pom12答案中的try-catch标志。
stage('sample') {
steps {
script{
try{
bat '''sample.cmd'''
RUN_SAMPLE_RESULT="SUCCESS"
echo "Intermediate build result: ${currentBuild.result}"
}//try
catch(e){
RUN_SAMPLE_RESULT="FAILURE"
echo "Intermediate build result: ${currentBuild.result}"
// normal error handling
throw e
}//catch
}//script
}//steps
}//stage
sys.exit()
或在Node中使用process.exit()
。 - Marco Roy