← BLOG
Workflow

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.

bash
# 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.00

How do you set up Claude Code in GitHub Actions?

yaml
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-permissions
WARNING

The --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?

TaskTriggerWhat Claude Code does
PR code reviewpull_request openedReviews diff, posts comments on issues found
Fix failing testspush (after test failure)Reads test output, fixes code, pushes a commit
Issue triageissues openedReads issue, adds labels, assigns priority
Changelog generationrelease publishedReads commits since last tag, generates changelog
Documentation updatepush to mainUpdates API docs based on code changes
Security auditschedule (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?

yaml
# .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"

Frequently asked questions

How much does CI/CD usage cost?+
Costs depend on the task complexity and model used. A typical PR review with Sonnet costs $0.10-0.50. Using --max-budget-usd prevents surprises. Budget $50-100/month for a team of 10 developers with active PR review.
Can Claude Code push commits in CI/CD?+
Yes. With proper git configuration and permissions, Claude Code can create commits and push them. This is useful for auto-fixing lint errors, updating generated files, or applying security patches automatically.
Is it safe to use --dangerously-skip-permissions?+
In a CI/CD environment with controlled inputs, yes. The flag is necessary because there's no human to approve actions. Mitigate risk by using --max-budget-usd, running on isolated runners, and limiting which workflows use the flag.
Can Claude Code create PRs from CI/CD?+
Yes. Claude Code can use the GitHub CLI (gh) to create PRs, add labels, request reviewers, and post comments. Install gh in your CI runner and authenticate with a GitHub token.
ALL POSTSSTART FREE COURSE →