【发布时间】:2020-11-23 18:07:55
【问题描述】:
我们正在开发一个脚本化的 Jenkinsfile,它可以并行和顺序地运行多个阶段。
我们有以下代码:
...
parallel {
stage('test1') {
try {
githubNotify status: 'PENDING', context: 'test1', description: 'PENDING'
test1 execution
junit
githubNotify status: 'SUCCESS', context: 'test1', description: 'SUCCESS'
} catch (Exception e) {
githubNotify status: 'FAILURE', context: 'test1, description: 'FAILURE'
}
}
stage('test2') {
try {
githubNotify status: 'PENDING', context: 'test2', description: 'PENDING'
test2 execution
junit
githubNotify status: 'SUCCESS', context: 'test2', description: 'SUCCESS'
} catch (Exception e) {
githubNotify status: 'FAILURE', context: 'test2', description: 'FAILURE'
}
}
}
...
问题是,每当JUnit记录结果并发现一些失败时,它会将stage和build设置为UNSTABLE,并且不会抛出异常。我们如何检查结果舞台还是一般处理?
在顺序情况下,这个答案就足够了:https://stackoverflow.com/a/48991594/7653022。在我们的例子中,将finally 块添加到第一个try 会导致:
...
} finally {
if (currentBuild.currentResult == 'UNSTABLE') {
githubNotify status: 'FAILURE', context: 'test1', description: 'FAILURE'
}
}
但是由于我们正在并行运行阶段,并且如果测试通过,我们仍希望在进一步的阶段发送正确的通知,我们不能使用currentBuild.currentResult,因为一旦有阶段UNSTABLE,以下所有阶段将进入 if 块。
提前致谢!! :)
【问题讨论】: