当我们使用嵌套的 try-catch 块时,如何终止 jenkins 管道中的执行? [英] How can I terminate execution in jenkins pipeline when we use nested try-catch blocks?

查看:45
本文介绍了当我们使用嵌套的 try-catch 块时,如何终止 jenkins 管道中的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用嵌套的 try-catch 块来定义 jenkins 管道.在执行时,如果我在父 try-catch 块中有另一个 try-catch 块并且子 try-catch 块中出现问题,它将跳转到子 catch 块然后再次继续执行父级中的代码try-catch 块.

I'm using a nested try-catch block to define a jenkins pipeline. At the execution time, if I have another try-catch block in the parent try-catch block and something goes wrong in the child try-catch block, it will jump to the child catch block then again will continue executing the code in the parent try-catch block.

我已尝试设置 currentBuild.result='Failure'错误发生错误",但它仍然会继续执行.我希望管道状态为失败并终止其余代码的执行.

I have tried setting the currentBuild.result='Failure' and error "Error occurred" but still, it will proceed with the execution. I want the pipeline status to be a failure and terminate the execution of the rest of the code.

try{
    stage('stage1'){
        //do something
    }

    try{
        stage('stage2'){
            //do something
        }

    }catch(Exception err1){
        error "Error Occurred"
        currentBuild.result='Failure'
    }



}catch(Exception ex){
    // Do something if stage 1 fails 
}

如果第 2 阶段失败,则不应跳转到第 1 阶段的 catch 语句.有人可以建议我实现这一目标的好方法吗?

If the stage 2 fails, it shouldn't jump to the stage 1's catch statement. Can someone please suggest me a good way to achieve this ?

推荐答案

也应该回答您的问题.

This should also answer your question.

设置 currentBuild.result = 'Failure' 后的单个返回应该适合您.注意,return 是在 stage 之外的,否则它会直接退出 stage.

A single return after setting currentBuild.result = 'Failure' should work for you. Take care, that the return is outside of a stage, or else it will just exit the stage.

这篇关于当我们使用嵌套的 try-catch 块时,如何终止 jenkins 管道中的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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