Study notes · 3.4% of the exam

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. 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 --batch flag or CLAUDE_HEADLESS variable.

  2. 2

    --output-format json returns a JSON object with result, session_id and usage metadata; stream-json emits newline-delimited events. Pair --output-format json with --json-schema '<schema>' to get output that conforms to the schema in the structured_output field, which a script can post as inline PR comments. Asking for JSON in the prompt is not enforcement.

  3. 3

    Piping works like any Unix tool: gh pr diff 123 | claude -p "..." --output-format json. Use --append-system-prompt to add reviewer instructions while keeping the default behaviour; --max-turns and --max-budget-usd cap runaway jobs.

  4. 4

    --allowedTools pre-approves tools using permission-rule syntax (Bash(git diff *)); in a -p run 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-permissions or yes | removes the guardrail, not just the prompt.

  5. 5

    --bare skips 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. 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. 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. --continue does 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. 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. 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. 10

    Claude Code GitHub Actions (anthropics/claude-code-action) supports @claude mentions and prompt-driven automation; pass CLI flags through claude_args, keep API keys in secrets, grant only the permissions the workflow needs, and define standards in CLAUDE.md.

  11. 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.

Test yourself on 3.6 Integrate Claude Code into CI/CD pipelines

Ten questions, with the answer and explanation after each one.