Task 3.4
CI/CD Integration
Non-interactive Claude Code in pipelines with session isolation
📘What You Need to Know
Running Claude Code in CI Pipelines
Claude Code is not just an interactive IDE tool — it runs in CI/CD pipelines for automated code review, test generation, PR analysis, and documentation checks. CI environments have no human at the keyboard, so you need non-interactive mode and machine-parseable output.
The foundation is the -p flag, which enables print/non-interactive mode: pipe in a prompt, get a response, exit. Without -p, Claude Code waits for interactive input and your pipeline hangs indefinitely. Every CI integration starts with -p.
The -p flag is REQUIRED for CI/CD. Without it, Claude Code expects interactive input and the pipeline will hang.
Structured Output with --output-format json
CI pipelines need to act on Claude's output programmatically — block a merge, post a PR comment, or trigger a follow-up job. Free-text responses require fragile regex parsing and break when Claude's formatting varies. The --output-format json flag returns structured JSON that your pipeline can reliably parse.
Combine -p with --output-format json for the standard CI pattern: claude -p "Review this PR for security issues" --output-format json. Your pipeline reads the JSON, checks for critical findings, and gates the merge accordingly.
# GitHub Actions example
- name: AI Code Review
run: |
claude -p "Review this PR for: SQL injection, missing error handling, API breaking changes. Return JSON array of findings with severity, file, line, description." --output-format json > review.jsonSession Isolation: The Critical Anti-Pattern
The most commonly tested anti-pattern in Domain 3 is same-session self-review. If Claude generates code in Session A and then reviews that same code in Session A (using --resume), it retains the reasoning context from generation. It already "knows" the code is correct because it wrote it — this causes confirmation bias and systematically missed bugs.
The fix is strict session isolation: generation runs in Session A; review runs in a fresh Session B with only the code diff as input. The reviewer has no memory of why design decisions were made, forcing genuine independent analysis.
# WRONG — same session self-review (confirmation bias)
claude -p "Write the authentication module"
claude --resume -p "Review your code for security issues"
# CORRECT — separate sessions
claude -p "Write the authentication module" # Session A (generator)
claude -p "Review this diff for security issues" # Session B (fresh reviewer)Same-session self-review is a CRITICAL anti-pattern. The exam tests this frequently. Always use separate fresh sessions for generation and review.
Designing Effective CI Prompts
Vague CI prompts like "review this code" produce generic, surface-level feedback. The model does not know what to focus on, so it gives broad, unhelpful comments. Effective CI prompts specify exactly what to check and how to format output.
Be explicit: "Check for: SQL injection in database queries, proper error handling in API routes, missing input validation at boundaries. Output as JSON with fields: severity (critical/high/medium/low), file, line, description." Specific criteria produce actionable findings your pipeline can act on.
End-to-End CI Pipeline Pattern
A production CI integration typically has three stages: (1) a generation or analysis step using claude -p with a focused prompt, (2) a separate review step in a fresh session if Claude generated the code being reviewed, and (3) a gate step that parses --output-format json output and blocks the merge if critical findings exist.
Store the JSON output as a CI artifact for audit trails. Post findings as PR comments using your CI platform's API. This creates a consistent, automated review layer that applies the same thorough checks on every PR, unlike human reviewers who may miss things when tired.
⚠️Exam Traps
Trap: Using --resume to review code generated in the same session
Why it's wrong: Same session retains reasoning context from generation, causing confirmation bias — the reviewer approves its own work.
Trap: Running Claude Code in CI without the -p flag
Why it's wrong: Without -p, Claude Code waits for interactive input and the CI pipeline hangs.
Trap: Vague CI prompts like "review this code for quality"
Why it's wrong: Unspecified criteria produce generic, unhelpful feedback that cannot gate merges reliably.
Trap: Parsing free-text Claude output with regex in CI
Why it's wrong: Free-text formatting varies between runs and breaks fragile parsing logic.
✓Concept Check
In a CI pipeline, Claude generates a new API module in Session A. How should the automated security review run?
🔨Build Exercise
Advanced50 minutesSet Up CI Code Review Pipeline
What you'll learn
- •Integrate Claude Code with -p and --output-format json
- •Implement session isolation between generation and review
- •Gate merges based on structured review findings