Skip to content
Latchkey

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 50

Sonar 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.go

Gotchas

  • jscpd exits 1 only when --threshold is set and exceeded; without it the run always passes.
  • Tune --min-tokens so small idiomatic blocks are not flagged as duplicates.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →