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.
- 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
| Where | How it starts | Key points |
|---|---|---|
| Script or cron | claude -p "<prompt>" | Exit code 0 on success, non-zero on failure. --output-format json or stream-json for machine-readable output |
| Reproducible script runs | claude --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 Actions | anthropics/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/CD | A job in .gitlab-ci.yml | Beta, maintained by GitLab. A masked ANTHROPIC_API_KEY variable and one job to start |
| Docker / dev container | Claude Code inside the container | Commands 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 provider | CLAUDE_CODE_USE_BEDROCK and the other provider variables | In 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:
-p(or--print) runs non-interactively. Not every CLI option combines with it.--output-format jsonreturns one result that includestotal_cost_usdand a per-model cost breakdown.stream-jsonemits newline-delimited events.--json-schemaasks for output matching a schema.--allowedToolspre-approves tools using permission rule syntax.Bash(git diff *)allows any command starting withgit diff; the space before*matters, becauseBash(git diff*)would also matchgit diff-index.--permission-modesets a baseline, such asacceptEditsorauto.--permission-prompts noneis for runs where nobody can answer. Anything that would prompt is denied unless aPermissionRequesthook allows it, and Claude is told not to retry.--continuecontinues a conversation;--max-budget-usdcaps spend for the run.
(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.
- In
-pmode,ANTHROPIC_API_KEYis always used when present, even over a logged-in subscription. CLAUDE_CODE_OAUTH_TOKENis an alternative for automated environments. It is ignored in project and local settings.- In GitHub Actions, never commit keys; store them as GitHub Secrets and reference them, for example
${{ secrets.ANTHROPIC_API_KEY }}./install-github-appsaves the secret asANTHROPIC_API_KEYorCLAUDE_CODE_OAUTH_TOKEN. - In GitLab, store
ANTHROPIC_API_KEYas a masked CI/CD variable. - With a cloud provider, OIDC (GitHub Actions; GitLab for Bedrock) or Workload Identity Federation (GitLab for Agent Platform) means no static key in the repository.
CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1strips credentials from the environment of Bash commands, hooks and stdio MCP servers.claude-code-actionsets it automatically whenallowed_non_write_usersis configured.
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.
- Claude Code Headless Mode: claude -p for Scripts and Cron: output formats, passing permissions, resuming sessions and running from cron.
- Claude Code GitHub Actions: Setup, @claude and Security: the GitHub App, the secret, the workflow, and who can trigger it.
- Claude Code GitLab CI/CD: Setup, Bedrock and Vertex (Beta): the beta integration, masked variables, and keyless Bedrock or Vertex.
- 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.
- Running Claude Code in CI: Costs and Traps: API tokens plus runner minutes, a cache trap, and stopping runaway jobs.
- Run Claude Code in Docker and Dev Containers: isolating Claude Code in a container with an outbound allowlist.
- Claude Code Code Review: /code-review vs Managed PR Reviews: the managed review for Team and Enterprise versus
/code-review. - Passes Locally, Fails in CI: the order to check differences when CI fails but your machine passes.
- Deploy and Migration Accidents: quiet failures such as success messages that did nothing, and how to prevent them.
- 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
claude -pis the entry point for scripts and CI. Use--output-format jsonorstream-jsonfor parsing and--allowedToolsto approve only what the job needs.--bareskips hooks, skills, MCP servers and CLAUDE.md. Without it, a-prun uses the project’s hooks and.mcp.jsoneven in an untrusted folder.- In
-pmodeANTHROPIC_API_KEYwins over a subscription login; keep keys in CI secrets or masked variables, or use OIDC with a cloud provider. - GitHub Actions runs need write access on issue and PR events and reject bots unless allowed. GitLab CI/CD is a beta maintained by GitLab.
- Costs in CI come from API tokens and runner minutes; cap turns, set timeouts and limit concurrency.
- Checked against the official docs on 2026-11-10. We ran nothing for this hub.
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.