如何使用 Jacoco 和 Jenkins Pipeline 更改构建状态?

How can the build status be changed using Jacoco with Jenkins Pipeline?

使用具有以下配置的 Jenkins Jacoco 插件版本 3.0.1 会生成有效的覆盖率报告,但实际上无法更改构建状态。

                jacoco( 
                    execPattern: '**/target/code-coverage/**.exec',
                    classPattern: '**/target/classes',
                    sourcePattern: '**/src',
                    inclusionPattern: 'com/company/**',
                    changeBuildStatus: true,
                    minimumInstructionCoverage: '70'
                )

记录的输出是

[JaCoCo plugin] Overall coverage: class: 19, method: 9, line: 4, branch: 2, instruction: 3
[JaCoCo plugin] Health thresholds: JacocoHealthReportThresholds [minClass=0, maxClass=0, minMethod=0, maxMethod=0, minLine=0, maxLine=0, minBranch=0, maxBranch=0, minInstruction=0, maxInstruction=0, minComplexity=0, maxComplexity=0]
[JaCoCo plugin] Apply Min/Max thresholds result: SUCCESS

"Apply Min/Max thresholds"行表示注册了changeBuildStatus标志,但是阈值都显示为0。

changeBuildStatus 是否真的适用于管道,如果适用,设置阈值的正确格式是什么?

对我来说,我需要添加相应的 maximumCoverage 值。例如,

jacoco(
    execPattern: '**/target/code-coverage/**.exec',
    classPattern: '**/target/classes',
    sourcePattern: '**/src',
    inclusionPattern: 'com/company/**',
    changeBuildStatus: true,
    minimumInstructionCoverage: '70',
    maximumInstructionCoverage: '80'
)

希望这能解决您的问题。

有关更多 JaCoCo 插件字段...https://jenkins.io/doc/pipeline/steps/jacoco/ 有一个可接受的参数列表。