Yah*_*Raj 16 jenkins jenkins-plugins jenkins-pipeline
我用try catch块处理了Jenkins管道步骤.我想在某些情况下手动抛出异常.但它显示以下错误.
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.io.IOException java.lang.String
Run Code Online (Sandbox Code Playgroud)
我检查了scriptApproval部分,没有待批准的批准.
Pom*_*m12 28
如果要在异常时中止程序,可以使用管道步骤error来停止管道执行并显示错误.示例:
try {
// Some pipeline code
} catch(Exception e) {
// Do something with the exception
error "Program failed, please read logs..."
}
Run Code Online (Sandbox Code Playgroud)
如果要以成功状态停止管道,可能需要某种布尔值来指示必须停止管道,例如:
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.
}
Run Code Online (Sandbox Code Playgroud)
这就是我在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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎没有其他异常Exception可以抛出。不IOException,不RuntimeException,等等。
这将起作用:
throw new Exception("Something went wrong!")
Run Code Online (Sandbox Code Playgroud)
但是这些不会:
throw new IOException("Something went wrong!")
throw new RuntimeException("Something went wrong!")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29301 次 |
| 最近记录: |