Enforce minimum code coverage threshold in CI pipeline#7188
Enforce minimum code coverage threshold in CI pipeline#7188
Conversation
Add a coverage gate to the CodeCoverage_Upload stage that fails the build when total statement coverage drops below a configured minimum. - New Test-CodeCoverageThreshold.ps1 script parses 'go tool cover -func' output and fails if coverage is below the threshold - New MinimumCoveragePercent parameter on code-coverage-upload.yml (default 0 = disabled) - Set threshold to 80% in release-cli.yml to validate the mechanism (will be adjusted to the actual floor after verification) Fixes #7187 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an optional CI “coverage gate” to fail the CodeCoverage_Upload stage when total statement coverage drops below a configured minimum, using a new PowerShell threshold-check script.
Changes:
- Introduces
eng/scripts/Test-CodeCoverageThreshold.ps1to parsego tool cover -funcoutput and enforce a minimum percentage. - Extends the
code-coverage-upload.ymlstage template with aMinimumCoveragePercentparameter and conditional threshold-check step. - Configures
release-cli.ymlto passMinimumCoveragePercent: 80into the coverage stage.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| eng/scripts/Test-CodeCoverageThreshold.ps1 | New script to compute total coverage from cover.out and fail when below a threshold. |
| eng/pipelines/templates/stages/code-coverage-upload.yml | Adds a parameter and conditionally runs the threshold check during coverage merge/publish. |
| eng/pipelines/release-cli.yml | Enables the gate by setting a minimum coverage percent for the release-cli pipeline. |
Comments suppressed due to low confidence (1)
eng/pipelines/templates/stages/code-coverage-upload.yml:74
- If the threshold check fails (exits non-zero), subsequent steps won’t run, so
PublishCodeCoverageResultswon’t publish the report that would help diagnose the failure. Add an explicitcondition: succeededOrFailed()(or equivalent) to the publish task so coverage still gets uploaded even when the gate fails.
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.xml'
displayName: Publish Code Coverage to DevOps
You can also share your feedback on Copilot code review. Take the survey.
The previous approach nested 'pwsh -File' inside a pwsh inline step, which caused $CoverageFile to be treated as a literal string instead of the script parameter. Switch to PowerShell@2 task with targetType: filePath so arguments are passed correctly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
840e0e4 to
b925633
Compare
Double-quote the '-func=$CoverageFile' argument so PowerShell expands the variable. Without quotes, PowerShell passes the literal string '$CoverageFile' to the go command. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
go tool cover -func needs to resolve Go module paths from the coverage file. Run the threshold check from cli/azd/ where go.mod lives, and pass the absolute path to cover.out. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Set threshold to 48% to match current baseline - Add ValidateRange(0,100) on MinimumCoveragePercent parameter - Find total line by 'total:' prefix instead of assuming last line - Match both integer and decimal percentages with invariant culture parsing - Add condition: succeededOrFailed() to PublishCodeCoverageResults so coverage reports are still uploaded when the threshold check fails Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Azure Dev CLI Install InstructionsInstall scriptsMacOS/Linux
bash: pwsh: WindowsPowerShell install MSI install Standalone Binary
MSI
Documentationlearn.microsoft.com documentationtitle: Azure Developer CLI reference
|
Description
Adds a coverage gate to the CI pipeline that fails the build when total statement coverage drops below a configured minimum percentage.
Changes
eng/scripts/Test-CodeCoverageThreshold.ps1go tool cover -func, parses total %, fails if below thresholdeng/pipelines/templates/stages/code-coverage-upload.ymlMinimumCoveragePercentparameter; conditionally runs the threshold check stepeng/pipelines/release-cli.ymlMinimumCoveragePercent: 80to the coverage upload stageHow it works
cover.out, a new step runs the threshold scriptgo tool cover -func=cover.outand parses thetotal:line for the statement coverage percentageMinimumCoveragePercent > 0(default is 0 = disabled)Note
The threshold is intentionally set to 80% to validate the mechanism fails as expected (current coverage is ~48%). After confirming the pipeline behavior, adjust the value to lock in the actual floor.
Fixes #7187