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.

Scenario switchThis section runs on the exam guide's Claude Code for Continuous Integration scenario. On every pull request, Claude Code reviews the diff and proposes tests, and the pipeline parses its JSON to post PR comments.
Why it's tested

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.

  1. A new commit starts the workflow on a CI runner.
  2. The runner calls claude -p with --output-format json and --json-schema, a schema of the required fields.
  3. The prompt carries the diff and the last run's findings, saved by the workflow as an artifact.
  4. Claude Code loads CLAUDE.md and reads the existing test files.
  5. It returns JSON with the findings in structured_output, and exits with code 0.
  6. The workflow posts each finding inline and fails the check if any is blocking.

Interactive · one CI review, step by step

Sequence diagram of one CI review in six steps. A push starts the workflow on the CI runner. The runner calls claude -p with the JSON flags, then passes the diff and the previous run's findings in the prompt. Claude Code loads CLAUDE.md and the existing tests from the repository checkout. It returns one JSON object and exit code 0 to the runner. The runner posts each finding as an inline comment and fails the check if any finding is blocking. Pull request CI runner claude -p Repo checkout PR comments 1 commit pushed 2 -p --output-format json --json-schema … without -p: waits for input 3 diff + prior findings from a saved artifact 4 CLAUDE.md + existing tests 5 JSON: structured_output exit 0 = the run finished 6 one inline comment per finding check fails if any is blocking

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.

Messages 1–6 are steps 1–6. Old findings come from the runner, not the repository. The merge decision comes from the findings, not the exit code.

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.
For each input, ask: where does the CI run get it?
Standards for every runCLAUDE.md: criteria, testing standards, fixtures.
What already exists for this changeThe prompt, filled by the workflow: prior findings, existing tests.
Whether the pull request may mergeThe workflow's own check on structured_output.
Takeaway
  • -p is the documented non-interactive mode. CLAUDE_HEADLESS and --batch do not exist.
  • Exit code 0 means the run finished. Blocking findings are counted in structured_output, never in result.
  • Review in a separate invocation. The session that wrote the code is anchored to its reasoning.

check yourself

0 / 3 answered

A pipeline step asks Claude to fix a failing lint check, then runs claude -p --continue "Review the fix you just made" before posting. The review approves 97% of fixes, yet humans later find bugs in one fix in five. The approvals often repeat the fix's own reasoning, such as "the null check was redundant, as planned".Which change most directly addresses the cause of the missed bugs?

Select one.

The review job fails the pull-request check only when claude -p exits non-zero. Last week a pull request merged with a finding marked "severity": "blocking" in structured_output. Twice this month the posting script broke because Claude reworded its prose summary. Runs that fail on authentication must still fail the check.Which change best blocks merges on blocking findings while keeping failed runs visible?

Select one.

Your review and test-generation job runs on runners that are deleted after each job. The only thing it keeps is the last run's findings JSON, uploaded as an artifact. On every push, developers see earlier findings posted again as new comments, and the proposed tests include cases that tests/test_checkout.py already covers.Which TWO changes best stop both kinds of duplicate?

Select two.

sources

  1. Claude Code Docs, "Run Claude Code programmatically": -p runs non-interactively; exit code 0 on success and non-zero when the run fails; the three --output-format values; --json-schema output in structured_output; parsing with jq; what --bare skips, that it is recommended for CI, and that it "will become the default for -p in a future release"; --continue / --resume in -p mode. Checked 2026-10-01.
  2. Claude Code Docs, "CLI reference": --print, --output-format (print mode), --json-schema ("validated JSON output … after the agent completes its workflow", print mode only), --bare, --append-system-prompt-file. Checked 2026-10-01.
  3. Claude Code Docs, "Claude Code GitHub Actions": CLI flags pass through claude_args; define review criteria and project rules in CLAUDE.md; inline PR comments from the review workflow. Checked 2026-10-01.
  4. Claude Code Docs, "Best practices for Claude Code": claude -p in CI; a fresh context improves code review because Claude "won't be biased toward code it just wrote" (Writer/Reviewer pattern). Checked 2026-10-01.
  5. Task statement, the session context isolation wording, and the hanging-pipeline example: Claude Certified Architect — Foundations Exam Guide v1.0, Domain 3.6 and Sample Question 10. The findings schema, the status field, file names and the blocking-count gate are this course's illustration, not a documented format.