简体   繁体   English

当覆盖率太低时,如何使 Jenkins 阶段失败并变成红色(使用 C# 和 dotnet 测试)?

[英]How to make Jenkins stage fail and turn red when coverage is too low (using C# and dotnet test)?

I am using C#, coverlet.msbuild and the Jenkins Cobertura adapter.我正在使用 C#、coverlet.msbuild 和 Jenkins Cobertura 适配器。 I have roughly this in my Jenkinsfile:我的 Jenkinsfile 中大致有这个:

stage ('Run unit tests') {
    steps {
        powershell "dotnet test -c:Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-build --no-restore --logger trx"
    }
    post {
        always {
            step([$class: 'MSTestPublisher'])
            publishCoverage failUnhealthy: true, 
                globalThresholds: [[thresholdTarget: 'Package', unhealthyThreshold: 50.0]],
                adapters: [coberturaAdapter(
                    mergeToOneReport: true, 
                    path: '**/*.cobertura.xml')]
        }
    }
}

This makes my Jenkins build fail if coverage is below 50% at package level.如果包级别的覆盖率低于 50%,这会使我的 Jenkins 构建失败。 So far so good.到现在为止还挺好。

But when a build fails due to this, it is user-hostile and hard to see why.但是,当构建因此而失败时,就会对用户怀有敌意,并且很难看出原因。 The 'Run unit tests' stage is green in Blue Ocean. “运行单元测试”阶段在 Blue Ocean 中是绿色的。

Can I make this stage turn red when it fails the build, so it is easier to see what the error is?当构建失败时,我可以让这个阶段变成红色,这样更容易看到错误是什么吗?

Inspired by the answer from Sers and some other Jenkinsfile code I read through, I arrived at this solution, which does what I want:受到来自 Sers 的回答和我阅读的其他一些 Jenkinsfile 代码的启发,我找到了这个解决方案,它可以满足我的需求:

stage ('Run unit tests') {
    steps {
        powershell "dotnet test -c:Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-build --no-restore --logger trx"
    }
    post {
        always {
            step([$class: 'MSTestPublisher'])
            publishCoverage failUnhealthy: true, 
                globalThresholds: [[thresholdTarget: 'Package', unhealthyThreshold: 50.0]],
                adapters: [coberturaAdapter(
                    mergeToOneReport: true, 
                    path: '**/*.cobertura.xml')]
            script {
                if (currentBuild.result == 'FAILURE') {
                    error("Test coverage is too low.")
                }
            }
        }
    }
}

You can set currentBuild.result to FAILURE if publishCoverage is true.如果publishCoverage为 true,您可以将currentBuild.result设置为FAILURE currentBuild.displayName and currentBuild.description optional: currentBuild.displayNamecurrentBuild.description可选:

post {
    always {
        script {
            def failed = publishCoverage (failUnhealthy: true, 
                        globalThresholds: [[thresholdTarget: 'Package', unhealthyThreshold: 50.0]],
                        adapters: [coberturaAdapter(
                            mergeToOneReport: true, 
                            path: '**/*.cobertura.xml')])
            if (failed) {
                currentBuild.result = 'FAILURE'
                currentBuild.displayName = "${currentBuild.displayName} Coverage"
                currentBuild.description = "Coverage lower than 50%"
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM