Run Claude Code in a CI pipeline
Write the Claude Code command for a CI job (-p, --output-format json, --json-schema) and choose the context it needs so reviews report only new issues and generated tests don't repeat existing ones.
Scenario
Scenario: Claude Code for Continuous Integration. A platform team adds an AI review step to their merge request pipeline. The job runs claude "Review this merge request", exactly as the engineers type it on their laptops.
The first run sits until the job times out. After a fix, the next runs post the same eleven comments again on every push, and a parser that scrapes the Markdown reply drops findings whenever a heading changes.
Each problem has a standard fix: a flag, a schema, or the right context.
Explanation
A CI job has no one at the keyboard, and its output goes to a program, not a person. Four things change.
1. Run non-interactively with -p. claude -p "..." (long form --print) runs the prompt, prints the result and exits. Without it, Claude Code opens an interactive session and waits for input that never comes. Pre-approve the tools the job needs with --allowedTools, for example "Read" for a review, because nobody can answer a permission prompt. Note that --allowedTools pre-approves; it doesn't remove other tools. To keep a review job read-only, narrow the tool set with --tools or deny tools with --disallowedTools. Exam items may call this "restricting the allowed tools".
2. Return data, not prose. --output-format json returns the result with the session ID and metadata, with the text in the result field. Add --json-schema with a JSON Schema and the response carries output that matches it in a structured_output field. A script can then post each finding as an inline comment without parsing Markdown.
3. Give project standards through CLAUDE.md. A -p run loads the repository's CLAUDE.md like an interactive session, so put review criteria, testing standards and available fixtures there, where developers and the pipeline share them. Watch for --bare: it skips CLAUDE.md (and hooks, skills and MCP servers) for faster, reproducible runs. The docs recommend it for scripts and say it will become the default for -p in a future release. Under bare mode, pass the standards with a flag such as --append-system-prompt-file.
4. Give the context that stops repetition.
- Re-runs after new commits: include the previous run's findings and ask Claude to report only issues that are new or still unaddressed. Otherwise every push repeats every comment.
- Test generation: include the existing test files, so Claude can see which scenarios are already covered and fill the gaps.
Review in a separate session. A session that just wrote code is biased toward it. Review generated code in an independent session (a separate claude -p run or a fresh subagent) that sees the diff and the criteria, not the reasoning that produced the change.
Worked example
Before: hangs in CI, and the output is Markdown.
claude "Review this merge request for bugs"
After: non-interactive, with reads pre-approved, and machine-readable.
git diff origin/main... > mr.diff
claude -p "Review mr.diff against CLAUDE.md. \
Report only bugs and security issues." \
--allowedTools "Read" \
--output-format json \
--json-schema "$(cat review.schema.json)" \
| jq '.structured_output.findings' > findings.json
The schema in review.schema.json fixes the shape the posting script expects:
{
"type": "object",
"required": ["findings"],
"properties": { "findings": { "type": "array",
"items": { "type": "object",
"required": ["file", "line", "message"],
"properties": {
"file": { "type": "string" },
"line": { "type": "integer" },
"message": { "type": "string" }
}
}
} }
}
The posting script reads file, line and message from findings.json. A change in Claude's wording no longer breaks it.