Claude Code 实战-2.8 Github集成

2.8 Github集成

Claude Code 提供官方 GitHub 集成,可让 Claude 在 GitHub Actions 中运行。该集成包含两类主要工作流:在问题与拉取请求中通过提及触发支持,以及自动拉取请求审查。

一、集成设置

在 Claude 中运行 /install-github-app 开始设置。该命令会引导你完成:

1.在 GitHub 安装 Claude Code 应用。

2.添加你的 API 密钥。

3.自动生成包含工作流文件的拉取请求。

合并该拉取请求后,你的仓库会新增两项 GitHub Actions,工作流文件位于 .github/workflows 目录。

二、默认 GitHub Actions

2.1 提及工作流(Mention Action)

在任意 Issue 或 PR 中使用 @claude 提及 Claude。被提及后,Claude 会:

1.分析请求并创建任务计划。

2.在拥有代码库完整访问权限下执行任务。

3.直接在 Issue 或 PR 中回复结果。

2.2 拉取请求工作流(Pull Request Action)

1.每当你创建 PR,Claude 会自动:

2.审查拟议的更改。

3.分析修改的影响。

4.在 PR 中发布详细报告。

三、自定义工作流

合并初始 PR 后,你可按项目需求自定义工作流文件。如下是增强“提及”工作流的方式:

3.1 添加项目初始化步骤

在 Claude 运行前,添加环境准备步骤:

– name: Project Setup
run: |
npm run setup
npm run dev:daemon

3.2 自定义指令

为 Claude 提供项目上下文:

custom_instructions: |
The project is already set up with all dependencies installed.
The server is already running at localhost:3000. Logs from it
are being written to logs.txt. If needed, you can query the
db with the ‘sqlite3’ cli. If needed, use the mcp__playwright
set of tools to launch a browser and interact with the app.

3.3 MCP 服务器配置

配置 MCP 服务器以扩展 Claude 能力:

mcp_config: |
{
“mcpServers”: {
“playwright”: {
“command”: “npx”,
“args”: [
“@playwright/mcp@latest”,
“–allowed-origins”,
“localhost:3000;cdn.tailwindcss.com;esm.sh”
]
}
}
}

四、工具权限

在 GitHub Actions 中运行 Claude 时,必须显式列出所有允许的工具,使用 MCP 服务器时尤为重要。

与本地开发不同,GitHub Actions 中没有权限“快捷方式”;每个 MCP 服务器的每个工具都要单独列出。

五、最佳实践

从默认工作流开始,逐步定制。

使用自定义指令提供项目特定上下文。

使用 MCP 服务器时明确列出工具权限。

先用简单任务验证工作流,再处理复杂任务。

根据项目实际需求配置额外步骤。

该 GitHub 集成可将 Claude 从开发助手升级为自动化的团队成员,在你的 GitHub 工作流中处理任务、审查代码并提供洞见。

发表回复