You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins声明式流水线:无硬编码阈值时覆盖率下降触发构建失败

Jenkins Declarative Pipeline: Fail on Coverage Drop Without Hardcoded Thresholds

Got it, let's tackle this problem—you want to fail builds when coverage drops without hardcoding fixed percentage thresholds, right? I’ve worked through similar scenarios with both the Cobertura plugin and the Coverage API plugin, so here’s how to set this up in your declarative Jenkinsfile:

Using the Cobertura Plugin

Your existing Cobertura config was close—you just need to add two key flags to enable comparison against the previous successful build and fail on coverage decreases. Here’s the updated snippet:

cobertura(
    coberturaReportFile: 'coverage/cobertura-coverage.xml',
    enableNewApi: true,
    autoUpdateHealth: true, // Auto-updates health thresholds based on historical data
    autoUpdateStability: true, // Auto-updates stability thresholds based on historical data
    failUnstable: true, // Fails if coverage drops below auto-updated unstable threshold
    failUnhealthy: true, // Fails if coverage drops below auto-updated unhealthy threshold
    failNoReports: true,
    onlyStable: false,
    comparePrevious: true, // Pulls coverage data from the last successful build as baseline
    failIfCoverageDecreased: true // Triggers failure if current coverage is lower than baseline
)

How this works:

  • comparePrevious: true tells the plugin to use the last successful build’s coverage as the reference point.
  • failIfCoverageDecreased: true marks the build as failed the moment current coverage falls below that baseline.
  • The auto-update flags eliminate the need for hardcoded percentages—thresholds adjust dynamically based on your project’s coverage trends over time.

Using the Coverage API Plugin

For the Coverage API, your existing config handles pull request-specific checks, but you can add a baseline block to enforce coverage drop failures for all builds (not just PRs). Here’s the expanded snippet:

publishCoverage(
    failUnhealthy: true,
    failNoReports: true,
    calculateDiffForChangeRequests: true, // Retains your PR-specific coverage checks
    failBuildIfCoverageDecreasedInChangeRequest: true, // Retains PR failure rule
    // Add baseline comparison for all non-PR builds
    baseline: [
        baselineType: 'LAST_SUCCESSFUL', // Uses last successful build as coverage reference
        compareTarget: 'BUILD',
        failIfCoverageDecreased: true // Fails build if coverage drops below baseline
    ],
    adapters: [ coberturaAdapter(path: 'coverage/cobertura-coverage.xml') ]
)

How this works:

  • The baseline block establishes a dynamic reference using the most recent successful build’s coverage.
  • failIfCoverageDecreased: true in the baseline section ensures any coverage drop (vs the reference) triggers a build failure.
  • Your original PR-specific rules stay intact, so this config covers both regular branch builds and pull requests.

Quick Notes:

  • The first build won’t fail—there’s no baseline to compare against. Subsequent builds will use the last successful build as the reference.
  • If you want to target specific metrics (like line coverage or branch coverage), you can add filters. For example, in the Coverage API adapter, add metrics: ['LINE', 'BRANCH'] to focus only on those metrics.

内容的提问来源于stack exchange,提问作者Mr.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 09:57:31