从 Jenkins 管道获取失败原因 [英] Get failure reason from Jenkins pipeline
问题描述
我正在为 Jenkins 设置一个声明性管道.在我的 post
部分,我使用 slackSend
来通知我的团队构建已损坏.我想包括失败的原因.这在 env
或 currentBuild
或其他东西中可用吗?我没有在文档中看到任何内容,但似乎是一个常见的用例
I'm setting up a declarative pipeline for Jenkins. In my post
section, I am using slackSend
to notify my team that the build is broken. I'd like to include the failure reason. Is this available in env
or currentBuild
or something else? I haven't seen anything in the documentation, but seems like a common use case
我已经看过一些关于使用 currentBuild.rawBuild.getLog(10)
的帖子,并且这些帖子很有效,但其中包含的信息太多了.我需要将实际异常归零
I've seen some posts about using currentBuild.rawBuild.getLog(10)
and that works, but it is just filled with way too much information. I need to zero in on the actual exception
推荐答案
另一种方法是使用catchError
或至少 a try/catch代码>
.
然后,作为in this answer,你可以得到错误信息:String error = "${e}";
Another approach is to use a catchError
or at least a try/catch
.
Then, as in this answer, you can get the error message: String error = "${e}";
关于 catchError,您可以将每个可能失败的步骤包装到 catchError
函数中.如果发生错误,它会将 build.result
设置为 FAILURE,然后继续 构建.
Regarding catchError, you would wrap every step that can potentially fail into a catchError
function. If an error occurs, it will set build.result
to FAILURE, and continue the build.
参见 catchError
,它指出只有 try/catch
方法可能有助于捕获实际错误 e
(及其字符串).
然后,您可以将该错误字符串添加到全局变量中,您的 post
步骤随后可以访问该变量.
这将比 currentBuild.rawBuild.getLog(10)
更简洁和/或更精确.
See catchError
, which points out that only the try/catch
approach might be useful to catch the actual error e
(and its string).
You might then add that error string to a global variable, that your post
step could then access.
That would be less verbose and/or more precise than currentBuild.rawBuild.getLog(10)
.
这篇关于从 Jenkins 管道获取失败原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!