Claude Code in CI/CD: -p Flag, JSON Output and Automated PR Review
👋 In plain words: Claude Code can review every pull request automatically inside your CI pipeline, which is great news for testers. But CI has no human to answer questions, so it must run non-interactively and produce output a script can read. This lesson shows the exact setup.
Running Claude Code in CI is where QA and automation engineers get the most value — and where the exam asks very practical questions about flags and review design (Task Statement 3.6).
🔑 Key terms
- CI pipeline: Automated jobs (GitHub Actions, Jenkins and so on) that run on every code change.
- Non-interactive mode: Claude Code takes one prompt, prints the result and exits. You turn it on with
-p. - JSON output: Machine-readable results, so a script can turn findings into PR comments.
- JSON schema: A template that fixes the exact shape of the JSON, for example file, line and severity.
- Independent reviewer: A separate Claude run that didn't write the code, so it reviews with fresh eyes.
📖 The story in this lesson
Ravi adds an AI reviewer to ShopNest's GitHub Actions. On its first day the job hangs for an hour. By the end of this lesson it posts clean, non-repeated comments on every PR.
💡 Think of it like this
Interactive mode is a phone call: Claude may ask you something and wait for your answer. Non-interactive mode is a letter: you send the full request, it sends back a full reply, and nobody waits on the line.
⚙️ Non-interactive mode
claude -p "Review the diff in $DIFF_FILE for bugs and security issues only" \
--output-format json \
--json-schema ./review-findings.schema.json
-p/--printruns non-interactively: process the prompt, print, exit. Without it, the job hangs waiting for input.--output-format jsonwith--json-schemagives machine-parseable findings you can post as inline PR comments.
CLAUDE_HEADLESS=true, --batch and redirecting stdin from /dev/null are distractors — not documented ways to run in CI.🌍 Real-world example: The job that hung for an hour
Ravi's first pipeline ran claude "review this", and Claude sat waiting for input that never came. Adding -p makes it process the prompt, print and exit. Adding JSON output with a schema gives findings his script can post as inline comments.
claude -p "Review the diff for bugs only" \
--output-format json \
--json-schema ./findings.schema.json⚙️ Give CI the project context
CI-invoked Claude Code reads the project CLAUDE.md. Document testing standards, what makes a test valuable, available fixtures and review criteria there. For test generation, include the existing test files so Claude doesn’t suggest scenarios you already cover.
🌍 Real-world example: Reviews that know the team's rules
The first reviews flagged ShopNest's custom test fixtures as 'unused'. Ravi documents the fixtures and review criteria in the project CLAUDE.md, which Claude Code also reads in CI. The false alarms stop.
🔍 Independent review instances
The session that generated code keeps its own reasoning context and is less likely to question its own decisions. Review generated code with a separate, independent Claude invocation.
🌍 Real-world example: Don't mark your own homework
When Claude generated a test and then reviewed it in the same session, it approved its own mistakes. Ravi runs the review as a separate Claude invocation that never saw the generation reasoning, and it catches two real bugs.
🔍 Re-running reviews after new commits
Include the earlier findings in context and ask Claude to report only new or still-unaddressed issues. Otherwise every push re-posts the same comments.
🌍 Real-world example: No more 14 duplicate comments
After each push, the reviewer reposted the same 14 comments. Ravi now passes the previous findings into the prompt and asks only for new or still-unfixed issues. Developers see one fresh comment instead of 14 repeats.
-p. The job waits for input that never comes and hangs until it times out.🛠️ Try it yourself (about 10 minutes)
- In a repository with a small change, run
claude -p "List possible bugs in this diff"with--output-format json. - Save the output to a file.
- Open the file and check it is valid JSON, for example with
python -m json.tool.
✅ You are done when: the command exits by itself and the saved file parses as JSON.
🧪 Exam-Style Practice Questions
Try these before opening the answers. They use the same scenario style as the real exam.
Question 1. A CI job running claude "review this" hangs. What's the fix?
- A.
CLAUDE_HEADLESS=true - B. Pipe in
yes - C.
claude -p "review this" - D.
claude --batch
Show answer
Answer: C. -p/--print is non-interactive mode. A and D don't exist. B is a hack that doesn't address the mode.
Question 2. After a developer pushes fixes, the re-run review repeats 14 already-resolved comments. What's the best fix?
- A. Include the prior findings in context and ask for only new or still-unaddressed issues
- B. Delete the old comments automatically
- C. Lower
max_tokens - D. Run reviews weekly
Show answer
Answer: A. Context lets Claude reconcile old and new findings. B loses history and may delete valid comments. D delays feedback. C truncates output.
Question 3. Your CI posts Claude's review findings as inline PR comments. What output configuration should you use?
- A. Markdown output
- B. Default text output parsed with regexes
- C.
--verbose - D.
--output-format jsonwith--json-schemadefining the findings structure
Show answer
Answer: D. Machine-parseable and schema-enforced. A and B are brittle. C is for debugging.
📝 Assignment
Do these tasks in order. Each one has a Done when check, so you know when it is complete. The stretch task is optional but close to what the exam expects from an architect. Keep your answers in one notes file or Git repository, and revisit them in your final week of revision together with the cheat sheet in the last lesson of this course.
- Task 1: Run claude
-pwith--output-formatjson on a sample diff and save the output.
✅ Done when: The command exits on its own and the saved file is valid JSON. - Task 2: Write a JSON schema for findings with file, line, severity, category and suggested fix, and use it with
--json-schema.
✅ Done when: The output validates against your schema, and every finding has a file, line and severity. - Stretch: add a GitHub Actions step that posts findings as PR comments and skips issues already reported.
✅ Done when: A second run after a new commit posts only new or still-open findings, with no duplicates.
❓ Frequently Asked Questions
🤔 Why does Claude Code hang in my CI job?
It is running interactively. Add -p (or --print) so it processes the prompt, prints and exits.
🤔 How do I get structured output from Claude Code in CI?
Use --output-format json together with --json-schema.
🤔 Should the same session review the code it generated?
No. An independent instance without the generator's context catches more issues.
🤔 How do I avoid duplicate review comments after new commits?
Pass the earlier findings in context and ask only for new or still-unaddressed issues.
🏁 Summary
claude -p+--output-format json+--json-schema.- CLAUDE.md carries the review criteria and testing standards into CI.
- Use an independent reviewer; pass earlier findings to avoid duplicate comments.
Next, continue with Prompt Engineering for Claude: Explicit Criteria and Few-Shot Examples.
Independent study material by SQA Tools. Not affiliated with, endorsed by or sponsored by Anthropic. Claude is a trademark of Anthropic. Mock questions are original practice items, not real exam content. Exam facts checked against Anthropic's official exam guides and certification FAQ on 11 November 2026.