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 issuesClaude 会读取 issue/PR 上下文、遵循仓库里的 CLAUDE.md,并在 GitHub runner 上执行代码修改。
核心能力:
- 从 issue 描述直接创建 PR
- 在 PR 评论里修复问题
- 自动审查每个 PR
- 运行自定义 prompt 或 Skill
- 在 CI 中输出结构化结果
快速安装:/install-github-app
最简单方式是在 Claude Code 终端里运行:
text
/install-github-app它会引导你:
- 安装 Claude GitHub App
- 配置仓库权限
- 添加所需 Secrets
- 创建 workflow 文件
要求:
- 你必须是 repository admin
- GitHub App 需要 Contents、Issues、Pull requests 的读写权限
- 直接 Claude API 用户可走快速安装;Bedrock/Vertex AI 用户需要手动配置
手动安装步骤
- 安装 GitHub App:
https://github.com/apps/claude - 在仓库 Secrets 中添加
ANTHROPIC_API_KEY - 从官方 examples 复制 workflow 到
.github/workflows/claude.yml - 在 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_prompt | prompt |
override_prompt | prompt + GitHub variables |
custom_instructions | claude_args: --append-system-prompt |
max_turns | claude_args: --max-turns |
model | claude_args: --model |
allowed_tools | claude_args: --allowedTools |
disallowed_tools | claude_args: --disallowedTools |
claude_env | settings 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` 或任何密钥安全最佳实践
- 永远用 GitHub Secrets,不要把 API Key 写进 workflow
- 最小权限:只给 Contents/Issues/Pull requests 必要权限
- Review 后再合并:Claude 生成的 PR 仍需人类审查
- 限制工具:用
allowed_tools/disallowed_tools控制能力 - 设置超时:避免 runaway job
- 保护主分支: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