CC Guide
上級

GitHub Actions で Claude Code を CI/CD に組み込む

anthropics/claude-code-action を使って、PRレビュー、自動修正、テスト生成をCIパイプラインに組み込む方法

ci-cdgithub-actionsautomation

GitHub Actions で Claude Code を CI/CD に組み込む

Claude Code は GitHub Actions との統合を提供しており、PR のレビュー、自動修正、テスト生成を CI/CD パイプラインに組み込めます。チーム全体で Claude Code の能力を活用できます。

基本的な設定

name: Claude Code Review
on:
  pull_request:
    types: [opened, synchronize]
  issue_comment:
    types: [created]

jobs:
  claude-review:
    runs-on: ubuntu-latest
    steps:
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          github_token: ${{ secrets.GITHUB_TOKEN }}

トリガーの設定

トリガー説明
PR のオープン/更新自動的にコードレビューを実行
コメント内の @claude手動でClaudeに指示を送信
# PR のオープン/更新時に自動レビュー
on:
  pull_request:
    types: [opened, synchronize]

# コメントで手動トリガー
on:
  issue_comment:
    types: [created]

コメントで @claude を含めると、Claude がその指示に基づいて動作します。

パラメータの詳細

- uses: anthropics/claude-code-action@v1
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    github_token: ${{ secrets.GITHUB_TOKEN }}
    trigger_phrase: "@claude"         # デフォルト: @claude
    prompt: "Review for security issues and performance"
    claude_args: "--max-turns 10 --model sonnet"
    use_vertex: false                  # Vertex AI を使用する場合
パラメータ説明デフォルト
anthropic_api_keyAPI キー(必須)--
github_tokenGitHub App トークン--
trigger_phraseトリガーフレーズ@claude
promptカスタム指示--
claude_argsCLI 引数--
use_vertexVertex AI 使用フラグfalse

実践的なユースケース

自動セキュリティレビュー

- uses: anthropics/claude-code-action@v1
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    github_token: ${{ secrets.GITHUB_TOKEN }}
    prompt: |
      Review this PR for security vulnerabilities:
      - SQL injection
      - XSS
      - Authentication issues
      - Sensitive data exposure

テスト生成

# PRコメントで @claude generate tests と入力
- uses: anthropics/claude-code-action@v1
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    github_token: ${{ secrets.GITHUB_TOKEN }}

コミット時に @claude generate unit tests for the changed files とコメントするだけで、テストが自動生成されます。

GitLab CI/CD との連携

GitLab CI/CD でも同様の統合が利用可能です。設定方法は GitHub Actions と同様のアプローチで、claude CLI をパイプラインステップとして実行します。

# GitLab CI/CD
review:
  stage: review
  script:
    - git diff origin/main...HEAD | claude -p "Review this diff for issues"

ワンショット実行との組み合わせ

- name: Type Check
  run: |
    npx tsc --noEmit 2>&1 | claude -p "Fix all TypeScript errors" --max-turns 5

コスト管理

CI/CD での使用は API キー経費となるため、--max-turns でターン数を制限し、--model で適切なモデルを選択しましょう。


あわせて読む

関連コンテンツ