Claude Code in CI/CD: -p Flag, JSON Output and Automated PR Review
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).
⚙️ 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.⚙️ 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.
🔍 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.
🔍 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.
🧪 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 json` with `–json-schema` defining the findings structure
Show answer
Answer: D. Machine-parseable and schema-enforced. B and A 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 -p with –output-format json 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.