Claude Code GitHub Actions
使用 RunAPI secret、模型、permissions、触发事件和最小 workflow 配置 Claude Code GitHub Actions。
概览
官方 anthropics/claude-code-action 可在 GitHub Actions workflow 中运行 Claude Code。本指南将 RunAPI key 保存为 GitHub Secret,在 Claude Code settings 中设置非敏感的 Base URL,选择模型,并响应经过授权的 @claude comment。
开始前准备
- 你需要 repository administrator 权限,以安装 Claude GitHub App、创建 Actions secrets 和添加 workflow。
- 在身份验证指南中创建标准 RunAPI API key。
- 从模型目录复制支持 Anthropic Messages 的 model identifier。
- 查看 Claude Code GitHub Actions 官方设置说明和组织的 workflow policy。
安装
在目标 repository 中打开 Claude Code,运行 /install-github-app 并按提示安装 GitHub App。当命令准备创建 Anthropic credential 时先停止保存:本指南改用 RunAPI repository 或 organization secret。
如需手动设置,请安装 Claude GitHub App,然后把下方 workflow 添加到 .github/workflows/claude.yml。
配置 RunAPI
创建名为 RUNAPI_API_KEY 的 repository 或 organization Actions secret。只通过 action 的 secret input 传入:
anthropic_api_key: ${{ secrets.RUNAPI_API_KEY }}
不要把 key 写入 workflow 文件或 action 的 settings JSON。action 会单独处理 settings;其中只应放非敏感的 Base URL。
以下最小交互式 workflow 监听新建的 issue comment 和 pull request review comment:
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
id-token: write
actions: read
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 1
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.RUNAPI_API_KEY }}
settings: |
{
"env": {
"ANTHROPIC_BASE_URL": "https://runapi.ai"
}
}
claude_args: "--model YOUR_RUNAPI_MODEL_ID"
Secrets
GitHub 不会向 fork pull request 或 Dependabot event 触发的 workflow 提供普通 Actions secrets。如果 run 中没有 credential,请先确认 event 来源,不要直接轮换 RunAPI key。Repository 和 organization scope 请查看 GitHub 的 Actions secrets 指南。
Permissions
contents: write允许 action 提交代码变更。pull-requests: write和issues: write允许读取并回复触发对话。id-token: write支持 action 默认的 GitHub App authentication。actions: read允许读取 workflow 结果。
请使用这些明确 permissions,不要使用 write-all。移除 workflow 不需要的权限。
触发事件
issue_comment 覆盖 issue 和 pull request conversation 中的 comment;pull_request_review_comment 覆盖 diff line comment。issue_comment 能触发前,workflow 文件必须已存在于 default branch。action 还会检查触发者身份;完整规则以官方安全行为为 canonical reference。
选择模型
将 claude_args 中的 YOUR_RUNAPI_MODEL_ID 替换为模型目录中的准确 identifier。将模型选择放在 settings.env 外,可让 workflow 选用的模型直接显示在 action 配置中。
验证
- 确认
RUNAPI_API_KEYsecret 存在,但不要打印其值。 - 将 workflow 合入 default branch,并确认 Actions 已启用。
- 使用有 repository write access 的账号,在 issue comment 中发送
@claude 用一句话描述这个仓库。 - 打开 workflow run,确认 action 成功,并检查 Claude 是否在同一 issue 中回复。
排查问题
- workflow 没有启动: 确认文件位于 default branch、comment event 匹配且 Actions 已启用。
- job 被跳过: 确认 comment 包含
@claude,且触发账号拥有所需 repository access。 - 身份验证失败: 确认 workflow 引用
secrets.RUNAPI_API_KEY;必要时通过身份验证指南轮换密钥。 - fork pull request 无法使用 key: GitHub 会有意阻止 fork-triggered run 读取普通 secrets。不要把 secret 改成 literal。
- 模型不可用: 从模型目录复制准确 identifier。
- Claude commit 没有启动另一个 workflow: 查看官方 action 的 GitHub token 排障说明。
下一步
协议行为请查看 LLM API 快速开始,请求字段请查看 Anthropic Messages API 参考。更多触发方式和 action inputs 请查看官方 examples和配置 reference。