CI/CD 統合:パイプラインへの Claude Code 導入
GitHub Actions と GitLab CI/CD に Claude Code を統合。PR レビュー、Issue 実装、セキュリティ監査、ドキュメント更新を開発パイプラインで自動化する方法を解説します。
⚠️ コミュニティパターンについて: この CI/CD 統合ガイドは、公式の Claude Code 機能ではなく、コミュニティで開発されたパターンを説明しています。
claude code reviewや/github-action-setupなどの CLI コマンドは、Claude Code を CI/CD パイプラインに統合する方法の概念的な例です。公式の Claude Code 機能は docs.anthropic.com で文書化されています。
Claude Code は単なるローカル開発ツールではありません。CI/CD パイプラインに統合することで、自動 PR レビュー、Issue 実装、セキュリティ監査などを実現できます。
このガイドでは、GitHub Actions と GitLab CI/CD の統合パターンを解説します。
なぜ CI/CD 統合が必要なのか?
従来の CI/CD:
└── テスト実行
└── Lint 実行
└── デプロイ(パス時)
Claude Code 導入後:
└── テスト実行
└── Lint 実行
└── AI によるコードレビュー
└── 自動セキュリティ監査
└── ドキュメント同期チェック
└── Issue 自動トリアージ
└── Issue からの機能実装
└── デプロイ(すべてパス時)
GitHub Actions 統合
クイックセットアップ
/github-action-setup
これにより以下のワークフローが作成されます:
- PR レビュー
- Issue トリアージ
- セキュリティ監査
- ドキュメントチェック
PR 自動レビューワークフロー
# .github/workflows/claude-pr-review.yml
name: Claude PR Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Run PR Review
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude code review \
--pr ${{ github.event.pull_request.number }} \
--output-format github \
--post-comments
- name: Security Audit
if: contains(github.event.pull_request.labels.*.name, 'security-review')
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude code audit \
--security \
--pr ${{ github.event.pull_request.number }} \
--fail-on critical,high
Issue 自動トリアージワークフロー
# .github/workflows/claude-issue-triage.yml
name: Claude Issue Triage
on:
issues:
types: [opened]
jobs:
triage:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Triage Issue
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
claude code triage-issue \
--issue ${{ github.event.issue.number }} \
--add-labels \
--estimate-complexity \
--suggest-assignee
Issue 自動実装ワークフロー
# .github/workflows/claude-implement-issue.yml
name: Claude Implement Issue
on:
issues:
types: [labeled]
jobs:
implement:
if: contains(github.event.label.name, 'claude-implement')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Dependencies
run: npm ci
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Implement Feature
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
claude code implement-issue \
--issue ${{ github.event.issue.number }} \
--create-pr \
--run-tests \
--request-review
- name: Comment on Issue
if: success()
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ github.event.issue.number }},
body: '🤖 Implementation PR created. Please review.'
})
ドキュメント同期チェック
# .github/workflows/claude-doc-check.yml
name: Documentation Sync Check
on:
pull_request:
paths:
- 'src/api/**'
- 'src/lib/**'
jobs:
doc-check:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Check Documentation
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude code doc-audit \
--changed-files \
--check-api-docs \
--check-readme \
--output-format github
- name: Post Comment
if: failure()
uses: actions/github-script@v7
with:
script: |
github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: ${{ github.event.pull_request.number }},
event: 'REQUEST_CHANGES',
body: '📚 Documentation needs to be updated for API changes. Please update docs before merging.'
})
GitLab CI/CD 統合
クイックセットアップ
/gitlab-ci-setup
GitLab CI 設定
# .gitlab-ci.yml
stages:
- review
- security
- implement
- deploy
variables:
CLAUDE_MODEL: "sonnet"
# PR/MR Review
claude-review:
stage: review
image: node:20
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- npm install -g @anthropic/claude-code
- |
claude code review \
--mr $CI_MERGE_REQUEST_IID \
--output-format gitlab \
--post-comments
variables:
ANTHROPIC_API_KEY: $ANTHROPIC_API_KEY
# Security Audit
claude-security:
stage: security
image: node:20
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: manual
allow_failure: false
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- npm install -g @anthropic/claude-code
- |
claude code audit \
--security \
--fail-on critical,high \
--output-format junit > security-report.xml
artifacts:
reports:
junit: security-report.xml
variables:
ANTHROPIC_API_KEY: $ANTHROPIC_API_KEY
# Issue Implementation
claude-implement:
stage: implement
image: node:20
rules:
- if: $CI_PIPELINE_SOURCE == "issue"
when: manual
script:
- npm install -g @anthropic/claude-code
- npm ci
- |
claude code implement-issue \
--issue $CI_ISSUE_IID \
--create-mr \
--run-tests
variables:
ANTHROPIC_API_KEY: $ANTHROPIC_API_KEY
GITLAB_TOKEN: $GITLAB_TOKEN
GitLab Webhook 統合
MR での @claude メンション用:
# claude-mention-handler.yml
claude-mention:
stage: review
image: node:20
rules:
- if: $CI_PIPELINE_SOURCE == "chat"
script:
- npm install -g @anthropic/claude-code
- |
claude code respond-mention \
--mr $CI_MERGE_REQUEST_IID \
--comment-id $COMMENT_ID \
--context-depth 10
variables:
ANTHROPIC_API_KEY: $ANTHROPIC_API_KEY
API プロバイダーオプション
Anthropic API(直接)
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
AWS Bedrock
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-east-1
CLAUDE_PROVIDER: bedrock
CLAUDE_MODEL: anthropic.claude-3-5-sonnet-20241022-v2:0
Google Vertex AI
env:
GOOGLE_APPLICATION_CREDENTIALS: /tmp/gcp-key.json
CLAUDE_PROVIDER: vertex
CLAUDE_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
CLAUDE_REGION: us-east5
steps:
- name: Setup GCP Credentials
run: echo '${{ secrets.GCP_SA_KEY }}' > /tmp/gcp-key.json
ワークフローパターン
パターン 1:完全な PR レビュー
# Complete PR review with multiple checks
jobs:
review:
strategy:
matrix:
check: [quality, security, performance, docs]
steps:
- name: Run Check
run: |
claude code review \
--type ${{ matrix.check }} \
--pr ${{ github.event.pull_request.number }}
パターン 2:段階的セキュリティレビュー
# Different review depth based on changed files
jobs:
security:
steps:
- name: Determine Review Depth
id: depth
run: |
if git diff --name-only ${{ github.event.pull_request.base.sha }} | grep -E 'auth/|payment/|admin/'; then
echo "depth=thorough" >> $GITHUB_OUTPUT
else
echo "depth=standard" >> $GITHUB_OUTPUT
fi
- name: Security Review
run: |
claude code audit \
--security \
--depth ${{ steps.depth.outputs.depth }} \
--fail-on critical
パターン 3:Issue から PR へのパイプライン
# Complete issue to implementation pipeline
name: Issue Implementation Pipeline
on:
issues:
types: [labeled]
jobs:
analyze:
if: contains(github.event.label.name, 'auto-implement')
outputs:
complexity: ${{ steps.analyze.outputs.complexity }}
assignee: ${{ steps.analyze.outputs.assignee }}
steps:
- name: Analyze Issue
id: analyze
run: |
claude code analyze-issue \
--issue ${{ github.event.issue.number }} \
--output complexity,assignee
implement:
needs: analyze
if: needs.analyze.outputs.complexity != 'high'
steps:
- name: Implement
run: |
claude code implement-issue \
--issue ${{ github.event.issue.number }} \
--model ${{ needs.analyze.outputs.complexity == 'low' && 'haiku' || 'sonnet' }}
create-pr:
needs: implement
steps:
- name: Create PR
run: |
claude code create-pr \
--issue ${{ github.event.issue.number }} \
--assign ${{ needs.analyze.outputs.assignee }} \
--label auto-generated
セキュリティ上の考慮事項
シークレット管理
# Never expose API keys
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
# Use repository secrets, not hardcoded values
# Configure in: Settings > Secrets > Actions
権限のスコープ設定
# Minimum required permissions
permissions:
contents: read
pull-requests: write
issues: write
# Don't use: permissions: write-all
レート制限
# Add rate limiting to prevent abuse
steps:
- name: Check Rate Limit
run: |
# Check if we've exceeded daily limit
USAGE=$(cat .claude-usage 2>/dev/null || echo "0")
if [ "$USAGE" -gt 100 ]; then
echo "Rate limit exceeded"
exit 1
fi
監査ログ
# Log all Claude operations
steps:
- name: Run with Audit
run: |
claude code review \
--pr ${{ github.event.pull_request.number }} \
--audit-log .claude-audit.json
- name: Upload Audit Log
uses: actions/upload-artifact@v4
with:
name: claude-audit
path: .claude-audit.json
コスト管理
タスク別モデル選択
# Use appropriate model for task
env:
CLAUDE_MODEL: ${{ github.event.label.name == 'security-critical' && 'opus' || 'sonnet' }}
トークン予算
# Set token limits
steps:
- name: Review with Budget
run: |
claude code review \
--pr ${{ github.event.pull_request.number }} \
--max-tokens 10000
キャッシング
# Cache Claude responses for identical inputs
steps:
- name: Setup Cache
uses: actions/cache@v4
with:
path: ~/.claude-cache
key: claude-${{ hashFiles('src/**') }}
- name: Review with Cache
run: |
claude code review \
--cache-dir ~/.claude-cache \
--pr ${{ github.event.pull_request.number }}
出力フォーマット
GitHub フォーマット
run: |
claude code review \
--output-format github \
--post-comments
出力内容:
- 特定行へのインラインコメント
- PR へのサマリーコメント
- アノテーション付きチェックラン
JUnit フォーマット
run: |
claude code audit --output-format junit > report.xml
artifacts:
reports:
junit: report.xml
出力内容:
- CI 統合用の JUnit XML
- テストスイートの可視化
- 失敗の追跡
JSON フォーマット
run: |
claude code review --output-format json > review.json
# Process in subsequent steps
- name: Process Results
run: |
CRITICAL=$(jq '.issues | map(select(.severity == "critical")) | length' review.json)
if [ "$CRITICAL" -gt 0 ]; then
exit 1
fi
統合例
例 1:完全なレビューパイプライン
name: Complete PR Pipeline
on:
pull_request:
types: [opened, synchronize]
jobs:
# Stage 1: Quick checks
quick-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
- run: npm run typecheck
# Stage 2: Claude review (only if quick checks pass)
claude-review:
needs: quick-checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Code Review
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude code review \
--pr ${{ github.event.pull_request.number }} \
--checks quality,patterns,security \
--post-comments
# Stage 3: Security audit (for labeled PRs)
security-audit:
needs: quick-checks
if: contains(github.event.pull_request.labels.*.name, 'needs-security-review')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Security Audit
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude code audit \
--security \
--model opus \
--fail-on critical,high
# Stage 4: Tests (parallel with Claude review)
tests:
needs: quick-checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test -- --coverage
例 2:夜間セキュリティスキャン
name: Nightly Security Scan
on:
schedule:
- cron: '0 2 * * *' # 2 AM daily
jobs:
security-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Claude Code
run: npm install -g @anthropic/claude-code
- name: Full Security Audit
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude code audit \
--security \
--model opus \
--depth thorough \
--output-format json > security-report.json
- name: Create Issue if Findings
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const report = JSON.parse(fs.readFileSync('security-report.json'));
if (report.critical > 0 || report.high > 0) {
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `🚨 Security Issues Found (${report.critical} critical, ${report.high} high)`,
body: '## Security Scan Results\n\n' + report.summary,
labels: ['security', 'urgent']
});
}
トラブルシューティング
API キーの問題
# Verify API key is set
- name: Check API Key
run: |
if [ -z "$ANTHROPIC_API_KEY" ]; then
echo "Error: ANTHROPIC_API_KEY not set"
exit 1
fi
レート制限
# Add retry with backoff
- name: Review with Retry
uses: nick-fields/retry@v2
with:
timeout_minutes: 10
max_attempts: 3
retry_wait_seconds: 60
command: |
claude code review --pr ${{ github.event.pull_request.number }}
大規模 PR
# Split review for large PRs
- name: Check PR Size
id: size
run: |
FILES=$(gh pr view ${{ github.event.pull_request.number }} --json files -q '.files | length')
if [ "$FILES" -gt 50 ]; then
echo "large=true" >> $GITHUB_OUTPUT
fi
- name: Review Large PR
if: steps.size.outputs.large == 'true'
run: |
claude code review \
--pr ${{ github.event.pull_request.number }} \
--batch-size 10 \
--summary-only
ベストプラクティス
1. 小さく始める
# Begin with code review only
- run: claude code review --pr ${{ github.event.pull_request.number }}
# Add more features gradually
# - Security audit
# - Issue implementation
# - Documentation checks
2. 適切なモデルを使用する
# Match model to task
# Quick review: haiku
# Standard review: sonnet
# Security/critical: opus
env:
CLAUDE_MODEL: ${{ contains(github.event.pull_request.labels.*.name, 'security') && 'opus' || 'sonnet' }}
3. 期待値を設定する
<!-- Add to PR template -->
## AI レビューについて
この PR は Claude Code によってレビューされます。
レビュー結果は提案です。最終判断はあなた自身で行ってください。
critical/high と判定されたセキュリティ問題はマージをブロックします。
4. 使用量を監視する
# Track usage for cost management
- name: Log Usage
run: |
echo "$(date): PR ${{ github.event.pull_request.number }}" >> .claude-usage.log
gh api repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
--method POST \
--field body="Claude review completed. Tokens used: $(cat .claude-tokens)"
はじめに
今日から:
/github-action-setupまたは/gitlab-ci-setupを実行- リポジトリの secrets に API キーを追加
- 1 つの PR でテスト
今週中に:
- レビュー設定をカスタマイズ
- 機密性の高いパスにセキュリティ監査を追加
- レビューコメントについてチームをトレーニング
今月中に:
- パイプラインへの完全統合
- Issue 自動実装の追加
- レビュー品質の測定
CI/CD 統合により、Claude Code を自動化ワークフローに組み込めます。すべての PR が一貫した徹底的なレビューを受け、すべての Issue が自動トリアージされます。チームは本当に重要なことに集中できます。
注意: このガイドは、コミュニティで開発された統合パターンと概念的なワークフローを説明しています。示されている特定の CLI コマンド(例:claude code review、claude code audit)は、このような統合がどのように機能するかを示す例示的なものです。実際の実装には、カスタムスクリプトや Claude API の直接使用が必要になる場合があります。
参考資料:
- Claude Code 公式ドキュメント - Claude Code の公式機能
- GitHub Actions ドキュメント - GitHub の CI/CD プラットフォーム
- GitLab CI/CD ドキュメント - GitLab の CI/CD プラットフォーム
- Claude API ドキュメント - カスタム統合の構築用