2017-08-08 16 views
2

我有詹金斯腳本,它使用的詹金斯流水線作業:Jenkinsfile如何捕捉多個異常並行分支

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. 
} 

我怎樣才能做到這一點?或者我不能這樣做?

回答

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 s到處理錯誤。