我们正在开发一个脚本化的 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 记录结果并发现一些失败时,它都会将阶段和构建设置为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 块。
提前致谢!!:)