claude-code-action

应用程序

anthropics/claude-code-action

GitHub Action 集成 Claude Code,实现代码评审与自动化修改。

项目简介

通用型 GitHub Action,将 Claude Code 接入 PR 与 issue 流程,可答疑、评审代码并直接实施修改。它根据工作流上下文自动选择执行模式,支持 Anthropic API、Amazon Bedrock、Google Vertex AI 与 Microsoft Foundry,并完全在自有 Runner 上运行,可输出结构化 JSON 结果。

README 预览

\n\n# Claude Code Action\n\nA general-purpose [Claude Code](https://claude.ai/code) action for GitHub PRs and issues that can answer questions and implement code changes. This action intelligently detects when to activate based on your workflow context—whether responding to @claude mentions, issue assignments, or executing automation tasks with explicit prompts. It supports multiple authentication methods including Anthropic direct API (API key or workload identity federation), Amazon Bedrock, Google Vertex AI, and Microsoft Foundry.\n\n## Features\n\n- 🎯 **Intelligent Mode Detection**: Automatically selects the appropriate execution mode based on your workflow context—no configuration needed\n- 🤖 **Interactive Code Assistant**: Claude can answer questions about code, architecture, and programming\n- 🔍 **Code Review**: Analyzes PR changes and suggests improvements\n- ✨ **Code Implementation**: Can implement simple fixes, refactoring, and even new features\n- 💬 **PR/Issue Integration**: Works seamlessly with GitHub comments and PR reviews\n- 🛠️ **Flexible Tool Access**: Access to GitHub APIs and file operations (additional tools can be enabled via configuration)\n- 📋 **Progress Tracking**: Visual progress indicators with checkboxes that dynamically update as Claude completes tasks\n- 📊 **Structured Outputs**: Get validated JSON results that automatically become GitHub Action outputs for complex automations\n- 🏃 **Runs on Your Infrastructure**: The action executes entirely on your own GitHub runner (Anthropic API calls go to your chosen provider)\n- ⚙️ **Simplified Configuration**: Unified `prompt` and `claude_args` inputs provide clean, powerful configuration aligned with Claude Code SDK\n\n## 📦 Upgrading from v0.x?\n\n**See our [Migration Guide](./docs/migration-guide.md)** for step-by-step instructions on updating your workflows to v1.0. The new version simplifies configuration while maintaining compatibility with most existing setups.\n\n## Quickstart\n\n

常见问题 (5)

故障排除
为什么 Claude Code Action 在运行审查之前会间歇性失败,并出现 "subtype: success" 和 "is_error: true"?

这通常意味着 Claude 进程在启动时失败,而不是代码审查完成后才失败。检查 claude-execution-output.json 并读取 result 字段;在所报告的案例中,该字段包含 Credit balance is too low。解决方法是检查 Anthropic API 账户的账单/信用额度余额,添加信用额度,然后重新运行工作流。启动失败通常表现为 is_error: true,且没有任何审查汇总或发现结果,例如 spawned: 0。这与报告了发现结果的已完成审查是不同的。

来源 Issue #1836
故障排除
如何修复 Claude Code Action 的 tag 模式下由 `files` 与 `paths` 参数不匹配导致的 `delete_files` schema 验证错误?

mcp__github_file_ops__delete_files 工具的 schema 接受 paths,但标签模式(tag-mode)的提示示例错误地使用了 files。这会导致在启用 use_commit_signing: true 时,Zod 验证拒绝第一次删除尝试。

修复 src/create-prompt/index.ts 中的提示示例(约第 827 行):

- mcp__github_file_ops__delete_files: {"files": ["path/to/old.js"], "message": "chore: remove deprecated file"}
+ mcp__github_file_ops__delete_files: {"paths": ["path/to/old.js"], "message": "chore: remove deprecated file"}

然后重新构建/发布该 action,或使用包含此修复的发布版本。在现有的运行中,使用 paths 重试该工具调用即可绕过失败的调用:

{"paths": ["path/to/old.js"], "message": "chore: remove deprecated file"}

请注意,commit_files 仍使用 files;只有 delete_files 使用 paths。回归测试应导入 MCP 工具的 schema,并验证提示示例中的参数名称与相应的 schema 是否一致。

来源 Issue #1665
故障排除
为什么在 Claude pull request 审查期间,通过 claude_args 传递的自定义 Bash 工具会被忽略?

对于 pull_request 事件,该 Action 会自动检测 tag 模式,此模式仅将以 mcp__github_ 开头的用户提供的 MCP 工具合并到默认允许列表中。诸如 Bash(npx markdownlint:*) 或 Bash(gh pr:*) 之类的自定义规则虽会被解析,但会被过滤掉。

要使用自定义 Bash 工具,请在 workflow 中显式设置 mode: agent:

- uses: anthropics/claude-code-action@v1
  with:
    mode: agent
    claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
    prompt: |
      Run `npx markdownlint *.md` to check markdown files.
    claude_args: |
      --allowedTools "Bash(npx markdownlint:*),Read,Glob"

请使用驼峰式命名的标志 --allowedTools;--allowed-tools 可能无法被识别。此行为是在 claude-code-action v1.0.22 中观察到的。

来源 Issue #724
故障排除
当分支名称包含 # 时,如何修复 GitHub 分支链接失效的问题?

在将分支名称的每个路径段插入 GitHub tree 或 compare URL 之前,先对其进行 URL 编码,同时保留 / 作为路径分隔符:

const encodedBranch = branchName
  .split("/")
  .map(encodeURIComponent)
  .join("/");

例如,claude/fix#123 会变成 claude/fix%23123,因此生成的链接为:

https://github.com/owner/repo/tree/claude/fix%23123

在所有创建分支 URL 的地方都应用此编码,包括 tracking-comment 和 branch-cleanup 代码。作为临时解决方案,请避免在生成的分支名称中使用 #,或手动将链接中的 # 替换为 %23。

来源 Issue #1712
故障排除
如何修复在 GitHub Actions 中 Claude Opus 5 或 Sonnet 5 被解析为 200k 上下文窗口而非 1M 的问题?

在工作流配置中,将 [1m] 上下文选择器附加到模型 ID 之后:

env:
  ANTHROPIC_MODEL: claude-opus-5[1m]
  ANTHROPIC_SMALL_FAST_MODEL: claude-sonnet-5[1m]

当 ANTHROPIC_BASE_URL 网关阻止 Claude Code 自动检测原生 1M 上下文支持时,必须进行此配置。该解决方案已在 action v1.0.185 与 Claude Code 2.1.222 上验证通过:claude-opus-5[1m] 会将 modelUsage.contextWindow 解析为 1000000,而 claude-opus-5 则解析为 200000,从而导致过早触发 auto-compaction。

要验证解析后的上限,请启用 show_full_output: true,并在任务日志中检查 modelUsage 对象:

"contextWindow": 1000000

注意:即使带有 [1m] 后缀,maxOutputTokens 仍可能报告为 64000;该值的解析独立于上下文窗口选择器。

来源 Issue #1600