【发布时间】:2018-06-28 03:02:00
【问题描述】:
在哪里放置 try/catch 以便它可以按预期工作,尤其是存在并行分支时? (BlueOcean 插件也有)
在 Jenkinsfile 的官方文档中,没有明确说明这个话题,但确实存在示例:
示例 1:Try{} 在阶段块内
Jenkinsfile (Scripted Pipeline)
node {
stage('Example') { //It's inside the stage block
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}
示例 2:Try{} 在节点块内
Jenkinsfile (Scripted Pipeline)
stage('Build') {
/* .. snip .. */
}
stage('Test') {
parallel linux: {
node('linux') {
checkout scm
try {
unstash 'app'
sh 'make check'
}
finally {
junit '**/target/*.xml'
}
}
},
windows: {
node('windows') {
/* .. snip .. */
}
}
}
但是我的实现是否有效?这是一个嵌套并行构建,try/catch{} 是最外层的块,见下图:
try{
parallel 'b0': {
node('parallel'){
....
}
}, 'b1': {
node('parallel'){
....
}
}, 'b2': {
parallel 'b2-0': {
node('parallel'){
....
}
}, 'b2-1': {
node('parallel'){
....
}
}, failFast: true
parallel 'anotherb0': {
node('parallel'){
.....
}
}, 'anotherb1': {
node('parallel'){
....
}
}, failFast: true
}, failFast: true
}catch(err){
print err
currentBuild.result = 'FAILURE'
}finally{
}
问题是,有时当其中一个分支出现故障时,整个构建继续进行,并且管道图在 BlueOcean 中保持绿色,但这不是我想要的;相反,我想让它整个构建失败,只要任何分支失败。我该怎么做?
【问题讨论】:
标签: jenkins groovy jenkins-pipeline