简体   繁体   English

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

[英]jenkins declerative pipeline - fail build when coverage drops

using declerative pipeline syntax in a Jenkinsfile and publishing coverage report using cobertura as followsJenkinsfile中使用 declerative 管道语法并使用cobertura发布覆盖率报告如下

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

also tried using code coverage api as follows:还尝试使用代码覆盖率 api,如下所示:

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

looking at all the documentation i could find, i wasn't able to figure out what are the instructions to fail the build if coverage drops without using hard-coded thresholds.查看我能找到的所有文档,我无法弄清楚如果覆盖率下降而不使用硬编码阈值,构建失败的说明是什么

would appreciate a reference or a code snippet.将不胜感激参考或代码片段。

enabling autoUpdateHealth in conjunction with hard-coded threshold would do the trick结合硬编码阈值启用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',
)

Here's the code we use to fail the build when coverage drops, key is setting failBuildIfCoverageDecreasedInChangeRequest and applyThresholdRecursively to true:这是我们用来在覆盖率下降时构建失败的代码,关键是将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 
)

在此处输入图像描述

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

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