💬 hippo:這是 Claude Code 官方文件精讀系列的其中一篇。


一、這個功能是什麼

Claude Code GitHub Actions 是一個官方 Action,讓你在 PR 或 Issue 留言區裡用 @claude 提及來呼叫 AI 幫你處理任務。它能做的事情包括:自動建立 PR、把 Issue 轉成程式碼、修復 Bug、審查程式碼品質。

和 GitHub 內建的 Code Review 不同,這個 Action 需要用 @claude 手動觸發,不會自動在你的每個 PR 上留言。Claude 會讀取儲存庫根目錄的 CLAUDE.md 來理解你的專案規範,所以它輸出的程式碼風格是由你決定的,不是隨意發揮。

程式碼執行在 GitHub 的伺服器上,不經過第三方,企業也可以串接 AWS Bedrock 或 Google Vertex AI 來控制資料駐留與計費。


二、官方教學精讀

2.1 安裝與設定

最簡單的方式是在 Claude Code 終端機裡執行斜線指令(Skills,即內建的快捷指令):

1
/install-github-app

它會引導你完成 App 安裝與 API Key 設定。如果自動安裝失敗,手動三步驟:

  1. 前往 https://github.com/apps/claude 安裝 GitHub App
  2. 在儲存庫的 Settings → Secrets and variables → Actions 裡新增 ANTHROPIC_API_KEY
  3. 建立 workflow 檔案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
name: Claude Code

on:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]

permissions:
contents: write
pull-requests: write
issues: write

jobs:
claude:
if: contains(github.event.comment.body, '@claude')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

注意 permissions 必須明確宣告寫入權限。GitHub App 也需要 Contents、Issues、Pull requests 三項讀寫權限,否則 Claude 無法推送程式碼與建立 PR。

2.2 Action 參數詳解與 v1 遷移

v1 版本(GA)大幅簡化了設定:prompt 統一了舊版的 direct_prompt 與 override_prompt,claude_args 統一了 max_turns、model、custom_instructions 等一系列參數,mode 參數則直接移除並改為自動偵測。

Beta → v1 遷移對照:

舊參數 v1 參數 說明
mode 移除 自動偵測,無需設定
direct_prompt prompt 參數名稱變更
override_prompt prompt + GitHub 變數 合併到 prompt
custom_instructions claude_args: --append-system-prompt 移入 claude_args
max_turns claude_args: --max-turns 移入 claude_args
model claude_args: --model 移入 claude_args
allowed_tools claude_args: --allowedTools 移入 claude_args
disallowed_tools claude_args: --disallowedTools 移入 claude_args
claude_env settings JSON 格式設定

Action 完整參數表:

參數 說明 必填
anthropic_api_key Anthropic API 金鑰 是(Bedrock/Vertex 除外)
prompt 給 Claude 的指令,純文字或斜線指令 否(留言情境可省略)
claude_args 傳遞給 Claude Code CLI 的參數字串 否
github_token GitHub API 權杖,預設 GITHUB_TOKEN 否
trigger_phrase 觸發詞,預設 @claude 否
use_bedrock 使用 AWS Bedrock 否
use_vertex 使用 Google Vertex AI 否
settings JSON 格式設定(取代舊版 claude_env) 否

claude_args 裡最常用的幾個 CLI 參數:

1
2
3
4
5
6
claude_args: |
--max-turns 10
--model claude-sonnet-4-6
--append-system-prompt "Follow our coding standards"
--allowedTools Read,Write,Edit,Bash,Glob,Grep
--debug

prompt 參數還支援斜線指令(Skills),例如 prompt: "/review" 就能觸發 Claude Code 內建的程式碼審查技能。除了在 prompt 裡指定指令,你也可以在儲存庫的 CLAUDE.md 中定義專案級規範,這是自訂 Claude 行為的首要方式。workflow 層級的臨時指令用 prompt,專案層級的持久規範用 CLAUDE.md,兩者會疊加生效。

2.3 典型使用情境

程式碼審查工作流程——PR 開啟或更新時自動 Review:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
name: Code Review
on:
pull_request:
types: [opened, synchronize]

jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: "Review this pull request for code quality and security"
claude_args: "--max-turns 5"

排程任務——用 cron 觸發日報或週報:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
name: Daily Report
on:
schedule:
- cron: "0 9 * * *"

jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: "Generate a summary of yesterday's commits and open issues"
claude_args: "--model opus"

留言區常用指令:

  • @claude implement this feature —— 讓 Claude 實作功能
  • @claude fix the failing test —— 修復問題
  • @claude how should I handle error cases? —— 提問諮詢

本質上,prompt + claude_args 的組合能涵蓋你想要的任何工作流程。官方在 examples 目錄 裡提供了更多範本。

2.4 企業部署:Bedrock 與 Vertex AI

企業可以用 AWS Bedrock 或 Google Vertex AI 取代 Anthropic API,透過 OIDC(OpenID Connect,一種身分聯合驗證協定)驗證,不需要 API Key。兩者的關鍵設定差異如下:

AWS Bedrock——需要先設定 AWS 憑證:

1
2
3
4
5
6
7
8
9
10
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::ACCOUNT_ID:role/ClaudeCodeRole
aws-region: us-east-1

- name: Run Claude Code
uses: anthropics/claude-code-action@v1
with:
use_bedrock: true

Google Vertex AI——需要先向 GCP 驗證身分:

1
2
3
4
5
6
7
8
9
10
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: 'projects/NUMBER/locations/global/workloadIdentityPools/POOL/providers/PROVIDER'
service_account: 'claude-code@PROJECT_ID.iam.gserviceaccount.com'

- name: Run Claude Code
uses: anthropics/claude-code-action@v1
with:
use_vertex: true

兩者的共同點:都需要在 permissions 裡加上 id-token: write 來啟用 OIDC,也都不需要 anthropic_api_key。


三、實戰經驗

💬 hippo:以下是我實際使用時的踩坑經驗。

CI 不觸發 Claude 的提交:這是最常見的坑。預設的 GITHUB_TOKEN 觸發的事件不會再次觸發 workflow,所以 Claude 的 push 不會跑你的 CI。解決方案是使用 GitHub App 產生的 token,而不是預設的 GITHUB_TOKEN。

CLAUDE.md 是關鍵:我強烈建議在儲存庫根目錄放一份 CLAUDE.md,定義程式碼風格、審查標準、專案規則。Claude 會嚴格遵循裡面的規範,沒有這個檔案 Claude 就只能靠猜。prompt 適合臨時指令,CLAUDE.md 適合持久規範,兩者疊加生效。

成本控制三招:

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 限制迭代輪數
claude_args: "--max-turns 5"

# 2. 設定 workflow 逾時
jobs:
claude:
timeout-minutes: 10

# 3. 並行控制,同一 PR 只跑一個 Claude 任務
concurrency:
group: claude-${{ github.event.issue.number || github.event.pull_request.number }}
cancel-in-progress: true

四、常見問題

Q: Claude 不回應 @claude 指令?

A: 依序排查:App 是否已安裝到儲存庫 → workflow 檔案是否在 .github/workflows/ 下 → ANTHROPIC_API_KEY 是否已設定在 Secrets 裡 → 確認寫的是 @claude 而不是 /claude。

Q: Claude 的提交不觸發我的 CI?

A: 使用 GitHub App 或自訂 App 產生 token,不要用預設的 Actions 使用者。同時在 workflow 的觸發條件裡確認包含了 pull_request 事件。

Q: API 驗證失敗?

A: 直連 Anthropic API 的話檢查 Key 的有效性與餘額;用 Bedrock/Vertex 的話檢查雲端服務憑證與 IAM 角色組態。

Q: 成本由什麼組成?

A: 兩部分:GitHub Actions 分鐘數(執行器本身的開銷)+ API Token 消耗(取決於任務複雜度與程式碼庫大小)。控制 --max-turns 與設定逾時是主要的省錢手段。


五、小結

核心流程就是:安裝 App → 設定 Key → 寫 workflow → @claude 觸發。關鍵設定項目:CLAUDE.md 定義專案規範,--max-turns 控制成本,企業使用者用 Bedrock/Vertex 控制資料駐留。更多範例見官方 examples 目錄。


系列導覽


本文精讀自 Claude Code GitHub Actions - Claude Code Docs

最後更新:2026-03-31