Task statement 3.6 · Claude Code configuration and workflows
Integrate Claude Code into CI/CD pipelines
Your pipeline script runs claude "Analyze this pull request for security issues" and hangs, waiting for input nobody will type. Once it runs, quieter failures follow: unparseable prose, repeated comments, and a green check on a blocking issue.
share
Invented flags such as --batch are easy to reject, but real flags mislead too: --output-format, the reply-shape flag, set to json alone still returns prose, and --bare changes what loads. The other trap is gating the merge on the exit code, or letting the session that wrote a fix review it, because neither checks the code itself.
KnowledgeOne review, end to end
In CI nobody is at a terminal, so Claude Code must run in non-interactive mode. With -p (or --print) it processes the prompt, prints the result and exits, instead of waiting for input. It then returns an exit code, the number a process hands back to the shell: 0 when the run succeeded, non-zero when it failed. So it says whether Claude finished, not whether the code may merge. The findings say that, and the workflow reads them with jq, a command-line tool for JSON.
- A new commit starts the workflow on a CI runner.
- The runner calls
claude -pwith--output-format jsonand--json-schema, a schema of the required fields. - The prompt carries the diff and the last run's findings, saved by the workflow as an artifact.
- Claude Code loads CLAUDE.md and reads the existing test files.
- It returns JSON with the findings in
structured_output, and exits with code 0. - The workflow posts each finding inline and fails the check if any is blocking.
Interactive · one CI review, step by step
Follow one review from the push to the merge gate.
1 · A new commit starts the workflow on a CI runner. Nobody is at a terminal to answer a prompt.
2 · The runner calls claude -p with --output-format json and --json-schema. Without -p the CLI waits for input and the job hangs.
3 · The runner puts the diff and the last run's findings, downloaded from a saved artifact, into the prompt, with an instruction to report only new or still-unaddressed issues.
4 · Claude Code loads CLAUDE.md (review criteria, testing standards, fixtures) and reads the existing test files from the checkout.
5 · One JSON object comes back, with the findings in structured_output. Exit code 0 means the run finished, not that the code is clean.
6 · jq posts each finding as an inline comment, and the workflow fails the check if any finding is blocking.
KnowledgeThe output flags
Two more flags make the output usable by a script. --output-format is the flag that picks the shape: text (the default), json or stream-json (one event per line). With json you get one object whose result field holds Claude's prose. --json-schema is the flag that passes a JSON Schema, a document listing the fields and types the answer must have. Claude Code validates the final answer against it and puts the conforming object in a separate structured_output field, so the script never parses sentences.
SkillA green check on a blocking finding
The review flags string-built SQL at api/orders.py:42 as blocking, yet the pull request merges. The step fails only on a non-zero exit code, and Claude Code exited 0 because the review ran. The fix keeps that check for failed runs and adds one that counts blocking findings in structured_output.
# schema: findings[] of {file, line, severity: blocking|warning|nit, issue, status} claude -p "$PROMPT" --output-format json \ --json-schema "$(cat findings.schema.json)" > review.json # non-zero: run failed jq -c '.structured_output.findings[]' review.json | ./post-inline.sh N=$(jq '[.structured_output.findings[] | select(.severity=="blocking")] | length' review.json) [ "$N" -eq 0 ] || exit 1 # the merge gate
SkillCLAUDE.md as the CI run's context
A CI run has no conversation history, so project conventions must reach it from the repository. A plain claude -p run loads CLAUDE.md as an interactive session does, so it is the place for review criteria, testing standards and fixture conventions. Define what a valuable test is, or test generation produces low-value test output such as getter tests.
## Review criteria Report injection, auth bypass and unhandled errors. Skip naming and formatting. ## Testing standards pytest, one behaviour per test. No tests for getters or constructors. Fixtures: use db_session and make_order() from tests/conftest.py.
More precise than the guide. The docs recommend --bare for CI. It skips auto-discovery of hooks, skills, MCP servers and CLAUDE.md, and it "will become the default for -p in a future release", so today it is not the default (checked 2026-10-01). Exam answer: CLAUDE.md gives CI-invoked Claude Code its project context. Under --bare, pass the file with --append-system-prompt-file.
KnowledgeSession context isolation
The guide calls this session context isolation: the same Claude session that generated code is less effective at reviewing its own changes than an independent review instance. The generating session still holds its reasoning, so the assumptions behind a bug sit in context as settled decisions. An independent review instance sees only the diff and the criteria, so it judges the code as written, not as intended. Asked to "review the fix you just wrote", a session approves the null check it removed, because removing it was its plan. A fresh call flags the missing check, because it never saw that plan. In a pipeline the review is therefore its own claude -p call, never a --continue or --resume of the session that wrote the fix.
SkillRe-runs and test generation: hand over what already exists
Each CI run is a new session, often on a fresh runner, so it remembers nothing from the last push. Both kinds of duplicate have one fix: pass in what already exists. When re-running a review after new commits, include the prior review findings in context and instruct Claude to report only new or still-unaddressed issues. For test generation, provide the existing test files, so Claude avoids duplicate scenarios.
Push 1 posted F1, string-built SQL at orders.py:42, and F2, an unchecked None at refund.py:17. The developer fixes F1 and pushes. Without the prior findings, run 2 posts F2 again as a new comment. With them, it drops F1 and marks F2 still_unaddressed, so the script replies on the existing thread. Likewise, the prompt below shows that the suite already tests zero-amount and over-limit refunds, so Claude proposes a currency-mismatch test.
# test-generation prompt
Existing tests: $(cat tests/test_refund.py)
Propose tests only for scenarios these tests do not already cover.
| Standards for every run | CLAUDE.md: criteria, testing standards, fixtures. |
| What already exists for this change | The prompt, filled by the workflow: prior findings, existing tests. |
| Whether the pull request may merge | The workflow's own check on structured_output. |
-pis the documented non-interactive mode.CLAUDE_HEADLESSand--batchdo not exist.- Exit code 0 means the run finished. Blocking findings are counted in
structured_output, never inresult. - Review in a separate invocation. The session that wrote the code is anchored to its reasoning.