【问题标题】:How can I terminate execution in jenkins pipeline when we use nested try-catch blocks?当我们使用嵌套的 try-catch 块时,如何终止 jenkins 管道中的执行?
【发布时间】:2017-05-16 12:30:47
【问题描述】:

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

我已尝试设置 currentBuild.result='Failure'error "Error occurred" 但仍然会继续执行。我希望管道状态为失败并终止其余代码的执行。

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 语句。有人可以建议我实现这一目标的好方法吗?

【问题讨论】:

    标签: jenkins groovy try-catch


    【解决方案1】:

    This 也应该回答你的问题。

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

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多