GitHub Actions
GitHub Actions
Section titled “GitHub Actions”GitHub 是开发者的「办公室」。PR 评论、issue 描述、CI 流水线都在这里发生。如果 Claude 能直接参与这个工作流——你只要在 PR 里 @claude 一下,它就动手——那 AI 协作就从「拷贝粘贴到聊天框」升级到了「原地干活」。
Claude Code GitHub Actions 做的就是这件事。它把 Claude Code 接进 GitHub workflow,让你用一个 @claude mention 调动整个 agent 去分析代码、创建 PR、实现功能、修 Bug,全部遵循你 CLAUDE.md 里定的项目规范。
| 优点 | 说明 |
|---|---|
| Instant PR creation | 你描述需求,Claude 直接生成完整 PR(带改动) |
| Automated code implementation | issue 一句话变工作代码 |
| Follows CLAUDE.md | 遵守你项目的编码规范和已有模式 |
| Simple setup | 几分钟装好,开箱即用 |
| Secure by default | 代码跑在 GitHub 自己的 runner 上,不外泄 |
它构建在 Claude Agent SDK 之上——后者把 Claude Code 编程化暴露出来,GitHub Actions 只是它的一种用法。需要更自由的自动化时,可以直接用 SDK 写自己的工作流。
模型方面:默认 Sonnet,需要 Opus 4.7 时把 model 参数配成 claude-opus-4-7。
@claude mention 触发
Section titled “@claude mention 触发”最常见的用法是在 PR 或 issue 评论里 @claude:
@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 componentClaude 自动分析上下文(PR diff、issue 描述、相关代码),给出方案或直接开干。Action 根据配置自动判断是「交互模式」(响应 @claude 提及)还是「自动化模式」(直接跑 prompt)。
Quick setup
Section titled “Quick setup”最快的方式是用 Claude Code 自己装:
# 在终端跑 claude,然后执行/install-github-app它会引导你装 GitHub app、配 secret。前提:你必须是仓库 admin,能装 app 和加 secret;app 会请求 Contents / Issues / Pull requests 的读写权限。Quick setup 只支持直接用 Claude API 的用户,用 Bedrock 或 Vertex 走 Manual setup。
Manual setup
Section titled “Manual setup”如果 /install-github-app 失败或者你偏好手动:
- 装 Claude GitHub app 到你的仓库:https://github.com/apps/claude
- 把
ANTHROPIC_API_KEY加进仓库 secrets - 把
examples/claude.yml复制到你仓库的.github/workflows/
装完后在 issue 或 PR 评论里 @claude 测试一下。
从 Beta 升级
Section titled “从 Beta 升级”v1.0 引入了 breaking changes,beta 用户必须更新 workflow 文件:
| 旧(Beta) | 新(v1.0) |
|---|---|
uses: anthropics/claude-code-action@beta |
uses: anthropics/claude-code-action@v1 |
mode: "tag" / mode: "agent" |
删除,现在自动检测 |
direct_prompt |
prompt |
override_prompt |
prompt + GitHub 变量 |
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 版- uses: anthropics/claude-code-action@beta with: mode: "tag" direct_prompt: "Review this PR for security issues" anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} custom_instructions: "Follow our coding standards" max_turns: "10" model: "claude-sonnet-4-6"
# v1.0 GA 版- uses: anthropics/claude-code-action@v1 with: prompt: "Review this PR for security issues" anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} claude_args: | --append-system-prompt "Follow our coding standards" --max-turns 10 --model claude-sonnet-4-6v1 的核心特性:统一 prompt 接口(所有指令都走 prompt)、Skills 直接调用、CLI passthrough(任何 Claude Code CLI 参数都能通过 claude_args 传)、灵活触发(任意 GitHub 事件)。
Basic workflow
Section titled “Basic workflow”最基础的:监听评论里的 @claude mention 并响应。
name: Claude Codeon: 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: ${{ secrets.ANTHROPIC_API_KEY }} # 自动响应 @claude mentionUsing skills
Section titled “Using skills”PR 一打开就让 Claude 跑一遍 review skill:
name: Code Reviewon: 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, correctness, and security. Analyze the diff, then post your findings as review comments." claude_args: "--max-turns 5"Custom automation with prompts
Section titled “Custom automation with prompts”任何 GitHub 事件都能触发——比如每天 9 点生成日报:
name: Daily Reporton: 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 在 issue/PR 评论里能干这些事:
- 代码审查:「review this PR for security issues」
- 修 Bug:「fix the TypeError in user dashboard」
- 实现功能:「implement this feature based on the issue description」
- 回答问题:「how should I implement authentication for this endpoint?」
- 生成测试:「add tests for the auth module」
Claude 会自动分析上下文并选择合适的方式回应。
CLAUDE.md 配置
Section titled “CLAUDE.md 配置”仓库根目录放一个 CLAUDE.md,定义编码风格、审查标准、项目特定规则和首选模式。Claude 在跑任务时会读这份文件,遵循你的规范。
- 永不把 API key 直接 commit 到仓库
- 用 GitHub Secrets:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} - 权限只给 Action 真正需要的
- Claude 的建议合并前还是过一眼
- 用 issue 模板给 Claude 更好的上下文
CLAUDE.md保持精炼聚焦- 配置合适的工作流超时
| 成本类型 | 说明 |
|---|---|
| GitHub Actions minutes | Claude 跑在 GitHub-hosted runner 上,消耗 Actions 配额 |
| API tokens | 每次交互消耗 token,量取决于 prompt/response 长度、任务复杂度、代码库大小 |
省钱技巧:用具体的 @claude 命令避免不必要的调用;用 --max-turns 限轮数;设工作流超时;用 GitHub concurrency 控制并行。
Bedrock / Vertex 支持
Section titled “Bedrock / Vertex 支持”企业环境可以跑在自己的云上,掌控数据驻留和计费:
- AWS Bedrock:配 GitHub OIDC + IAM role,免静态 key
- Google Vertex AI:用 Workload Identity Federation,免下载 service account key
需要加的 secret 各不相同——Bedrock 要 AWS_ROLE_TO_ASSUME,Vertex 要 GCP_WORKLOAD_IDENTITY_PROVIDER 和 GCP_SERVICE_ACCOUNT。详细配置见官方文档。
Troubleshooting
Section titled “Troubleshooting”| 症状 | 排查方向 |
|---|---|
@claude 没反应 |
workflow 触发条件是否对、ANTHROPIC_API_KEY 是否在 secrets 里且未过期、评论里写的是 @claude(不是 /claude) |
| Claude 的 commit 没触发 CI | 检查 GitHub Actions 的 push 事件过滤是否排除了 bot/GITHUB_TOKEN 触发的提交 |
| 认证错误 | Claude API:检查 ANTHROPIC_API_KEY;Bedrock/Vertex:验证 OIDC/WIF 配置、角色名、secret 名 |
Action 参数
Section titled “Action 参数”v1 的统一接口:
- uses: anthropics/claude-code-action@v1 with: anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} prompt: "Your instructions here" # 可选 claude_args: "--max-turns 5" # 可选 CLI 参数prompt 是统一入口;claude_args 把任意 Claude Code CLI 参数透传进去——包括 --allowedTools、--disallowedTools、--append-system-prompt、--model、--max-turns 等。
GitHub Actions 把 Claude Code 从「另一个网页聊天框」变成「办公室同事」——你
@claude,它就动手。CLAUDE.md是它的工作守则,claude_args是它的工具箱,GitHub Secrets 是它的工卡。
下一篇看 GitLab CI/CD,把同样的能力接进 GitLab 工作流。🔧