try/catch/finally 在 groovy 编译器异常的情况下掩盖 Jenkinsfile 问题 [英] try/catch/finally masks Jenkinsfile problems in case of groovy compiler exceptions
问题描述
我的 Jenkinsfile 中有类似于下面的代码:
I have code similar to the one below in my Jenkinsfile:
node {
checkout scm
// do some stuff
try {
// do some maven magic
} catch (error) {
stage "Cleanup after fail"
emailext attachLog: true, body: "Build failed (see ${env.BUILD_URL}): ${error}", subject: "[JENKINS] ${env.JOB_NAME} failed", to: 'someone@example.com'
throw error
} finally {
step $class: 'JUnitResultArchiver', testResults: '**/TEST-*.xml'
}
}
如果上述代码由于 try { }
中的一些与 jenkins-pipeline 相关的错误(例如使用未经批准的静态方法)而失败,则脚本会静默失败.当我删除 try/catch/finally 时,我可以看到错误.难道我做错了什么?不应该重新抛出 error
使管道错误出现在日志中吗?
If the above code fails because of some jenkins-pipeline related errors in the try { }
(e.g. using unapproved static method) the script fails silently. When I remove the try/catch/finally I can see the errors.
Am I doing something wrong? Shouldn't rethrowing error
make the pipeline errors appear in the log?
我已经设法将问题确定为 groovy 语法,例如我使用了一个尚未分配的变量.例子:<代码>回声富代码>如果 foo
没有在任何地方声明/分配,Jenkins 将失败构建,并且如果它位于重新抛出异常的 try/catch/finally 内,则不会显示原因.
I've managed to nail down the problem to groovy syntax, when e.g. I use a variable that hasn't been assigned yet.
Example:
echo foo
If foo
is not declared/assigned anywhere Jenkins will fail the build and won't show the reason if it is inside the try/catch/finally which rethrows the exception.
推荐答案
当在 finally
块内或在 catch
.在这些情况下,RejectedAccessException
被吞下,而 script-security
没有捕捉到它.
This happens when an additional exception is thrown inside the finally
block or before the re-throw inside catch
. In these cases the RejectedAccessException
is swallowed and script-security
does not catch it.
这篇关于try/catch/finally 在 groovy 编译器异常的情况下掩盖 Jenkinsfile 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!