jenkins declerative pipeline -覆盖率下降时构建失败

h43kikqp  于 2023-06-21  发布在  Jenkins
关注(0)|答案(2)|浏览(136)

Jenkinsfile中使用声明性管道语法,并使用cobertura发布覆盖报告,如下所示

cobertura(
  coberturaReportFile: 'coverage/cobertura-coverage.xml', 
  enableNewApi: true,
  autoUpdateHealth: true,
  autoUpdateStability: true,
  failUnstable: true,
  failUnhealthy: true,
  failNoReports: true,
  onlyStable: false
)

我也尝试使用code coverage api,如下所示:

publishCoverage(
  failUnhealthy: true, 
  calculateDiffForChangeRequests: true,
  failBuildIfCoverageDecreasedInChangeRequest: true,
  failNoReports: true,
  adapters: [
    coberturaAdapter(path: 'coverage/cobertura-coverage.xml')
  ]
)

查看我能找到的所有文档,如果不使用硬编码的阈值,我就无法计算出what are the instructions to fail the build if coverage drops
会很感激一个参考或代码片段。

kx7yvsdv

kx7yvsdv1#

结合硬编码阈值启用autoUpdateHealth可以达到这个目的

cobertura(
  coberturaReportFile: 'coverage/cobertura-coverage.xml', 
  enableNewApi: true,
  autoUpdateHealth: true,
  autoUpdateStability: true,
  failUnstable: true,
  failUnhealthy: true,
  failNoReports: true,
  onlyStable: false
  conditionalCoverageTargets: '80, 0, 0',
  fileCoverageTargets: '80, 0, 0',
  lineCoverageTargets: '80, 0, 0',
  methodCoverageTargets: '80, 0, 0',
  packageCoverageTargets: '80, 0, 0',
)
of1yzvn4

of1yzvn42#

下面是我们用来在覆盖率下降时使构建失败的代码,关键是将 failBuildIfCoverageDecreasedInChangeRequestapplyThresholdRecursively 设置为true:

def coverage = ['applyThresholdRecursively':true, 'failBuildIfCoverageDecreasedInChangeRequest':true, /* ... etc. */]
coverage.globalThresholds = [[failUnhealthy: false, thresholdTarget: 'File', unhealthyThreshold: 1.0, unstableThreshold: 0.0]] //use your own values here
def coverageFilePath = 'path-to-your-coverage-file'

publishCoverage( 
 adapters: [coberturaAdapter(mergeToOneReport: true, path: coverageFilePath)],
 applyThresholdRecursively: coverage.applyThresholdRecursively, 
 failBuildIfCoverageDecreasedInChangeRequest: coverage.failBuildIfCoverageDecreasedInChangeRequest, 
 failNoReports: coverage.failNoReports,
 failUnhealthy: coverage.failUnhealthy,
 failUnstable: coverage.failUnstable,
 globalThresholds: coverage.globalThresholds 
)

相关问题