20%

3.6 CI/CD Integration

Claude Code ใน CI/CD คืออะไร

Claude Code สามารถ run ใน CI/CD pipelines แบบ headless — ไม่ต้องมีคน interact ใช้สำหรับ automated code review, test generation, documentation updates, และ deployment automation

Headless Mode

ใช้ --print flag สำหรับ non-interactive mode:

# Single prompt, print output, exit
claude --print "Review this PR for security issues"

# Pipe input
git diff main | claude --print "Review this diff for bugs"

# With specific model
claude --print --model sonnet "Generate changelog from git log"

GitHub Actions Integration

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: Get diff
        run: git diff origin/main...HEAD > /tmp/diff.txt

      - name: AI Review
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          cat /tmp/diff.txt | claude --print "
          Review this PR diff for:
          1. Bugs and logic errors
          2. Security vulnerabilities
          3. Performance issues
          
          Output as markdown with severity levels.
          " > /tmp/review.md

      - name: Post Review Comment
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const review = fs.readFileSync('/tmp/review.md', 'utf8');
            github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
              body: review
            });

Use Cases in CI/CD

Automated Code Review

  • PR review bot ที่ comment issues
  • Security scanning
  • Style/convention checking beyond linters

Test Generation

claude --print "Generate unit tests for src/new-feature.ts" > tests/new-feature.test.ts

Documentation

claude --print "Update API docs based on route changes in this PR" 

Changelog Generation

git log --oneline v1.0..HEAD | claude --print "Generate user-facing changelog"

Best Practices

  • Cost control — set max_tokens limit, use cheaper models (Haiku) for simple tasks
  • Determinism — same input should produce similar output (use low temperature)
  • Timeout — set CI timeout เพื่อป้องกัน hung jobs
  • Caching — cache results for same diff/input to avoid re-running
  • Scope — ให้ Claude review เฉพาะ changed files ไม่ใช่ทั้ง repo

Key Concepts

  • –print — headless mode, output to stdout, no interaction
  • Pipe-friendly — รับ input จาก stdin, ส่ง output ไป stdout
  • API key — ต้องมี ANTHROPIC_API_KEY ใน environment
  • Stateless — แต่ละ run เริ่มใหม่ ไม่มี conversation history

Exam Tips

  • claude --print = headless/non-interactive mode สำหรับ CI
  • ข้อสอบอาจถามว่า flag ไหนใช้สำหรับ CI integration — --print
  • Claude Code ใน CI ควร scope ให้เล็ก (review เฉพาะ diff, ไม่ใช่ทั้ง codebase)
  • Cost management สำคัญใน CI — ทุก PR run = API call = $
  • ต้องมี ANTHROPIC_API_KEY ใน CI secrets