捕获Jenkins工作流中的多个错误
我的工作流在使用try-catch失败时发送邮件。我还启用了并发,当同一工作流的多个作业进入节流阶段时,新作业将取消旧作业。这会引发异常“org.jenkinsci.plugins.workflow.steps.FlowInterruptedException”,取消的作业也会触发邮件通知 现在我已经修改了我的工作流程,以捕获特定的捕获Jenkins工作流中的多个错误,jenkins,groovy,jenkins-pipeline,jenkins-groovy,Jenkins,Groovy,Jenkins Pipeline,Jenkins Groovy,我的工作流在使用try-catch失败时发送邮件。我还启用了并发,当同一工作流的多个作业进入节流阶段时,新作业将取消旧作业。这会引发异常“org.jenkinsci.plugins.workflow.steps.FlowInterruptedException”,取消的作业也会触发邮件通知 现在我已经修改了我的工作流程,以捕获特定的FlowInterruptedException异常,并抑制邮件通知,并让其他任何东西触发邮件,如下所示 node { try { // some stages fo
FlowInterruptedException
异常,并抑制邮件通知,并让其他任何东西触发邮件,如下所示
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
,当作业由于任何其他原因(命令输入错误等)确实失败时,我希望它会被另一个捕获捕获,并触发其中的代码来发送邮件。但事实并非如此
我认为我的代码在try-catch中有一些缺陷。有什么想法吗
更新:
以防万一,如果我使用下面的代码,它只会发送任何失败的邮件
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.stagesteExecution.CanceledCause
,这意味着流在等待进入阶段时被中断
任何其他组合都是合法的错误,有资格发送通知电子邮件。我在这里发现了一些问题。但我不会说这是一个解决办法。带有2个catch的代码段只要返回一些特定于hudson的异常(可能不是正确的术语,我不是java或groovy的人)就可以工作。但是,如果存在诸如NoSuchMethod异常之类的异常,则catch failsCatching only FlowInterruptedException似乎不明确,请参阅。