Automation & CI/CD

Use Claude Code's headless mode in scripts and CI/CD pipelines


title: "Automation & CI/CD" description: "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-permissions skips 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://api.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://api.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

  1. Always limit turns: Use --max-turns in CI to prevent infinite execution

  2. Restrict tools: Use --allowedTools to only enable the tools you need

  3. Isolate the environment: Use --dangerously-skip-permissions inside Docker containers

  4. Use JSON output: For automation, prefer --output-format json for easy parsing

Next Steps

Related Documents

Getting Started with Claude Agent SDK
Learn how to build AI Agent applications using the Claude Agent SDK, connected via QCode.cc API
Security Best Practices
Fully mastering Claude Code's security mechanisms — permission control, sensitive file protection, command interception, API key management
Sub-agent Collaboration Mode
Understanding and using Claude Code's sub-agents — parallel exploration, background tasks, isolated execution
🚀
Get Started with QCode — Claude Code & Codex
One plan for both Claude Code and Codex, Asia-Pacific low latency
View Pricing Plans → Create Account
Team of 3+?
Enterprise: dedicated domain + sub-key management + ban protection, from ¥250/person/mo
Learn Enterprise →