确定Jenkins脚本管道中的失败阶段 [英] Determine Failed Stage in Jenkins Scripted Pipeline

查看:268
本文介绍了确定Jenkins脚本管道中的失败阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通用方法来确定Jenkins脚本化管道的末尾失败阶段的名称.

I am looking for a generic way to determine the name of the failed stage at the end of a Jenkins scripted Pipeline.

请注意,这与在Jenkins声明式管道中确定失败阶段关于声明性管道.

Please note that this is different than Determine Failed Stage in Jenkins Declaritive Pipeline which is about declarative pipeline.

还请注意,在每个阶段中使用try/catch是毫无疑问的,因为这会使流水线脚本无法读取.这是因为我们有10-15个阶段,它们存储在多个文件中,并且使用JJB进行编译以创建最终的管道脚本.它们已经很复杂,所以我需要一种干净的方法来确定哪个阶段失败了.

Also note that use of try/catch inside each stage is out of question because it would make the pipeline script impossible to read. This is because we have like 10-15 stages which are stored in multiple files and they are compiled using JJB to create the final pipeline script. They are already complex so I need a clean approach on finding which stage failed.

推荐答案

U还可以在共享库super_stage中创建自定义步骤

U could also create a custom step in a shared library, a super_stage

简单示例:

 // vars/super_stage.groovy
 def call(name, body) {
     try {
         stage(name) {
             body()
         }   
     } catch(e) {
         register_failed_stage(name, e)       
         throw e                                                                                            
     }   
 }

通过这种方式,您可以重用"相同的异常处理程序.

In that way you can 'reuse' the same exception handler.

在脚本化管道中,您将像这样使用它:

In your scripted pipeline you would then use it like:

super_stage("mystage01") {
    //do stuff
}

来源

这篇关于确定Jenkins脚本管道中的失败阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆