Last active
April 11, 2025 15:58
-
-
Save hkusu/df701347c38f5a51a5b907269687e933 to your computer and use it in GitHub Desktop.
Claude Code を GitHub Actions で動かす
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Claude Code Run | |
# 現状では Issue の説明およびコメントに応答する形で起動 | |
on: | |
issues: | |
types: [opened] | |
issue_comment: | |
types: [created] | |
concurrency: # 既存の処理が走っていたらキャンセル | |
group: ${{ github.workflow }}-${{ github.event.issue.number || github.run_id }} | |
cancel-in-progress: true | |
jobs: | |
claude-code-run: | |
name: Claude Code Run | |
if: |- # 現状ではプルリクのコメントは対象外としている | |
github.event.issue.pull_request == null && | |
github.event.issue.state == 'open' && | |
( | |
github.event_name == 'issues' && contains(github.event.issue.body, '@claude ') || | |
github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude ') | |
) | |
runs-on: ubuntu-latest | |
permissions: # Claude Code に何をさせるか次第 | |
pull-requests: write | |
contents: write | |
issues: write | |
env: # for GitHub CLI | |
# 標準トークンでプルリクまで作る場合はリポジトリの設定で「Allow GitHub Actions to create and approve pull requests」を有効にする必要がある | |
# またプルリクの作成を契機にした CI など、標準トークンが起こしたイベントを契機にしたワークフローは GitHub の仕様により動かせないので注意 | |
GH_REPO: ${{ github.repository }} | |
GH_TOKEN: ${{ github.token }} | |
steps: | |
# - name: Dump GitHub context # デバッグ用。利用できるコンテキストを確認 | |
# env: | |
# GITHUB_CONTEXT: ${{ toJson(github) }} | |
# run: echo "$GITHUB_CONTEXT" | |
- name: Add eyes reaction to issue # 一旦リアクションだけ返す | |
if: github.event_name == 'issues' | |
env: | |
ISSUE_NUMBER: ${{ github.event.issue.number }} | |
run: gh api --method POST "repos/{owner}/{repo}/issues/$ISSUE_NUMBER/reactions" -f 'content=eyes' | |
- name: Add eyes reaction to comment | |
if: github.event_name == 'issue_comment' | |
env: | |
COMMENT_ID: ${{ github.event.comment.id }} | |
run: gh api --method POST "repos/{owner}/{repo}/issues/comments/$COMMENT_ID/reactions" -f 'content=eyes' | |
- name: Checkout code # issue イベントの場合はここではデフォルトブランチがチェックアウトされる(Claude Code への指示でブランチを切り替えさせることは可能 | |
uses: actions/checkout@v4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Install Claude Code CLI | |
run: npm install -g @anthropic-ai/claude-code | |
- name: Run Claude Code | |
env: | |
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} # あらかじめリポジトリの secrets に登録しておく | |
USER_PROMPT: ${{ github.event_name == 'issues' && github.event.issue.body || github.event.comment.body }} | |
ISSUE_NUMBER: ${{ github.event.issue.number }} | |
run: | | |
# コミットする場合に備えて設定 | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com' | |
# 許可する操作は Claude Code に何をさせるか次第 | |
# Bash とだけ書くとコマンド全般の許可となる模様 | |
# --dangerously-skip-permissions というモードもある | |
# Edit と Replace は別物らしい? | |
# ref: https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview#automate-ci-and-infra-workflows | |
response="$(claude --print --allowedTools 'Bash(git:*)' 'Bash(gh:*)' 'Edit' 'Replace' << EOF | |
<前提条件 開始> | |
- Git コマンドや、GitHub CLI の gh コマンド が利用できますので適宜、利用してください。 | |
- あなたは今、GitHub Actions の runner 上で動いています。 | |
- リポジトリは $GH_REPO です。 | |
- このリポジトリのデフォルトブランチのソースコードは現在のワークスペースにチェックアウト済みです。 | |
- あなたとのこれまでの会話は GitHub Issue 上で行われており、次のコマンドで確認できます。 | |
- gh issue view $ISSUE_NUMBER -c | |
- 基本的に、これまでの会話の内容を確認してから作業にあたってください。 | |
- この後 @claude に続く文字列にて、私からあなたへの依頼を伝えます。 | |
- どのような作業を行ったかを簡潔に報告してください。指示がない限りは日本語でお願いします。 | |
<前提条件 終了> | |
$USER_PROMPT | |
EOF | |
)" | |
# 一応コンソールにも結果を出力 | |
echo "Claude Code's response: $response" | |
# Issue に結果をコメント | |
LF=$'\n' | |
gh issue comment ${{ github.event.issue.number }} --body "${response}${LF}${LF}🤖 Generated with [Claude Code](https://claude.ai/code)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment