Issue 00 · Autumn 2026日本語 · RSS
AgentKoji

Claude Code in CI, Headless and Production

How Claude Code runs unattended: claude -p and --bare, GitHub Actions, GitLab CI/CD, containers and cloud providers, plus permissions and API keys in CI. Checked 2026-11-10.

TL;DR

  • claude -p runs Claude Code non-interactively, --output-format json or stream-json gives machine-readable output, and --allowedTools pre-approves tools. --bare skips hooks, skills, MCP servers and CLAUDE.md, and the docs recommend it for scripts.
  • Without --bare, a -p run executes the project's hooks and connects .mcp.json servers even in a folder you never trusted, with no trust dialog.
  • In GitHub Actions, @claude can be triggered only by users with write access, and keys belong in GitHub Secrets. With Bedrock, Agent Platform or Foundry, the action authenticates through OIDC instead of a stored key.

This page is a map of running Claude Code without a person at the keyboard: in scripts, cron, CI pipelines and production automation. It covers how to start a run, how to keep permissions and keys tight, and the order in which to read our guides on CI, containers, deploys and monitoring. Every fact was checked against the official docs on 2026-11-10, when the latest Claude Code on npm was 2.1.322.

We did not run any command on this page for this hub. Where the docs did not state something, we left it out or marked it “unverified”.

Ways to run it unattended

WhereHow it startsKey points
Script or cronclaude -p "<prompt>"Exit code 0 on success, non-zero on failure. --output-format json or stream-json for machine-readable output
Reproducible script runsclaude --bare -p ...Skips hooks, skills, custom commands, subagents, plugins, MCP servers, auto memory and CLAUDE.md. Does not use your subscription login; set ANTHROPIC_API_KEY
GitHub Actionsanthropics/claude-code-action/install-github-app for quick setup (github.com only, admin access needed). Interactive mode answers @claude; automation mode runs a prompt input
GitLab CI/CDA job in .gitlab-ci.ymlBeta, maintained by GitLab. A masked ANTHROPIC_API_KEY variable and one job to start
Docker / dev containerClaude Code inside the containerCommands run in the container; edits to project files show up locally. See our Docker guide (based on the devcontainer page, checked 2026-10-20)
Cloud providerCLAUDE_CODE_USE_BEDROCK and the other provider variablesIn GitHub Actions: use_bedrock, use_vertex, use_foundry with OIDC instead of a Claude API key

(source: https://code.claude.com/docs/en/headless, checked 2026-11-10) (source: https://code.claude.com/docs/en/github-actions, checked 2026-11-10) (source: https://code.claude.com/docs/en/gitlab-ci-cd, checked 2026-11-10) (source: https://code.claude.com/docs/en/env-vars, checked 2026-11-10)

The workflow examples on the GitHub Actions page reference anthropics/claude-code-action@v1. The page also has an “Upgrade from beta” section for workflows still on @beta.

Headless essentials

The headless page lists the flags most CI jobs touch:

(source: https://code.claude.com/docs/en/headless, checked 2026-11-10)

Permissions and secrets in CI

Trust. Claude Code shows the workspace trust dialog only in interactive sessions. A claude -p run never shows it. Without --bare, a -p session runs the hooks in the project’s .claude/settings.json and connects the servers in its .mcp.json, even in a folder you never trusted. If the pipeline checks out code you did not write, that is the first thing to decide. --bare never reads them.

Tools. List only what the job needs in --allowedTools, using prefix rules such as Bash(git diff *). Rules are evaluated deny, then ask, then allow, and the first match decides.

Keys.

Who can trigger. The GitHub Action requires write access on issue and PR events, and rejects bot actors unless they are in allowed_bots, which keeps bots from triggering Claude in a loop.

Cost. Each GitHub Actions run uses runner minutes and API tokens; with an OAuth token, runs use the Claude subscription instead of API billing. The docs suggest --max-turns in claude_args, workflow timeouts and concurrency limits. Specific prices are not on these pages; see the costs page and your provider’s pricing.

(source: https://code.claude.com/docs/en/permissions, checked 2026-11-10) (source: https://code.claude.com/docs/en/headless, checked 2026-11-10) (source: https://code.claude.com/docs/en/env-vars, checked 2026-11-10) (source: https://code.claude.com/docs/en/github-actions, checked 2026-11-10) (source: https://code.claude.com/docs/en/gitlab-ci-cd, checked 2026-11-10) (source: https://code.claude.com/docs/en/costs, checked 2026-11-10)

Reading order

These are starting points, not a ranking. Read top to bottom if you are new to running Claude Code unattended.

  1. Claude Code Headless Mode: claude -p for Scripts and Cron: output formats, passing permissions, resuming sessions and running from cron.
  2. Claude Code GitHub Actions: Setup, @claude and Security: the GitHub App, the secret, the workflow, and who can trigger it.
  3. Claude Code GitLab CI/CD: Setup, Bedrock and Vertex (Beta): the beta integration, masked variables, and keyless Bedrock or Vertex.
  4. CLAUDE_CODE_OAUTH_TOKEN: Setup, CI Use and Leak Response: the one-year subscription token, how it ranks against an API key, and how to revoke it after a leak.
  5. Running Claude Code in CI: Costs and Traps: API tokens plus runner minutes, a cache trap, and stopping runaway jobs.
  6. Run Claude Code in Docker and Dev Containers: isolating Claude Code in a container with an outbound allowlist.
  7. Claude Code Code Review: /code-review vs Managed PR Reviews: the managed review for Team and Enterprise versus /code-review.
  8. Passes Locally, Fails in CI: the order to check differences when CI fails but your machine passes.
  9. Deploy and Migration Accidents: quiet failures such as success messages that did nothing, and how to prevent them.
  10. Pitfalls of Always-On Bot Monitoring: how a heartbeat can stay green while the job fails.

Also related: Claude Code on Bedrock, Vertex and Foundry: Setup Guide for running on a cloud provider, the Claude Code hooks hub, and Claude Code Setup and Daily Operations.

If the runner is your own VPS, requirements, outbound access and login are covered in the hub Running AI Coding Agents on a VPS: Choosing, Logging In, Staying Up.

Summary

FAQ

How do I run Claude Code in CI?
Use claude -p with your prompt. Add --output-format json for a single JSON result or stream-json for streamed events, --allowedTools to pre-approve only the tools the job needs, and --bare so the run does not pick up hooks, skills, MCP servers or CLAUDE.md from the machine.
Which API key does claude -p use?
In non-interactive mode, ANTHROPIC_API_KEY is always used when it is set, even if you are logged in with a subscription. --bare does not use your subscription login, so set ANTHROPIC_API_KEY for bare runs.
Who can trigger Claude Code GitHub Actions?
On issue and pull request events, only users with write access to the repository. Bot actors are rejected unless listed in allowed_bots. Scheduled runs skip the write-access check.
Can I run Claude Code in CI without storing an Anthropic API key?
Yes. The GitHub Action can route through Amazon Bedrock, Google Cloud's Agent Platform or Microsoft Foundry using OIDC identity federation. GitLab CI/CD documents Bedrock through OIDC and Agent Platform through Workload Identity Federation.
Is Claude Code for GitLab CI/CD official?
It is in beta and maintained by GitLab, according to the Claude Code docs. The quick start is a masked ANTHROPIC_API_KEY CI/CD variable and one job in .gitlab-ci.yml.