3.6 Claude Code in CI/CD pipelines
Non-interactive execution
Flag reference: CLI reference; pipeline patterns: common workflows.
# WRONG: hangs waiting for interactive input.
claude "Analyze this pull request for security issues"
# RIGHT: -p (or --print) processes the prompt, writes to stdout, exits.
claude -p "Analyze this pull request for security issues"
CLAUDE_HEADLESS=true, a --batch flag, and redirecting stdin from /dev/null are not the answer. -p / --print is the documented mechanism.
Structured output for automation
claude -p "$REVIEW_PROMPT" \
--output-format json \
--json-schema ./ci/review-findings.schema.json \
> findings.json
jq -c '.findings[]' findings.json | while read -r f; do
gh pr comment "$PR" --body "$(jq -r '.summary' <<<"$f")"
done
--output-format json with --json-schema yields machine-parseable structured findings suitable for posting as inline PR comments.
CLAUDE.md as the CI context channel
CI-invoked Claude Code has no human to ask. Document in CLAUDE.md:
- Testing standards and what counts as a valuable test
- Available fixtures and factory conventions
- Review criteria — what to flag and what to skip
This directly reduces low-value test output and improves generated-test quality.
Re-running reviews on new commits
Include prior review findings in context and instruct Claude to report only new or still-unaddressed issues. Without this, every re-run duplicates comments.
Likewise, provide the existing test files so test generation does not propose scenarios the suite already covers.
Session context isolation
The session that generated the code retains its reasoning context and is therefore less likely to question its own decisions. A separate instance without that context catches subtle issues that self-review instructions and extended thinking do not. See Domain 4.6.
Quiz
- Question 1
Your pipeline script runs claude "Analyze this pull request for security issues" and the job hangs indefinitely; logs indicate Claude Code is waiting for interactive input. What is the correct approach?
- Question 2
Your CI job posts Claude’s review as inline PR comments. The step that turns the response into comments is a Python script built on four regular expressions, and it silently drops findings whenever the model phrases a file location differently — "src/api.py line 40", "in src/api.py:40", "the handler in api.py". Reviewers only notice the loss when a bug ships. What is the correct fix?
- Question 3Select all that apply (2)
Your CI step runs claude -p with a review prompt. The reviews are accurate about the code but repeatedly recommend test patterns the team abandoned a year ago and fixtures that no longer exist; developers running the same prompt locally never see this. The CI job uses a sparse checkout containing only the changed files. Which two statements are correct? Select two.
- Question 4
Your review job re-runs on every push to a pull request. On a PR with six pushes, developers now see the same six findings repeated six times, including findings they have already addressed. What is the correct fix?