在groovy编译器异常的情况下,try / catch /最终掩盖Jenkinsfile的问题 [英] try/catch/finally masks Jenkinsfile problems in case of groovy compiler exceptions
问题描述
节点{
checkout scm $ b $我的代码类似于下面的代码b //做一些事情
try {
//做一些maven magic
} catch(error){
stage失败后清理
emailext attachLog:true ,body:Build failed(see $ {env.BUILD_URL}):$ {error},主题:[JENKINS] $ {env.JOB_NAME}失败,为:someone@example.com
抛出错误
} finally {
step $ class:'JUnitResultArchiver',testResults:'** / TEST - *。xml'
}
}
$ b如果上面的代码由于
中的一些jenkins-pipeline相关错误而失败,请尝试{}
(例如,使用未经批准的静态方法),脚本会自动失败。当我删除try / catch /最后我可以看到错误。
我做错了什么?不应该重新抛出错误
使管道错误出现在日志中?
编辑:
我'例如,当设法将问题确定为常规语法时我使用尚未分配的变量。
例如:
echo foo
如果foo
没有被声明/分配到任何地方Jenkins会失败构建,并且如果它在try / catch / finally里面,那么它将不会显示原因,它会抛出异常。
解决方案在
最后
块内或在catch内重新抛出
。在这些情况下,RejectedAccessException
被吞并,script-security
无法捕捉它。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' } }
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 rethrowingerror
make the pipeline errors appear in the log?EDIT: 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
Iffoo
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.解决方案This happens when an additional exception is thrown inside the
finally
block or before the re-throw insidecatch
. In these cases theRejectedAccessException
is swallowed andscript-security
does not catch it.这篇关于在groovy编译器异常的情况下,try / catch /最终掩盖Jenkinsfile的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文