'jenkins declerative pipeline - fail build when coverage drops

using declerative pipeline syntax in a Jenkinsfile and publishing coverage report using cobertura as follows

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:

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.



Solution 1:[1]

enabling autoUpdateHealth in conjunction with hard-coded threshold would do the trick

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',
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mr.