Automation & CI/CD
Use Claude Code's headless mode in scripts and CI/CD pipelines
Automation & CI/CD¶
Claude Code supports headless mode for use in scripts, CI/CD pipelines, and automated workflows β no human interaction required.
Basic Usage¶
Single Execution (-p Mode)¶
# Basic execution
claude -p "Analyze this project's architecture"
# JSON output format
claude -p "List all TODO comments" --output-format json
# Limit execution turns
claude -p "Write unit tests for UserService" --max-turns 5
Restricting Available Tools¶
# Read-only analysis (no writes allowed)
claude -p "Analyze code quality" --allowedTools Read,Glob,Grep
# Allow read and write (no command execution)
claude -p "Refactor this file" --allowedTools Read,Write,Edit
Skipping Permission Prompts¶
In secure, isolated environments (e.g., Docker containers or CI environments):
claude -p "Fix all lint errors and commit" --dangerously-skip-permissions
Security warning:
--dangerously-skip-permissionsskips all permission prompts. Only use this in trusted, isolated environments.
CI/CD Integration Examples¶
GitHub Actions¶
name: AI Code Review
on: [pull_request]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install Claude Code
run: npm install -g @anthropic-ai/claude-code
- name: Run AI Code Review
env:
ANTHROPIC_BASE_URL: "https://asia.qcode.cc/api"
ANTHROPIC_AUTH_TOKEN: ${{ secrets.QCODE_API_KEY }}
run: |
claude -p "Review this PR's code changes, focusing on security and performance issues" \
--output-format json \
--max-turns 3 \
--allowedTools Read,Glob,Grep \
> review.json
Script Automation¶
#!/bin/bash
# auto-test.sh β Automatically generate and run tests
export ANTHROPIC_BASE_URL="https://asia.qcode.cc/api"
export ANTHROPIC_AUTH_TOKEN="cr_your_api_key"
# Generate tests
claude -p "Generate unit tests for all files under src/services/" \
--max-turns 10 \
--allowedTools Read,Write,Glob,Grep,Bash
# Run tests
npm test
Session Resumption¶
You can maintain context across multiple invocations:
# First call: analysis
claude -p "Analyze the project architecture" --session-id "ci-task-42" --output-format json
# Second call: suggest improvements based on analysis
claude -p "Based on the previous analysis, suggest improvements" --resume --session-id "ci-task-42"
Output Formats¶
| Format | Flag | Description |
|---|---|---|
| Plain text | --output-format text |
Default, human-readable |
| JSON | --output-format json |
Structured output, suitable for script parsing |
| Streaming JSON | --output-format stream-json |
Real-time streaming output |
Environment Variables¶
| Variable | Description |
|---|---|
ANTHROPIC_BASE_URL |
QCode.cc API endpoint |
ANTHROPIC_AUTH_TOKEN |
API key |
CLAUDE_CODE_MAX_TURNS |
Default maximum turns |
CLAUDE_CODE_OUTPUT_FORMAT |
Default output format |
Best Practices¶
-
Always limit turns: Use
--max-turnsin CI to prevent infinite execution -
Restrict tools: Use
--allowedToolsto only enable the tools you need -
Isolate the environment: Use
--dangerously-skip-permissionsinside Docker containers -
Use JSON output: For automation, prefer
--output-format jsonfor easy parsing
Next Steps¶
-
See Hooks System for triggering automated actions in CI
-
See CLI Tips for more command-line usage