实战

Claude Code GitHub Actions v1 完整指南:@claude 自动开发、PR 审查和 CI 集成

Claude Code GitHub Actions 官方文档中文整理:它能做什么、快速安装 /install-github-app、手动配置 GitHub App 和 ANTHROPIC_API_KEY、v1 相比 beta 的破坏性变更、@claude 评论触发、自动 PR 审查、Skills 调用、Daily Report 自动化、权限安全、成本控制和可直接复制的 workflow 模板。

2026/5/154分钟 阅读ClaudeEagle

Claude Code GitHub Actions 把 Claude Code 放进 GitHub 工作流。你可以在 issue 或 PR 里 @claude,让它分析代码、实现功能、修复 bug、创建 PR,或者在 CI 里自动做代码审查。


它能做什么?

典型用法:

text
@claude implement this feature based on the issue description
@claude how should I implement user authentication for this endpoint?
@claude fix the TypeError in the user dashboard component
@claude review this PR for security issues

Claude 会读取 issue/PR 上下文、遵循仓库里的 CLAUDE.md,并在 GitHub runner 上执行代码修改。

核心能力:

  • 从 issue 描述直接创建 PR
  • 在 PR 评论里修复问题
  • 自动审查每个 PR
  • 运行自定义 prompt 或 Skill
  • 在 CI 中输出结构化结果

快速安装:/install-github-app

最简单方式是在 Claude Code 终端里运行:

text
/install-github-app

它会引导你:

  1. 安装 Claude GitHub App
  2. 配置仓库权限
  3. 添加所需 Secrets
  4. 创建 workflow 文件

要求:

  • 你必须是 repository admin
  • GitHub App 需要 Contents、Issues、Pull requests 的读写权限
  • 直接 Claude API 用户可走快速安装;Bedrock/Vertex AI 用户需要手动配置

手动安装步骤

  1. 安装 GitHub App:https://github.com/apps/claude
  2. 在仓库 Secrets 中添加 ANTHROPIC_API_KEY
  3. 从官方 examples 复制 workflow 到 .github/workflows/claude.yml
  4. 在 issue 或 PR 评论里测试 @claude

最小 workflow:

yaml
name: Claude Code
on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]

jobs:
  claude:
    runs-on: ubuntu-latest
    steps:
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: '<ANTHROPIC_API_KEY secret>'

v1.0 从 Beta 迁移:破坏性变更

v1.0 简化了配置,但 beta 用户需要修改 workflow。

Beta 输入v1.0 输入
mode删除,自动检测
direct_promptprompt
override_promptprompt + GitHub variables
custom_instructionsclaude_args: --append-system-prompt
max_turnsclaude_args: --max-turns
modelclaude_args: --model
allowed_toolsclaude_args: --allowedTools
disallowed_toolsclaude_args: --disallowedTools
claude_envsettings JSON

Beta 示例:

yaml
- uses: anthropics/claude-code-action@beta
  with:
    mode: "tag"
    direct_prompt: "Review this PR for security issues"
    anthropic_api_key: '<ANTHROPIC_API_KEY secret>'
    custom_instructions: "Follow our coding standards"
    max_turns: "10"
    model: "claude-sonnet-4-6"

v1 示例:

yaml
- uses: anthropics/claude-code-action@v1
  with:
    prompt: "Review this PR for security issues"
    anthropic_api_key: '<ANTHROPIC_API_KEY secret>'
    claude_args: |
      --append-system-prompt "Follow our coding standards"
      --max-turns 10
      --model claude-sonnet-4-6

自动 PR 审查:结合 Skill

prompt 可以是普通文本,也可以调用 Skill。

yaml
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: '<ANTHROPIC_API_KEY secret>'
          plugin_marketplaces: "https://github.com/anthropics/claude-code.git"
          plugins: "code-review@claude-code-plugins"
          prompt: "/code-review:code-review <github.repository>/pull/<pull_request.number>"

如果 Skill 在仓库 .claude/skills/ 目录中,需要先 checkout:

yaml
steps:
  - uses: actions/checkout@v4
  - uses: anthropics/claude-code-action@v1
    with:
      anthropic_api_key: '<ANTHROPIC_API_KEY secret>'
      prompt: "/security-review"

定时自动化:每日摘要

yaml
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: '<ANTHROPIC_API_KEY secret>'
          prompt: "Generate a summary of yesterday's commits and open issues"
          claude_args: "--model opus"

这类任务适合:

  • 每日 commit 摘要
  • 每周 issue backlog 整理
  • 自动生成 changelog 草稿
  • 定时检查依赖升级

CLAUDE.md 的作用

GitHub Actions 中的 Claude 会读取仓库里的 CLAUDE.md,因此建议写清楚:

  • 代码风格
  • 测试命令
  • PR 审查标准
  • 安全要求
  • 哪些目录不能修改
  • 提交信息格式

示例:

markdown
# CLAUDE.md

## CI 要求
- 修改 TypeScript 后运行 `npm run typecheck`
- 修改 API 后运行 `npm test -- api`
- PR 审查必须关注认证、授权、SQL 注入和日志泄露

## 禁止
- 不要修改 `src/legacy/`,该目录由外部团队维护
- 不要提交 `.env` 或任何密钥

安全最佳实践

  1. 永远用 GitHub Secrets,不要把 API Key 写进 workflow
  2. 最小权限:只给 Contents/Issues/Pull requests 必要权限
  3. Review 后再合并:Claude 生成的 PR 仍需人类审查
  4. 限制工具:用 allowed_tools / disallowed_tools 控制能力
  5. 设置超时:避免 runaway job
  6. 保护主分支:Claude 不应绕过 branch protection

成本控制

Claude Code GitHub Actions 有两类成本:

  • GitHub Actions minutes
  • Claude API token 成本

优化建议:

  • Prompt 尽量具体,减少无效探索
  • 配置 --max-turns
  • 对自动 PR 审查使用 concurrency,避免同一 PR 多次并发审查
  • 大仓库中用路径过滤,只在相关目录变化时触发
yaml
concurrency:
  group: claude-review-<pull_request.number>
  cancel-in-progress: true

来源:Claude Code 官方文档 - GitHub Actions | 整理:ClaudeEagle

相关文章推荐

实战Claude Code CI/CD 完全集成指南:GitHub Actions 自动化代码审查与测试Claude Code 与 CI/CD 流水线完整集成教程:GitHub Actions 中非交互模式(claude -p)调用、PR 自动代码审查 Workflow、自动测试生成、构建失败时的 AI 诊断、安全扫描集成、Claude API Key 的 Secrets 管理、控制成本的模型选择策略(PR 审查用 Sonnet/失败诊断用 Haiku),以及 GitLab CI 和 Jenkins 的适配方案。2026/3/20实战Claude Code 非交互模式(Headless):脚本自动化与 CI/CD 集成实战Claude Code Headless 非交互模式完整实战:基础 -p 参数用法、三大自动化脚本(PR 摘要/安全扫描/Changelog 生成)、GitHub Actions 集成(每 PR 自动审查/每周技术债务扫描)、输出格式控制与批量任务成本优化。2026/3/14实战Claude Code 并行 Worktree 实战:同时跑 4 个 AI 任务的工作流设计Claude Code Worktree 4 种并行工作流模式:功能开发+Bug修复同时进行(3个终端并行、时间节省分析);并行代码审查(PR Worktree审查+继续开发);大规模重构+主线开发(破坏性变更的隔离策略);Subagent 隔离自动并行(4个维度同时分析节省 75% 时间)。不应该并行的场景(有依赖/需共享上下文/单一简单任务)。5 个实用技巧(命名规范、.worktreeinclude、状态监控脚本、PR Worktree 直接推送)。2026/5/13实战Claude Code Routines 实战:6 个可直接使用的 Routine 配置模板6 个开箱即用的 Claude Code Routines 模板:PR 代码审查(GitHub 触发,含 OWASP 安全清单和内联评论格式);依赖安全扫描(每日 Schedule,自动修复低风险漏洞并创建 PR);文档漂移检测(每周 Schedule,比对代码变更与文档的一致性);生产告警响应(API 触发,含 curl 请求示例和 Slack 通知格式);每日 PR 摘要(含超时 PR 的 @mention 提醒);发布后烟雾测试(CD 流水线调用,健康检查 + 错误率验证)。含写好 Routine Prompt 的 5 个核心原则。2026/5/12实战Claude Code Skills 实战:15 个可直接使用的 SKILL.md 模板(Git/审查/测试/文档/部署/调试)15 个精心设计的开箱即用 SKILL.md 模板:Git 工作流类(Smart Commit/PR Creator/Branch Cleanup);代码审查类(Security Review 含 OWASP 清单/Performance Review N+1 检测);测试类(Test Generator/Coverage Check);文档类(API Doc Generator OpenAPI 格式/Changelog Generator);部署运维类(Pre-deploy Checklist);调试类(Error Analyzer);效率工具类(Code Explainer/Refactor Advisor/Dependency Auditor/Daily Standup Helper)。2026/5/10实战Claude Code 成本优化完整指南:Token 节省策略、模型选择和 Prompt Cache 配置Claude Code 成本优化完整指南:Token 消耗来源分析(对话历史/大文件读取/工具输出/MCP 服务器/长 CLAUDE.md);8 个优化策略(/compact 主动压缩/精确 @ 引用/控制 MCP 数量/模型选择 Haiku vs Sonnet vs Opus 价格对比/努力等级按需调整/Prompt Cache 1 小时 TTL/CLAUDE.md 精简/usage 监控);不同场景的成本估算(个人/小团队/企业);以及订阅 vs API 的临界点分析。2026/5/8