Run Claude Code in GitHub Actions and CI/CD Pipelines
Claude Code runs in headless mode for CI/CD. Use it in GitHub Actions to auto-review PRs, triage issues, generate fixes, and maintain code quality at scale.
Claude Code's headless mode (-p flag) lets it run without human interaction, which makes it perfect for CI/CD pipelines. You can automate code reviews, generate fixes for failing tests, triage issues, and enforce code standards on every push.
How do you run Claude Code in headless mode?
The -p flag runs Claude Code with a prompt and exits when done. It reads the project, executes the task, and outputs the result. No interactive terminal needed.
# Basic headless usage
claude -p "Review the changes in the last commit and report any issues"
# With output format
claude -p "List all TODO comments in the codebase" --output-format json
# With budget limit
claude -p "Fix the failing tests" --max-budget-usd 1.00How do you set up Claude Code in GitHub Actions?
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Claude Code
run: npm install -g @anthropic-ai/claude-code
- name: Review PR
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude -p "Review the changes in this PR. \
Check for bugs, security issues, and style violations. \
Post your review as a PR comment." \
--max-budget-usd 2.00 \
--dangerously-skip-permissionsThe --dangerously-skip-permissions flag is required in CI/CD because there is no human to approve tool usage. Only use this in controlled environments with trusted code. Never use it on public repositories without careful access controls.
What CI/CD tasks can Claude Code automate?
| Task | Trigger | What Claude Code does |
|---|---|---|
| PR code review | pull_request opened | Reviews diff, posts comments on issues found |
| Fix failing tests | push (after test failure) | Reads test output, fixes code, pushes a commit |
| Issue triage | issues opened | Reads issue, adds labels, assigns priority |
| Changelog generation | release published | Reads commits since last tag, generates changelog |
| Documentation update | push to main | Updates API docs based on code changes |
| Security audit | schedule (weekly) | Scans for common vulnerabilities, opens issues |
How do you handle API keys and costs?
- +Store ANTHROPIC_API_KEY in GitHub Secrets (never in code)
- +Use --max-budget-usd to cap spending per run (e.g., $2 per PR review)
- +Use Sonnet model for cost-sensitive tasks: claude -p "..." --model sonnet
- +Monitor usage in your Anthropic dashboard
How do you use Claude Code with GitLab CI?
# .gitlab-ci.yml
ai-review:
stage: review
image: node:20
script:
- npm install -g @anthropic-ai/claude-code
- claude -p "Review the MR changes and report issues" --max-budget-usd 2.00 --dangerously-skip-permissions
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"