OK9*_*999 5 groovy jenkins jenkins-groovy jenkins-pipeline
我的工作流程在使用 try-catch 失败时发送邮件。我还启用了并发性,这样,当同一工作流程的多个作业进入限制阶段时,新作业会取消旧作业。这会引发异常"org.jenkinsci.plugins.workflow.steps.FlowInterruptedException"并取消作业也会触发邮件通知。
现在我修改了我的工作流程以捕获特定的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'
}
}
Run Code Online (Sandbox Code Playgroud)
这仅捕获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'
}
}
Run Code Online (Sandbox Code Playgroud)
您可以捕获FlowInterruptedException- 正如您现在所做的 - 然后检查其原因之一 ( FlowInterruptedException#getCauses()) 是org.jenkinsci.plugins.workflow.support.steps.StageStepExecution.CanceledCause,这意味着流程在等待进入步骤时被中断stage。
任何其他组合都是合法错误,有资格发送通知电子邮件。
| 归档时间: |
|
| 查看次数: |
12741 次 |
| 最近记录: |