跳至主要內容

CI/CD 整合:在你的 Pipeline 中使用 Claude Code

將 Claude Code 整合到 GitHub Actions 和 GitLab CI/CD。在你的開發 pipeline 中自動化 PR 審查、Issue 實作、安全稽核和文件更新。

2026 年 10 月 21 日更新: 本指南先前使用的是 claude code review、/github-action-setup 這類並不存在的 CLI 指令。現在所有工作流程都改以真正的整合方式撰寫:官方的 anthropics/claude-code-action@v1 GitHub Action、Anthropic 文件中的 GitLab CI/CD job,以及非互動的 claude -p 執行。同時補上 v2.1.302 起 -p 執行會以 Manual 模式啟動、--permission-prompts none,以及目前的模型。

Claude Code 不只是本機開發工具——它可以整合進你的 CI/CD pipeline,自動進行 PR 審查、Issue 實作、安全稽核等工作。

本指南涵蓋 GitHub Actions 與 GitLab CI/CD 的整合模式。

為什麼要做 CI/CD 整合?

傳統 CI/CD:
└── 執行測試
└── 執行 linting
└── 部署(若通過)

加入 Claude Code:
└── 執行測試
└── 執行 linting
└── AI 驅動的程式碼審查
└── 自動化安全稽核
└── 文件同步檢查
└── Issue 自動分類
└── 從 Issue 實作功能
└── 部署(若全部通過)

該用哪一種整合

你想要使用
@claude 提及與從 Issue 到 PR 的工作Claude Code GitHub Action(anthropics/claude-code-action@v1)
每個 PR 自動審查,不用寫 workflow 檔Code Review,自 2026 年 10 月 15 日起對 Team 與 Enterprise GA
在任何 pipeline 中自訂腳本步驟非互動的 claude -p
GitLab pipelineGitLab CI/CD 整合(beta,由 GitLab 維護)

GitHub Actions 整合

快速設定

在你要連接的儲存庫中,於 Claude Code 內執行:

/install-github-app

Claude Code 會安裝 Claude GitHub App、把憑證存成儲存庫 secret(API 金鑰為 ANTHROPIC_API_KEY,用 claude setup-token 產生的訂閱 token 則是 CLAUDE_CODE_OAUTH_TOKEN)、推送一個含有你選擇之 workflow 檔案的分支,並開啟一個待你合併的 PR。此指令僅支援 github.com 儲存庫,需要管理員權限,並且要先用 gh auth login 登入 GitHub CLI。

想手動設定?安裝 Claude GitHub App、自行加入 secret,再把 examples/claude.yml 複製到 .github/workflows/。

PR 自動審查工作流程

這個 action 會執行內建的 code-review plugin,並把發現以行內評論張貼出來:

# .github/workflows/claude-code-review.yml
name: Code Review

on:
  pull_request:
    types: [opened, synchronize, ready_for_review, reopened]

jobs:
  review:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 1

      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          plugin_marketplaces: "https://github.com/anthropics/claude-code.git"
          plugins: "code-review@claude-code-plugins"
          prompt: "/code-review:code-review --comment ${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
          claude_args: '--allowedTools "mcp__github_inline_comment__create_inline_comment"'

有兩行決定審查結果會去哪裡:

  • --comment 會把審查張貼到 PR 上:每個發現一則行內評論,沒有發現時則是一則總結評論。不加這個參數,發現只會留在 workflow 執行日誌裡。
  • claude_args 必須指名行內評論工具,因為只有在 --allowedTools 列出它時,action 才會啟動那個 MCP 伺服器。

Claude 會略過草稿與已關閉的 PR、它判定過於瑣碎而不需審查的 PR,以及它已經留過評論的 PR。在公開儲存庫中,GitHub 不會把 secret 提供給來自 fork 的 PR,所以審查只會在同一儲存庫的分支上執行。

若想要更深入的多代理審查,可在獨立的 job 中執行 claude ultrareview <pr>,或改用託管的 Code Review 產品,它完全不需要 workflow 檔。

Issue 自動分類工作流程

提供 prompt 會讓 action 進入自動化模式,不必等待 @claude 提及即可執行:

# .github/workflows/claude-issue-triage.yml
name: Claude Issue Triage

on:
  issues:
    types: [opened]

jobs:
  triage:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
      id-token: write

    steps:
      - uses: actions/checkout@v6

      - uses: anthropics/claude-code-action@v1
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Read issue #${{ github.event.issue.number }} in ${{ github.repository }}.
            Decide which existing labels apply, estimate complexity as low, medium, or high,
            and add the labels with a short comment explaining the triage.
          claude_args: |
            --model claude-sonnet-5-5
            --max-turns 10
            --allowedTools "Bash(gh issue view *),Bash(gh issue edit *),Bash(gh issue comment *),Bash(gh label list *)"

在自動化模式下,Claude 在你授權之前沒有 shell 或 GitHub API 存取權:請在 --allowedTools 中列出工具,或透過 settings 輸入設定 permissions.allow 規則。此處的允許清單是限定範圍的 gh 指令,GitHub 託管的 runner 本身就提供這個工具;若偏好在唯讀步驟使用 action 官方文件列出的 GitHub MCP 工具(例如 mcp__github__list_issues 與 mcp__github__list_commits),用法也相同。

Issue 自動實作工作流程

# .github/workflows/claude-implement-issue.yml
name: Claude Implement Issue

on:
  issues:
    types: [labeled]

jobs:
  implement:
    if: github.event.label.name == 'claude-implement'
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      issues: write
      id-token: write

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - run: npm ci

      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Implement issue #${{ github.event.issue.number }} on a new branch,
            run the test suite, and open a pull request that closes the issue.
          claude_args: |
            --model claude-opus-5-5
            --max-turns 40
            --allowedTools "Bash,Read,Edit"

這個 action 預設以 Claude GitHub App 的身分驗證,因此它推送的 commit 會正常觸發你其他的 workflow。如果你傳入 github_token: ${{ secrets.GITHUB_TOKEN }},GitHub 就不會觸發那些 workflow——請移除它,或改用自訂的 App token。

文件同步檢查

# .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:
      contents: read
      pull-requests: write
      id-token: write

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: anthropics/claude-code-action@v1
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Compare the API changes in this pull request with docs/ and README.md.
            If documentation is missing or now wrong, post one PR comment listing
            exactly what to update. If the docs are in sync, say so and stop.
          claude_args: |
            --max-turns 15
            --allowedTools "Bash(git diff *),Read,Glob,Grep,Bash(gh pr comment *)"

GitLab CI/CD 整合

Claude Code 的 GitLab CI/CD 整合目前是 beta,由 GitLab 維護。它沒有設定用的斜線指令:你只需要新增一個遮蔽的 CI/CD 變數和一個 job。

快速設定

  1. 在 Settings → CI/CD → Variables 新增遮蔽變數 ANTHROPIC_API_KEY。
  2. 在 .gitlab-ci.yml 中新增 Claude job。

GitLab CI 設定

# .gitlab-ci.yml
stages:
  - ai

claude:
  stage: ai
  image: node:24-alpine3.21
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  variables:
    GIT_STRATEGY: fetch
  before_script:
    - apk update
    - apk add --no-cache git curl bash
    - curl -fsSL https://claude.ai/install.sh | bash
    # 安裝程式會把 claude 放在 ~/.local/bin,但此 image 的 PATH 不含該目錄
    - export PATH="$HOME/.local/bin:$PATH"
  script:
    # 選用:若你的環境提供 GitLab MCP 伺服器,可在此啟動
    - /bin/gitlab-mcp-server || true
    # AI_FLOW_* 變數來自帶有 context payload 的 web/API 觸發
    - echo "$AI_FLOW_INPUT for $AI_FLOW_CONTEXT on $AI_FLOW_EVENT"
    - >
      claude
      -p "${AI_FLOW_INPUT:-'Review this MR and implement the requested changes'}"
      --permission-mode acceptEdits
      --allowedTools "Bash Read Edit Write mcp__gitlab"
      --debug

第二個 job 可以執行不張貼任何內容、但在有發現時讓 pipeline 失敗的審查:

claude-review:
  stage: ai
  image: node:24-alpine3.21
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  before_script:
    - apk add --no-cache git curl bash jq
    - curl -fsSL https://claude.ai/install.sh | bash
    - export PATH="$HOME/.local/bin:$PATH"
  script:
    - >
      claude -p "/code-review high"
      --permission-mode dontAsk
      --output-format json > review.json
    - jq -r '.result' review.json
  artifacts:
    when: always
    paths:
      - review.json

GitLab 的提及處理

GitLab 沒有內建的 @claude 觸發機制。若要回應提及,請為你自行執行的監聽服務新增 Comments (notes) 專案 webhook,並在評論含有 @claude 時,讓該服務以 AI_FLOW_INPUT 與 AI_FLOW_CONTEXT 呼叫 GitLab 的 pipeline trigger API。上面的 job 已經會讀取這些變數。

API 供應商選項

Anthropic API(直接使用)

# GitHub Action 輸入
with:
  anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

# 或使用 `claude setup-token` 產生的 Claude 訂閱 token
with:
  claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}

若不想儲存長效 secret,可用 anthropic_federation_rule_id 與 anthropic_organization_id 輸入搭配 workload identity federation 驗證,並給該 job id-token: write 權限。

Amazon Bedrock

# GitHub Action:透過你的 AWS 帳號進行推論
with:
  use_bedrock: "true"

# 非互動執行改用環境變數
env:
  CLAUDE_CODE_USE_BEDROCK: "1"
  AWS_REGION: us-east-1

Google Cloud’s Agent Platform

# GitHub Action
with:
  use_vertex: "true"

# 非互動執行
env:
  CLAUDE_CODE_USE_VERTEX: "1"
  CLOUD_ML_REGION: global
  ANTHROPIC_VERTEX_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}

Microsoft Foundry 則使用 use_foundry: "true" 與 CLAUDE_CODE_USE_FOUNDRY=1。這三家供應商都透過 OIDC 身分聯盟驗證,因此儲存庫中不會存放靜態雲端憑證。設定方式請見雲端供應商文件。

工作流程模式

模式 1:依標籤決定審查深度

/code-review 接受 effort 等級,所以同一個 workflow 可以在 PR 帶有特定標籤時審得更深:

jobs:
  review:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      id-token: write
    steps:
      - uses: actions/checkout@v6
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          plugin_marketplaces: "https://github.com/anthropics/claude-code.git"
          plugins: "code-review@claude-code-plugins"
          prompt: >
            /code-review:code-review
            ${{ contains(github.event.pull_request.labels.*.name, 'security') && 'xhigh' || 'medium' }}
            --comment ${{ github.repository }}/pull/${{ github.event.pull_request.number }}
          claude_args: '--allowedTools "mcp__github_inline_comment__create_inline_comment"'

模式 2:敏感路徑的安全審查

jobs:
  security:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      id-token: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Check for sensitive paths
        id: paths
        run: |
          if git diff --name-only origin/${{ github.base_ref }}... | grep -E 'auth/|payment/|admin/'; then
            echo "sensitive=true" >> $GITHUB_OUTPUT
          fi

      - if: steps.paths.outputs.sensitive == 'true'
        uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: "/security-review"
          claude_args: '--model claude-opus-5-5 --effort high'

模式 3:從 Issue 到 PR 的流程

name: Issue Implementation Pipeline

on:
  issues:
    types: [labeled]

jobs:
  triage:
    if: github.event.label.name == 'auto-implement'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
      id-token: write
    steps:
      - uses: actions/checkout@v6
      - uses: anthropics/claude-code-action@v1
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Read issue #${{ github.event.issue.number }} and label it
            complexity:low, complexity:medium, or complexity:high.
          claude_args: '--allowedTools "Bash(gh issue view *),Bash(gh issue edit *)"'

  implement:
    needs: triage
    if: "!contains(github.event.issue.labels.*.name, 'complexity:high')"
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    steps:
      - uses: actions/checkout@v6
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: "Implement issue #${{ github.event.issue.number }} and open a pull request."
          claude_args: '--max-turns 40 --allowedTools "Bash,Read,Edit"'

高複雜度的 Issue 仍交給人處理。模型選擇請明確寫在 claude_args 中,不要讓各個 job 各自猜測。

安全性考量

Secret 管理

# 永遠不要提交金鑰;一律引用儲存庫或組織層級的 secret
with:
  anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

# 設定位置:Settings > Secrets and variables > Actions

claude setup-token 產生的 OAuth token 綁定到個人的訂閱,所以要共用組織層級的 secret 時,請改用 Claude Console 的 API 金鑰,或使用完全不儲存 secret 的 workload identity federation。

權限範圍

# 最小必要權限;action 預設的 GitHub App 驗證需要 id-token: write
permissions:
  contents: read
  pull-requests: write
  issues: write
  id-token: write

# 不要使用:permissions: write-all

這個 action 也會檢查觸發者:在 issue 與 PR 事件中,使用者必須具備寫入權限(或列在 allowed_non_write_users 中並搭配你自己的 github_token),而 bot 觸發者除非列在 allowed_bots,否則會被拒絕。這項檢查同樣適用於排程執行,因為 GitHub 會把排程執行歸屬到某個儲存庫使用者。

鎖緊非互動執行

自 v2.1.302 起,沒有設定權限模式的 claude -p 與 Agent SDK 執行會以 Manual 模式啟動,任何需要核准的動作會被拒絕,而不是悄悄放行。請在 CI 中明確指定:

# 精確的允許清單,其他一律不執行
claude -p "Run the test suite and summarize failures" \
  --permission-mode dontAsk \
  --allowedTools "Bash(npm test *),Read"

# 由分類器審查、且無人可回答提示的自主執行
claude -p "Update the dependency pins and run the tests" \
  --permission-mode auto \
  --permission-prompts none

--permission-prompts none(v2.1.259+)會在沒有主機可回應時直接拒絕所有會跳出提示的動作,而不是等待。再加上 --bare,可略過 hooks、skills、plugins、MCP 伺服器與 CLAUDE.md 的自動探索,讓同事本機的設定無法影響 CI 的執行內容。至於共用機器上的評估環境,--restricted 會移除會執行程式碼的工具與 WebFetch,並忽略使用者、專案與本機設定。

稽核日誌

steps:
  - name: Review with a saved transcript
    run: |
      claude -p "/code-review high" \
        --permission-mode dontAsk \
        --output-format json > claude-review.json

  - name: Upload the run record
    if: always()
    uses: actions/upload-artifact@v4
    with:
      name: claude-review
      path: claude-review.json

若要取得全組織的可視性,請以 OpenTelemetry(CLAUDE_CODE_ENABLE_TELEMETRY=1)匯出指標與事件,而不是逐個 job 解析日誌。

成本管理

依任務選擇模型

# 在 claude_args 中依任務選模型
claude_args: >
  --model ${{ contains(github.event.pull_request.labels.*.name, 'security')
  && 'claude-opus-5-5' || 'claude-sonnet-5-5' }}

Opus 5.5(每百萬輸入/輸出 token $4/$20)是預設模型;Sonnet 5.5($2/$10)適合日常審查,Haiku 5.5(100K token 以內的提示為 $0.10/$0.50)適合機械性的檢查。Opus 5.5、Sonnet 5.5 與 Haiku 5.5 預設以 medium effort 執行——請先用 --effort 調整,而不是急著換更大的模型。

預算與回合數上限

steps:
  - name: Review with a spend cap
    run: |
      claude -p "/code-review" \
        --permission-mode dontAsk \
        --max-turns 20 \
        --max-budget-usd 2.00

--max-budget-usd 會在列印模式執行的預估花費達到上限時停止,並把 subagent 的花費一併計入;請預留一些餘裕,因為實際花費可能略微超過上限。--max-turns 則限制代理回合數。兩者都僅適用於列印模式,在 GitHub Action 中要放進 claude_args。

Prompt 快取

Claude Code 會自動快取提示,而快取讀取遠比全新輸入便宜(例如 Opus 5.5 為每百萬 token $0.20)。要讓快取更有效,可以保持 CLAUDE.md 精簡、把提示寫得具體以減少回合數,並用 GitHub 的 concurrency 控制避免同一分支重複執行。

輸出格式

PR 行內評論

prompt: "/code-review:code-review --comment ${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
claude_args: '--allowedTools "mcp__github_inline_comment__create_inline_comment"'

輸出:在相關行上的行內評論;若 Claude 沒有發現問題,則是一則總結評論。

JSON

claude -p "/code-review" --permission-mode dontAsk --output-format json > review.json

這份 payload 包含最終的 result 文字、total_cost_usd、各模型的成本明細,以及 permission_denials。你可以在後續步驟中據此判斷:

COST=$(jq -r '.total_cost_usd' review.json)
echo "Review cost: $COST USD"

以 Schema 取得結構化輸出

當後續步驟需要可直接解析的欄位時,可用 JSON Schema 驗證執行結果:

claude -p "List the blocking review findings" \
  --permission-mode dontAsk \
  --json-schema '{"type":"object","properties":{"blocking":{"type":"integer"}},"required":["blocking"]}'

串流

claude -p "Fix the failing tests" --output-format stream-json --include-partial-messages

適合長時間執行的 job:你會在日誌中看到進度,而不是最後才一次輸出。

整合範例

範例 1:完整的審查流程

name: Complete PR Pipeline

on:
  pull_request:
    types: [opened, synchronize]

jobs:
  # 階段 1:快速檢查
  quick-checks:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: npm ci
      - run: npm run lint
      - run: npm run typecheck

  # 階段 2:Claude 審查(僅在快速檢查通過後)
  claude-review:
    needs: quick-checks
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
      issues: read
      id-token: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 1
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          plugin_marketplaces: "https://github.com/anthropics/claude-code.git"
          plugins: "code-review@claude-code-plugins"
          prompt: "/code-review:code-review --comment ${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
          claude_args: '--allowedTools "mcp__github_inline_comment__create_inline_comment"'

  # 階段 3:帶標籤 PR 的安全審查
  security-review:
    needs: quick-checks
    if: contains(github.event.pull_request.labels.*.name, 'needs-security-review')
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      id-token: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: "/security-review"
          claude_args: '--model claude-opus-5-5 --effort high'

  # 階段 4:測試(與 Claude 審查並行)
  tests:
    needs: quick-checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - run: npm ci
      - run: npm test -- --coverage

範例 2:夜間安全掃描

name: Nightly Security Scan

on:
  schedule:
    - cron: '0 2 * * *'  # 每天凌晨 2 點

jobs:
  security-scan:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
      id-token: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: anthropics/claude-code-action@v1
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Audit this repository for security vulnerabilities introduced in the
            last 24 hours of commits. If you find critical or high severity issues,
            open one GitHub issue titled "Security scan findings" labeled
            security,urgent, listing each finding with its severity and file.
            If you find none, do nothing.
          claude_args: |
            --model claude-opus-5-5
            --effort high
            --max-turns 30
            --allowedTools "Bash(git log *),Bash(git diff *),Read,Glob,Grep,Bash(gh issue create *)"

GitHub 只會從預設分支執行排程 workflow,公開儲存庫在 60 天沒有活動後會停用排程,而且排程執行會被歸屬到某個儲存庫使用者——如果那個帳號是 bot,請把它加入 allowed_bots。

疑難排解

Claude 沒有回應 @claude

  • 確認儲存庫已安裝 Claude GitHub App,且 workflow 已啟用
  • 確認 secret 存在,且與你傳入的輸入相符(anthropic_api_key 或 claude_code_oauth_token)
  • 確認評論中的 @claude 是完整單字,而不是 /claude 或 @claude-bot
  • 確認留言者對該儲存庫有寫入權限

Claude 的 commit 沒有觸發 CI

使用預設 GITHUB_TOKEN 建立的 commit 不會觸發 GitHub workflow。請移除 github_token: ${{ secrets.GITHUB_TOKEN }},讓 action 以 Claude GitHub App 身分驗證,或改傳自訂的 App token。

驗證錯誤

# 在除錯 workflow 之前,先在本機測試憑證
claude auth status --text
claude -p "say ok"

Bedrock、Agent Platform 與 Foundry 請參考雲端供應商的疑難排解章節。

非互動執行中的工具呼叫被拒絕

沒有指定權限模式的 -p 執行會以 Manual 模式啟動,因此未獲核准的呼叫會被拒絕。請把需要的工具加入 --allowedTools,或傳入 --permission-mode auto(由分類器審查)或 dontAsk(僅允許清單)。使用 --output-format stream-json 時,這些拒絕會以 permission_denied 訊息出現,並列在最終結果的 permission_denials 中。

大型 PR

- name: Review large PRs with a turn cap
  run: |
    claude -p "/code-review --max-findings 20" \
      --permission-mode dontAsk \
      --max-turns 30 \
      --max-budget-usd 5.00

最佳實踐

1. 從小開始

# 先只做審查
prompt: "/code-review:code-review --comment ${{ github.repository }}/pull/${{ github.event.pull_request.number }}"

# 之後再加入:安全審查、Issue 分類、功能實作

2. 在 CLAUDE.md 中定義標準

Claude 每次執行都會讀取儲存庫的 CLAUDE.md(沒有 CLAUDE.md 時則讀 AGENTS.md),所以請把審查標準、程式碼風格與專案慣例寫在那裡,而不是在每個 workflow 的提示中重複。同時保持精簡——它每次執行都會消耗 token。

3. 設定好期待

<!-- 加入 PR 模板 -->
## AI 審查說明
本 PR 會由 Claude Code 審查。
審查發現屬於建議——請自行判斷。
標記為 critical 或 high 的安全發現必須在合併前解決。

4. 監控用量

從 JSON 輸出的 total_cost_usd 追蹤每次執行的成本,並透過 OpenTelemetry 指標與分析儀表板彙整全組織的用量,而不是零散的日誌檔。

開始行動

今天:

  1. 執行 /install-github-app(GitHub),或在 .gitlab-ci.yml 新增 Claude job(GitLab)
  2. 把 ANTHROPIC_API_KEY 或 CLAUDE_CODE_OAUTH_TOKEN 存成 secret
  3. 在一個 PR 上測試

這週:

  1. 加入審查工作流程並調整其 effort 等級
  2. 為敏感路徑加入安全審查
  3. 為每個 job 設定 --max-turns 與 --max-budget-usd

這個月:

  1. 加入 Issue 分類與實作
  2. 決定團隊要用這個 action 還是託管的 Code Review 產品
  3. 匯出用量指標並檢視成本

CI/CD 整合把 Claude Code 帶進你的自動化流程。每個 PR 都能得到一致而徹底的審查,每個 Issue 都能被自動分類,團隊則專注在真正重要的事情上。

參考資料: