在Jenkins工作流程中捕获多个错误 [英] Catching multiple errors in Jenkins workflow

查看:479
本文介绍了在Jenkins工作流程中捕获多个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作流在使用try-catch失败时发送邮件.我还启用了并发功能,与此同时,当同一工作流的多个作业进入限制阶段时,新作业将取消较旧的作业.这将引发"org.jenkinsci.plugins.workflow.steps.FlowInterruptedException"异常,并且取消的作业也会触发邮件通知.

My workflow sends mails when it fails using a try-catch. i have also enable concurrency and with this, when multiple jobs of the same workflow enters into a throttling stage, the new ones cancels the older ones. This throw an exception of "org.jenkinsci.plugins.workflow.steps.FlowInterruptedException" And canceled jobs also triggers the mail notification.

现在,我已经修改了工作流程,以捕获特定的FlowInterruptedException异常并取消邮件通知,并让其他任何东西触发邮件,就像这样.

Now i have modified my workflow to catch the specific FlowInterruptedException exception and suppress the mail notice and let anything else to trigger the mail, like so.

node {
try {
// some stages for the workflow
}

catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e){

        echo "the job was cancelled or aborted"
         }

 catch (err){ 
         stage 'Send Notification' 
         mail (to: 'adminj...@somename.com', 
         subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.", 
              body: "Some text", 
            mimeType:'text/html'); 
         currentBuild.result = 'FAILURE' 
     } 

}

这仅捕获FlowInterruptedException,并且当由于任何其他原因(命令键入错误等)导致作业真正失败时,我期望它会被其他捕获捕获,并触发其中的代码来发送邮件.但事实并非如此.

This is catching only the FlowInterruptedException and when the job really fails due to any other reason (command typo, etc), i was expecting it will be caught by the other catch and will trigger the code inside it to send the mail. But it isn't.

我认为我的代码在try catch中有一些缺陷.有什么主意吗?

I think my code have some flaw in the try catch. Any idea?

更新:

以防万一,如果我使用下面的代码,它只会发送几乎所有失败的邮件

Just incase, if i use the below code it just send mail for just about any failures

node {
try {
// some stages for the workflow
}

catch (err){ 
         stage 'Send Notification' 
         mail (to: 'adminj...@somename.com', 
         subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.", 
              body: "Some text", 
            mimeType:'text/html'); 
         currentBuild.result = 'FAILURE' 
     } 

}

推荐答案

您可以像现在一样捕获FlowInterruptedException-然后检查其原因之一(FlowInterruptedException#getCauses())是org.jenkinsci.plugins.workflow.support.steps.StageStepExecution.CanceledCause,这意味着等待进入stage步骤时,流程被中断.

You can catch FlowInterruptedException - as you are doing now - and then check one of its causes (FlowInterruptedException#getCauses()) is org.jenkinsci.plugins.workflow.support.steps.StageStepExecution.CanceledCause, which means that the flow was interrupted while waiting to enter a stage step.

任何其他组合都是有资格发送通知电子邮件的合法错误.

Any other combination is a legitimate error eligible to send the notification email.

这篇关于在Jenkins工作流程中捕获多个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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