How to Fail CI on Code Duplication
Add a duplicated_lines_density condition to the Sonar gate, or run jscpd standalone with a max threshold that exits non-zero.
For a Sonar-based flow, set a duplicated lines density condition. For a standalone check, run jscpd with --threshold so the command fails the job when duplication is too high.
Standalone jscpd
.github/workflows/ci.yml
jobs:
duplication:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npx jscpd src --threshold 3 --reporters console --min-tokens 50Sonar gate condition
sonar-project.properties
# In the quality gate on New Code:
# Duplicated Lines (%) is greater than 3.0 -> gate fails
sonar.cpd.exclusions=**/generated/**,**/*.pb.goGotchas
- jscpd exits 1 only when
--thresholdis set and exceeded; without it the run always passes. - Tune
--min-tokensso small idiomatic blocks are not flagged as duplicates.
Related guides
How to Set Coverage, Duplication, and Maintainability Thresholds in a Quality GateDefine quality-gate conditions in SonarQube for minimum coverage, maximum duplicated lines, and a maintainabi…
How to Exclude Generated Code From Quality AnalysisKeep generated files, vendored code, and build output out of a SonarQube scan with sonar.exclusions so metric…