Claude Code in CI/CD pipelines
What this covers
- Run non-interactively without hanging the pipeline
- Emit machine-parseable structured findings
- Deduplicate re-run findings with a stable fingerprint assigned by the posting code
- Explain why a fresh instance reviews a change better than the session that produced it
- Explain why a hung run costs more than a fast failure, in billed minutes and in a blocked merge queue
- Use
--json-schemato validate a run output, and know it does not stabilize identifiers across runs - Commit the criteria a CI run needs, because it starts from a clean checkout and cannot see a developer's user-level configuration
Key terms
- Command line (CLI)
- A text-based way to run programs and work with files.
- JSON
- A text format that stores structured data as objects, lists, strings, numbers, and other basic values.
- JSON Schema
- A set of rules that describes the required shape and allowed values of JSON data.
Non-interactive by default
-p (or --print) runs Claude Code without an interactive session. Omit it in CI and the process can sit waiting for input that will never arrive — a hung pipeline rather than a failed one, which is worse.
Worse in money and in other people's time. Continuous integration (CI) runners are billed by the minute: a run that errors in forty seconds costs forty seconds, and one that waits out a 30-minute timeout costs thirty minutes. In a merge queue, changes are tested one at a time in the order they will enter the main branch. Nothing behind a stuck job can start until that job ends, so every waiting change is delayed too.
Structured output
--output-format json with --json-schema produces machine-parseable output. That's what lets findings be posted automatically as inline PR comments, instead of being scraped out of prose with a regular expression that breaks the first time phrasing changes.
claude -p "Review this diff." --output-format json --json-schema review.json--json-schema is available in print mode, which a CI run already uses, and it validates the finished run's output against the schema you supply. Asking for JSON in the prompt is a request; a schema is a check. Know its limit too: a schema guarantees a finding has an identifier field, not that the same finding gets the same identifier on the next run.
CI has no context unless you give it
A CI-invoked run doesn't carry what a developer carries. It starts from a clean checkout with only what the repository hands it, so anything in a developer's ~/.claude/ is invisible to it. CLAUDE.md is the mechanism for supplying context: testing standards, fixture conventions, review criteria, committed where the run will read them.
Documenting what makes a test valuable, and which fixtures exist, measurably reduces low-value generated tests. Supplying the existing test files stops generation from proposing scenarios the suite already covers.
Re-runs and duplicate comments
When a review re-runs after new commits, prior findings help Claude focus on what is new or still unaddressed, but that prompt is not a deterministic deduplicator. Have the posting code assign a stable fingerprint from fields such as rule, repository path, and source location. Update or suppress an existing comment when the fingerprint matches; use semantic text comparison only as a fallback.
Why an independent reviewer
Checking your own arithmetic tends to reproduce the same slip, because you retrace the steps that produced it. Someone who didn't do the sum reads the digits that are actually on the page.
The session that generated a change is less effective at reviewing it than a fresh instance. It carries the reasoning that produced the code, including whatever assumption caused the defect. A separate review instance approaches the diff without that commitment.
-p / --print--output-format json--json-schemaCLAUDE.md
Field note — common misconceptions
- MythThat the session that wrote the code is well placed to review it
- ActuallyIt carries the reasoning that produced the defect; a fresh instance reads the diff without it.
- MythThat structured output can be reliably parsed out of prose
- Actually
--output-format jsonwith--json-schemavalidates the output instead of scraping it. - MythThat CI has the project context a developer has
- ActuallyA run starts from a clean checkout, so criteria must be committed where the pipeline reads them.
Guided review
Review this lesson as a study deck
Review the lesson's main ideas in five guided slides, then test yourself with three flashcards.
Open Task 3.6 study deckCross-domain reasoning
Connect this idea
Evaluation continues through delivery
Clear criteria, examples, repeated testing, continuous integration, independent review, and human review all provide evidence that a change works as intended.
- 3.5 · Apply iterative refinement techniques for progressive improvement
- 4.1 · Design prompts with explicit criteria to improve precision and reduce false positives
- 4.2 · Apply few-shot prompting to improve output consistency and quality
- 4.6 · Design multi-instance and multi-pass review architectures
- 5.5 · Design human review workflows and confidence calibration
Applied practice
Practice this lesson in a lab
Use a related lab to create a decision, implementation or diagram, evidence record, and review.
- Lab 7 · CI review with a strict output contract
Make a review job distinguish a clean diff from malformed output, execution failure, and invalid evidence.