如何在脚本 Jenkinsfile 的并行阶段处理不稳定的 JUnit 结果?
How to handle unstable JUnit results in parallel stages in a scripted Jenkinsfile?
我们正在开发一个脚本化的 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
,并且不会抛出异常。 如何查看阶段结果或一般处理?
在连续的情况下,这个答案就足够了:。在我们的例子中,将 finally
块添加到第一个 try
,结果为:
...
} finally {
if (currentBuild.currentResult == 'UNSTABLE') {
githubNotify status: 'FAILURE', context: 'test1', description: 'FAILURE'
}
}
但是由于我们是并行的 运行 个阶段,如果测试通过,我们仍然希望在进一步的阶段发送正确的通知,我们不能使用 currentBuild.currentResult
,因为一旦有一个阶段UNSTABLE
,后面的阶段都会进入if块。
提前致谢!! :)
jUnit插件执行会return一个TestResultSummary,你可以在其中查看测试失败的次数。您可以保存 jUnit 结果,如果出现故障,您将引发异常以被 catch 块捕获:
try {
githubNotify status: 'PENDING', context: 'test2', description: 'PENDING'
test2 execution
TestResultSummary summaryJUnit = junit
if(summaryJUnit.getFailCount() > 0) {
throw new Exception('Has tests failing')
}
githubNotify status: 'SUCCESS', context: 'test2', description: 'SUCCESS'
} catch (Exception e) {
githubNotify status: 'FAILURE', context: 'test2', description: 'FAILURE'
}
您没有处理阶段状态(不稳定),但对于您使用 jUnit 的特定情况,它可以满足您的要求。
我们正在开发一个脚本化的 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
,并且不会抛出异常。 如何查看阶段结果或一般处理?
在连续的情况下,这个答案就足够了:finally
块添加到第一个 try
,结果为:
...
} finally {
if (currentBuild.currentResult == 'UNSTABLE') {
githubNotify status: 'FAILURE', context: 'test1', description: 'FAILURE'
}
}
但是由于我们是并行的 运行 个阶段,如果测试通过,我们仍然希望在进一步的阶段发送正确的通知,我们不能使用 currentBuild.currentResult
,因为一旦有一个阶段UNSTABLE
,后面的阶段都会进入if块。
提前致谢!! :)
jUnit插件执行会return一个TestResultSummary,你可以在其中查看测试失败的次数。您可以保存 jUnit 结果,如果出现故障,您将引发异常以被 catch 块捕获:
try {
githubNotify status: 'PENDING', context: 'test2', description: 'PENDING'
test2 execution
TestResultSummary summaryJUnit = junit
if(summaryJUnit.getFailCount() > 0) {
throw new Exception('Has tests failing')
}
githubNotify status: 'SUCCESS', context: 'test2', description: 'SUCCESS'
} catch (Exception e) {
githubNotify status: 'FAILURE', context: 'test2', description: 'FAILURE'
}
您没有处理阶段状态(不稳定),但对于您使用 jUnit 的特定情况,它可以满足您的要求。