2

我们正在开发一个脚本化的 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 块。

提前致谢!!:)

4

1 回答 1

1

jUnit 插件执行将返回一个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'
}

您没有处理阶段状态(UNSTABLE),但对于您使用 jUnit 的特定情况,它将允许您满足您的要求。

于 2020-12-01T17:29:49.383 回答