3.6 Integrate Claude Code into CI/CD pipelines
Run Claude Code non-interactively in pipelines with -p, produce structured findings with --output-format json and --json-schema, keep re-runs free of duplicate comments, isolate review from generation, and feed CI project context through CLAUDE.md.
Key points
- 1
-p(--print) runs Claude Code non-interactively: it processes the prompt, writes the result to stdout and exits. Without it the process waits for interactive input and the CI job hangs. There is no--batchflag orCLAUDE_HEADLESSvariable. - 2
--output-format jsonreturns a JSON object withresult,session_idand usage metadata;stream-jsonemits newline-delimited events. Pair--output-format jsonwith--json-schema '<schema>'to get output that conforms to the schema in thestructured_outputfield, which a script can post as inline PR comments. Asking for JSON in the prompt is not enforcement. - 3
Piping works like any Unix tool:
gh pr diff 123 | claude -p "..." --output-format json. Use--append-system-promptto add reviewer instructions while keeping the default behaviour;--max-turnsand--max-budget-usdcap runaway jobs. - 4
--allowedToolspre-approves tools using permission-rule syntax (Bash(git diff *)); in a-prun a tool call that would otherwise prompt is denied, so listing only read-only tools makes a review job unable to modify the repository.--dangerously-skip-permissionsoryes |removes the guardrail, not just the prompt. - 5
--bareskips hooks, skills, MCP servers and CLAUDE.md for reproducible scripts, but do not use it when the job needs project context from CLAUDE.md. - 6
CLAUDE.md is the mechanism for giving CI-invoked Claude Code project context: testing standards, valuable-test criteria, fixture conventions and review criteria, versioned with the code so every repository applies the same standard.
- 7
Re-runs after new commits: fetch the findings already posted on the PR, include them in the prompt, and instruct Claude to report only new or still-unaddressed issues.
--continuedoes not help on ephemeral runners (sessions live on the machine that ran them) and exact-text deduplication cannot tell that a flagged line was fixed. - 8
Test generation: provide the existing test files in context so Claude does not propose scenarios already covered, and document testing standards and available fixtures in CLAUDE.md to reduce low-value tests. "More thorough" adjectives and extra turns do not close these gaps.
- 9
Session context isolation: the session that generated code is worse at reviewing its own changes because it carries the assumptions behind them. Run the review as a separate instance with fresh context that sees only the diff and the criteria (a second
claude -p, a reviewer subagent, or the Writer/Reviewer pattern). - 10
Claude Code GitHub Actions (
anthropics/claude-code-action) supports@claudementions and prompt-driven automation; pass CLI flags throughclaude_args, keep API keys in secrets, grant only the permissions the workflow needs, and define standards in CLAUDE.md. - 11
Batch versus real time (from the official samples): the Message Batches API suits overnight reports, not blocking pre-merge checks that developers wait on; results are correlated with
custom_id.
Read the source
Test yourself on 3.6 Integrate Claude Code into CI/CD pipelines
Ten questions, with the answer and explanation after each one.