【问题标题】:How to handle unstable JUnit results in parallel stages in a scripted Jenkinsfile?如何在脚本化的 Jenkinsfile 的并行阶段处理不稳定的 JUnit 结果?
【发布时间】: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 块。

提前致谢!! :)

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案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 的特定情况,它将允许您满足您的要求。

    【讨论】:

      猜你喜欢
      • 2018-03-31
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多