教程

Claude Code GitHub Actions 实战:用 @claude 让 AI 自动修 bug、实现功能、生成 PR

Claude Code GitHub Actions 实战教程:配置 @claude 命令触发 AI 自动修 bug、实现功能、生成 PR。含 2 步快速配置、3 个工作流示例、安全设置和常见问题排查。

2026/4/93分钟 阅读ClaudeEagle

在 GitHub issue 里描述 bug,评论 @claude fix this,几分钟后 PR 出来了——这是 Claude Code GitHub Actions 的日常场景。


能做什么?

在任何 PR 或 issue 里 @claude 一下:

  • @claude fix the null pointer exception in auth.ts
  • @claude implement the feature described in this issue
  • @claude add unit tests for the new endpoint
  • @claude refactor this to use async/await
  • @claude write a PR description for these changes

Claude 读取代码库上下文、遵循 CLAUDE.md 规范,创建 PR 或直接提交修改。


快速配置(2 步)

第一步:添加 API Key

GitHub 仓库 Settings → Secrets and variables → Actions,添加:

ANTHROPIC_API_KEY = your_api_key

第二步:创建 Workflow 文件

.github/workflows/claude.yml

yaml
name: Claude Code

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

jobs:
  claude:
    if: contains(github.event.comment.body, '@claude')
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      issues: write
    steps:
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

完成。现在在任何 issue/PR 评论里 @claude 即可触发。


3 个实用工作流

1. 自动实现 issue 功能

yaml
jobs:
  claude:
    if: |
      github.event.comment.body == '@claude implement' &&
      github.event.comment.author_association == 'OWNER'
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Read this issue and implement the feature.
            Follow CLAUDE.md coding standards.
            Create a PR with your changes.

Issue 里评论 @claude implement,AI 自动实现 + 创建 PR。

2. 自动生成 PR 描述

yaml
on:
  pull_request:
    types: [opened]

jobs:
  claude:
    runs-on: ubuntu-latest
    steps:
      - uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          prompt: |
            Analyze the PR changes and write a clear description:
            - Summary of changes
            - Why these changes
            - Testing notes
            Update the PR description.

3. 使用 Opus 做复杂任务

yaml
- uses: anthropics/claude-code-action@v1
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    model: claude-opus-4-6  # 默认 Sonnet,复杂任务换 Opus

安全设置

只允许仓库成员触发(防外部滥用):

yaml
if: |
  contains(github.event.comment.body, '@claude') &&
  (github.event.comment.author_association == 'OWNER' ||
   github.event.comment.author_association == 'COLLABORATOR')

常见问题

@claude 没有响应

  1. 确认 workflow 文件已提交到默认分支
  2. 检查 ANTHROPIC_API_KEY 是否正确
  3. 查看 GitHub Actions 日志找报错

Claude 的 commit 没有触发 CI

GitHub 限制防止循环触发。解决:

yaml
- uses: anthropics/claude-code-action@v1
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    use_github_app_token: true

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

相关文章推荐

教程Claude Code + GitHub Actions:自动化代码审查与 CI/CD 集成完全指南Claude Code GitHub Actions 完整配置指南:5 分钟快速安装(GitHub App + API Key Secret + Workflow 文件)、四大使用场景(按需 PR 审查/Issue 自动实现/快速修复/自动 Changelog)、每 PR 自动触发审查配置、高级参数(模型/轮数/工具限制/AWS Bedrock)与安全最佳实践。2026/3/13教程Claude Code GitHub Actions 集成指南:让 AI 自动化你的 CI/CD 流水线Claude Code GitHub Actions 让 AI 融入 GitHub 工作流,支持通过 @claude 提及触发代码审查、自动创建 PR 和修复 Bug。本文涵盖快速安装、手动配置、Beta 到 v1.0 升级指南、实用工作流示例(PR 审查、Issue 自动修复)以及 AWS Bedrock/Google Vertex AI 集成方案。2026/2/27教程Claude Code 程序化调用完全指南:-p 标志、结构化输出、流式响应与会话续接Claude Code 程序化调用完全指南:-p/--print 非交互模式基础用法、三种输出格式(text/json/stream-json)、按 JSON Schema 提取结构化数据(structured_output 字段)、jq 解析响应、流式响应(stream-json + --verbose + --include-partial-messages + jq -rj 过滤 text_delta)、--allowedTools 自动批准工具(权限规则语法/末尾空格注意事项)、自动创建 Commit 示例、--append-system-prompt/--system-prompt 系统提示词、--continue/--resume 会话续接(Session ID 捕获)、GitHub Actions CI/CD 集成,以及 Python/TypeScript Agent SDK 高级用法入口。2026/3/8教程Claude Code 无头模式与 Agent SDK:非交互式脚本、CI/CD 集成完全指南Claude Code 无头模式与 Agent SDK 完全指南:-p 参数基础用法、三种输出格式(text/json/stream-json)、JSON Schema 结构化输出、精细工具权限控制、多轮对话 Session 管理,以及 GitHub Actions PR 安全审查和批量处理的 CI/CD 实战场景。2026/3/2教程Claude Code GitHub Actions 集成:自动化 CI/CD 工作流与 PR 代码审查Claude Code GitHub Actions 集成完整指南:PR 自动代码审查工作流、自动生成单元测试、--print 非交互模式配置、GitHub Actions 权限配置、成本控制策略,以及官方 Claude Code Action 使用方法。2026/3/1教程Claude Code Plan Mode 深度解析:让 AI 先想清楚再动手,避免改代码越改越乱Claude Code Plan Mode 深度解析:进入只读分析模式,让 AI 先制定完整方案再动手,彻底解决「越改越乱」。含典型工作流、4 大使用价值和场景判断。2026/4/9