背景・目的
Claoud APIとGitHubを統合して、自動化してみたいと思います。
まとめ
下記に特徴をまとめます。
特徴 | 説明 |
---|---|
概要 | Claoude CodeをGitHubワークフローに統合して、コードレビュー、PR管理、Issueのトリアージを自動化できる |
できること | Claudeが必要な変更をすべて加えた完全なPRを作成する Issue をコードに変換 |
概要
GitHub Actions
下記を基に整理します。
- Claoude CodeをGitHubワークフローに統合して、コードレビュー、PR管理、Issueのトリアージを自動化できる
- メンションするだけで、Claudeがコード解析、PR作成、機能実装、バグ修正をプロジェクトの標準に沿って実行する
- Claude Code GitHub Actionsは、Claude Code SDKをベースに構築されている
2025/6/8現在、Claude Code GitHub Actionsは現在ベータ版のようです。
Why use Claude Code GitHub Actions?
- Instant PR creation: Describe what you need, and Claude creates a complete PR with all necessary changes
- Automated code implementation: Turn issues into working code with a single command
- Follows your standards: Claude respects your CLAUDE.md guidelines and existing code patterns
- Simple setup: Get started in minutes with our installer and API key
- Secure by default: Your code stays on Github’s runners
- Claudeが必要な変更をすべて加えた完全なPRを作成する
- Issue をコードに変換
- CLAUDE.mdガイドラインと既存のコードパターンを使う
- APIキーを使うことですぐに始められる
- デフォルトで安全
What can Claude do?
- Claude Code Action
- GitHub Actionsワークフロー内でClaude Codeを実行できる
- Claude Codeをベースに任意のカスタムワークフローを構築できる
- Claude Code Action (Base)
- Claude でカスタム GitHub ワークフローを構築するための基盤
- この拡張可能なフレームワークにより、Claude の機能を最大限に活用し、カスタマイズされた自動化を作成できる
Example use cases
Turn issues into PRs
# In an issue comment:
@claude implement this feature based on the issue description
- Claude は問題を分析し、コードを記述し、レビュー用の PR を作成する
Get implementation help
# In a PR comment:
@claude how should I implement user authentication for this endpoint?
- Claude がコードを分析し、具体的な実装ガイダンスを提供する
Fix bugs quickly
# In an issue:
@claude fix the TypeError in the user dashboard component
- Claude はバグを見つけ、修正を実施し、PR を作成する
Best practices
CLAUDE.md configuration
- CLAUDE.mdリポジトリのルートにファイルを作成し、下記を定義する
- コードスタイルのガイドライン
- レビュー基準
- プロジェクト固有のルール
- 推奨パターンを定義
- このファイルは、Claude がプロジェクト標準を理解するためのガイドとなる
Security considerations
API キーをリポジトリに直接コミットしない
- ${{ secrets.ANTHROPIC_API_KEY }}ワークフロー ファイルで API キーを直接ハードコーディングするのではなく、常に GitHub Secrets (例: ) を使用する
Optimizing performance
- issue templatesを使用してコンテキストを提供し、CLAUDE.md簡潔かつ焦点を絞ったまま、ワークフローに適切なタイムアウトを構成する
CI costs
- Claude Code GitHub Actions を使用する場合は、関連するコストに注意が必要
GitHub Actions costs
- Claude CodeはGitHubホストランナー上で実行され、GitHub Actionsの分数を消費する
API costs
- クロードとのやり取りごとに、プロンプトと応答の長さに基づいてAPIトークンが消費される
- トークンの使用量はタスクの複雑さとコードベースのサイズによって異なる
Cost optimization tips
- 特定の@claudeコマンドを使用して不要なAPI呼び出しを削減する
- max_turns過度な反復を防ぐために適切な制限を設定する
- timeout_minutesワークフローの暴走を避けるために適切な設定をする
- GitHubの同時実行制御を使用して並列実行を制限することを検討する
実践
コードレビューの自動化
下記を参考に試します。
前提
- GitHub アカウントがあること
- Pythonがインストール済み
- Claude アカウントがあること
環境
- Cursorを使用しています
GitHubリポジトリの作成
- GitHubにサインインし、リポジトリを作成します
Claude APIキーの発行
- Anthropic Consoleでサインインします
- ナビゲーションペインの「API Keys」をクリックします
- 「Create Key」をクリックし、あたらしくKeyを発行します
- 次のステップでGitHub Actionsに保存するためコピーします
Claude GitHubアプリをリポジトリにインストールします
-
ttps://github.com/apps/claudeにアクセスします
Secretsの保存
-
上記で作成したリポジトリを選択します
-
Settingsをクリックします
-
Secrets and variablesをクリックし「Actions」をクリックします
-
下記を指定します
- Name: APIキー名
- Value:上記で作成したAPIキーをコピーします
レビューを自動化する
-
上記で作成したGitHubリポジトリをローカルにcloneします
% git clone git@github.com:XXXXX/claude-github-actions.git Cloning into 'claude-github-actions'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) Receiving objects: 100% (3/3), done. %
-
ブランチを作成します
% git checkout -b feat/workflow-test origin/main branch 'feat/workflow-test' set up to track 'origin/main'. Switched to a new branch 'feat/workflow-test' %
-
Cursorのワークスペースに追加します
Claude Code Action
-
.github/workflows
ディレクトリを作成します -
claude-review.yml
ファイルを作成します -
下記のコードを指定します
name: Claude PR Review on: pull_request: types: [opened, synchronize] permissions: contents: write pull-requests: write id-token: write jobs: review: runs-on: ubuntu-latest steps: - name: Checkout entire repository uses: actions/checkout@v4 with: fetch-depth: 0 # PR の差分ファイルまで取得する - name: Run Claude Code Review id: claude uses: anthropics/claude-code-action@beta with: anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} direct_prompt: | Review the PR changes. Focus on code quality, potential bugs, performance, and suggest improvements.
-
適当なファイルを用意します
def greet(name): print("Hello, " + name) def add(a, b): return a + b # 不要かもしれない関数 def unused(): pass
-
コミットし、Pushします
% git push origin feat/workflow-test Enumerating objects: 8, done. Counting objects: 100% (8/8), done. Delta compression using up to 8 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (7/7), 940 bytes | 470.00 KiB/s, done. Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0) remote: remote: Create a pull request for 'feat/workflow-test' on GitHub by visiting: remote: https://github.com/XXXX/claude-github-actions/pull/new/feat/workflow-test remote: To github.com:XXXX/claude-github-actions.git * [new branch] feat/workflow-test -> feat/workflow-test %
-
PRを作成します
- 優先度付きで書いてくれました
- 高:未使用の機能を削除し、不完全なコメントを修正
- 中: f文字列を使用するように文字列の書式を更新する
- 低: 型ヒントとドキュメントを追加する
- 修正して、再度Pushします
def greet(name: str) -> None: """Print a personalized greeting message. Args: name: The name to include in the greeting. """ print(f"Hello, {name}") def add(a: int | float, b: int | float) -> int | float: """Add two numbers together. Args: a: First number to add. b: Second number to add. Returns: The sum of a and b. """ return a + b
- 成功しました
考察
今回、AnthropicのClaude APIとGitHub Actionsを組み合わせて、Pull Requestの自動レビュー機能を構築しました。コード品質の維持・向上を支援する自動化ワークフローが実装できることがわかりました。
次回以降も試してみます。
参考