Skip to content

CI/CD Pipelines

Claude Code runs in CI/CD as a scriptable, headless agent. Use it to review code, generate release notes, triage issues, run migrations, or translate strings, all triggered by git events, on a schedule, or on demand. This lesson covers GitHub Actions and GitLab CI/CD integration.

Gnome mascot

What you'll learn

  • Print mode (-p) with --max-turns and --output-format json is the CI pattern
  • --bare mode skips hooks, plugins, MCP discovery, and OAuth for fastest CI startup
  • ANTHROPIC_API_KEY is required in CI, OAuth flows don't work headless

The problem

CI/CD pipelines run tests, linters, and builds. But they don't do anything that requires judgment, reviewing code quality, writing release notes from commits, triaging whether an issue is a bug or a feature request. Claude Code fills that gap by running as an agent inside your pipeline, making decisions and producing structured output.

Build it

Step 1: Basic CI pattern

Configure your pipeline tasks using bare mode alongside a fixed turn limit to guarantee predictable execution speeds.

bash
claude --bare -p "task description" --max-turns 10 --output-format json

--bare is critical in CI: it skips hooks, plugins, MCP discovery, CLAUDE.md auto-loading, and OAuth. Fastest startup. Requires ANTHROPIC_API_KEY.

Step 2: GitHub Actions, issue triage

Integrate Claude into a GitHub workflow file to automatically categorize and label incoming issues as they open.

yaml
# .github/workflows/triage.yml
name: Claude Triage
on:
  issues:
    types: [opened]
jobs:
  triage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: |
          curl -fsSL https://claude.ai/install.sh | bash
      - name: Triage issue
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          claude --bare -p "Triage issue #${{ github.event.issue.number }}: '${{ github.event.issue.title }}'. Classify as bug, feature, or question. Add appropriate labels. Respond with a helpful comment." \
            --max-turns 5 --output-format json

Step 3: GitHub Actions, release notes

Use this command to analyze Git history and generate structured release notes entirely within your deployment pipeline.

bash
claude --bare -p "Generate release notes from the commits since the last tag. Group by feature, fix, and chore. Use conventional commit prefixes." \
  --max-turns 5 --output-format json

Step 4: GitLab CI/CD

Define a GitLab pipeline job that automatically invokes Claude to review merge requests using bare mode.

yaml
# .gitlab-ci.yml
claude-review:
  image: node:20
  script:
    - curl -fsSL https://claude.ai/install.sh | bash
    - claude --bare -p "Review the diff in $CI_MERGE_REQUEST_DIFF" --max-turns 10
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

Step 5: Selectively load context in bare mode

FlagEffect
--append-system-prompt "text"Add to system prompt
--settings <file-or-json>Load settings
--mcp-config <file>Load MCP servers
--agents '<json>'Define custom agents

What goes wrong

MistakeHow you notice itThe fix
OAuth fails in CIclaude prompts for browser loginUse ANTHROPIC_API_KEY env var. OAuth requires a browser
--max-turns too lowTask stops mid-way, output is incompleteStart with 10-15 turns for complex tasks. Each tool call counts as one turn
--max-budget-usd minimumError: budget too lowMinimum is ~$0.05 (system prompt cache creation). Set at least $0.10
Output is too large for CI logTruncated JSON in CI outputUse --output-format json and parse the result field. Pipe to a file: > result.json

Confirm it worked

Test your pipeline integration locally to verify that bare mode successfully skips interactive authentications and configuration steps.

bash
# 1. Run a CI-style task locally
ANTHROPIC_API_KEY=your-key claude --bare -p "Count the number of Python files in this repo and report the total" --max-turns 3

# 2. Should return a number, not an error

Next: Chrome & Web Testing