Skip to main content

Headless Claude Code in CI

Why This Matters

In CI there is nobody to answer a question or approve a step. Claude Code has to run non-interactively, with exactly the permissions the job needs, and return output a pipeline can parse.

Non-interactive mode

claude -p "…" (print mode) runs one task and exits. Add --output-format json and you get a JSON result with the answer, the session id and usage, which the next step in your pipeline can read:

claude -p "Review the golden-set changes in this pull request" --output-format json | jq -r '.result'

For a result in a fixed shape, combine --output-format json with --json-schema and a JSON Schema; check the headless documentation for the details.

A real run in TestPilot, asking which npm scripts call the Claude API, with only the Read tool allowed:

claude -p with –output-format json and –allowedTools Read, piped through jq: result is a JSON string listing triage, eval, triage:tools and team; total_cost_usd 0.189; a session_id

Three things to notice before you put this in a pipeline:

  1. result is a string, not an object. The JSON you asked for is inside it, escaped. Your script needs .result | fromjson to read it, and should survive a model that wraps it in a code fence (the workflow on the solution branch strips one).
  2. It cost $0.19 for one small question. Claude Code loads the project context, the rules and the tool definitions before it answers, so a headless call is not a cheap single API request. Run it where it earns its cost, such as golden-set changes, not on every push.
  3. The answer is right. It found exactly the four scripts that call the API, and left out check, test and mcp. Careful with what that flag means: --allowedTools "Read" only pre-approves reading. It does not take other tools away; reading files in the working directory needs no approval anyway. To restrict a run to reading, use --tools "Read".

A CI job for golden-set changes

On every pull request that changes fixtures/golden/, check that every changed label has a reason:

on:
  pull_request:
    paths: ["fixtures/golden/**"]
jobs:
  golden-review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }
      - run: git diff origin/main -- fixtures/golden > golden.diff
      - run: |
          claude -p "Read golden.diff. For every changed label, check that a reason is given. Answer with JSON: {\"ok\": boolean, \"missing\": [case ids]}" \
            --output-format json --tools "Read" > review.json
      - run: jq -e '.result | fromjson | .ok' review.json

The job above is shortened to the essentials. A runner does not have claude installed and has no credentials: either install the CLI and provide ANTHROPIC_API_KEY from the repository secrets (as .github/workflows/golden-review.yml on the solution branch does), or use Anthropic’s claude-code-action.

Context for a CI run

A CI run gets what it needs and nothing more:

  • the diff, not the whole repository history
  • the project CLAUDE.md and the relevant rules, which the run picks up from the checkout
  • read-only tools: --tools "Read" restricts the run to reading (--allowedTools would only pre-approve)

Credentials

The Console offers a CI option that needs no stored secret (you saw it when creating a key in lesson 3). Prefer it over an API key in a repository secret when your CI platform supports it. Either way: never put a key in the prompt, the workflow file or the logs.

Fail on structure, not on words

A build that fails when the review “sounds negative” is unpredictable. Ask for a structured result (ok, missing) and fail the build on that field. Post the readable review as a comment for humans, but let the pipeline decide on the structure.

Exercise

Add the golden-review job to TestPilot. Open a pull request that changes a label without a reason and check that the job fails with the right case id.

Compare with solution/lesson-52-claude-code, which has the Claude Code setup for lessons 42–52. Its docs/claude-code-demo.md walks you through all of module 6 in one session.