Jenkinsfile如何在并行分支中捕获多个异常

时间:2017-08-08 19:36:32

标签: jenkins groovy

我有一个jenkins脚本,由jenkins管道工作使用:

try{
    parallel branch1: {
         //some function that may throw exception
    }, branch2: {
         //some function that may throw exception
    }, branch3: {
         //some function that may throw exception
    }
}catch(Exception e){
    //gather the error information together (if there 1 error then just it, if there are 2 errors then concatenate 2 errors together, etc) and print it. 
}

我该怎么做?或者我不能这样做?

1 个答案:

答案 0 :(得分:0)

假设您可以以某种方式将错误捕获到变量中,捕获输出并将其存储到结束阶段。然后,您所要做的就是将每个阶段的构建结果设置为成功,以防止作业过早失败。有关如何执行此操作,请参阅this question。最后,在最后连接变量。

最终结果可能看起来像(半伪代码);

try{
    parallel branch1: {
         //some function that may throw exception
         error1=$(echo error)
         currentBuild.result = 'SUCCESS'
         return
    }, branch2: {
         //some function that may throw exception
         error2=$(echo error)
         currentBuild.result = 'SUCCESS'
         return
    }, branch3: {
         //some function that may throw exception
         error3=$(echo error)
         currentBuild.result = 'SUCCESS'
         return
    }
}catch(Exception e){
    //gather the error information together (if there 1 error then just it, if there are 2 errors then concatenate 2 errors together, etc) and print it. 
    print $error1 + $error2 + $error3
}

我甚至可以在try / catch之外使用它,只需依靠设置currentBuild.result来处理错误。