Claude Code接入Github

目前AI编程工具可以分为3种类别:

  1. 1.本地IDE,代表产品有cursor、windsurf
  2. 2.在线网站,代表产品有lovable、bolt.new
  3. 3.命令行,代表产品有claude code、codex

claude code这种命令行工具可以很方便地集成到各种平台中,本篇文章就是介绍如何把claude code快速接入到github中,在开发流程中嵌入AI能力。

claude code 接入github,我们不需要重复造轮子,anthropic官方提供并开源了名为claude code action(https://github.com/anthropics/claude-code-action)的工具,claude code action更新很频繁,前段时间刚发布了正式版本v1,借助它可以快速把claude code集成到github中。

接下来按照配置api key、在github安装claude、让claude code参与开发3个步骤,分享一下如何把claude code集成到github。

配置api key

claude对国内账号封控很严重,我们很难用到claude官方的api,好在国产模型进步很快,并且都原生提供anthropic接口格式了,可以作为sonnet的平替接入claude code。我测试glm-4.6和kimi-k2都能够驱动claude code正常运行。可以在 https://bigmodel.cn/usercenter/proj-mgmt/apikeys 创建glm的api key,在 https://platform.moonshot.cn/console/api-keys 创建kimi的api key,然后在你期望接入claude code的github仓库中配置api key。

进入github仓库中,点击settings,然后选择secrets and variables中的actions,点击new repository secret按钮。

在新打开的页面中填写Name:ANTHROPIC_API_KEY,Secret:你刚刚创建的glm 或 kimi api key,最后点击add secret配置成功。

在github安装claude

接下来,需要在GitHub中安装claude。在浏览器打开 https://github.com/apps/claude,进入claude github app页面,点击install按钮安装claude应用,如果按钮位置显示的不是install而是configure,表示已经安装过该应用。

然后在下面的安装确认页面中,可以配置安装到所有仓库或者指定仓库中,点击install & authorize按钮确认安装。

在安装成功后,如果你的github仓库还没有.github/workflows目录,你需要先创建该目录,然后在该目录中添加claude.yml和claude-review.yml文件。

claude.yml文件内容如下

复制代码
name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      issues: write
      id-token: write
      actions: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Run Claude Code
        id: claude
        uses: anthropics/claude-code-action@v1
        env:
          # ANTHROPIC_BASE_URL: https://api.moonshot.cn/anthropic
          ANTHROPIC_BASE_URL: https://open.bigmodel.cn/api/anthropic
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          claude_args: |
            --allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh issue:*),Bash(gh search:*),Bash(gh label:*)"

claude-review.yml文件内容如下

复制代码
name: Claude Review

on:
  pull_request:
    types: [opened, synchronize]
    # Optional: Only run on specific file changes
    # paths:
    #   - "src/**/*.ts"
    #   - "src/**/*.tsx"
    #   - "src/**/*.js"
    #   - "src/**/*.jsx"

jobs:
  claude-review:
    # Optional: Filter by PR author
    # if: |
    #   github.event.pull_request.user.login == 'external-contributor' ||
    #   github.event.pull_request.user.login == 'new-developer' ||
    #   github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR'

    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      id-token: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Run Claude Review
        id: claude-review
        uses: anthropics/claude-code-action@v1
        env:
          # ANTHROPIC_BASE_URL: https://api.moonshot.cn/anthropic
          ANTHROPIC_BASE_URL: https://open.bigmodel.cn/api/anthropic
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          track_progress: true
          prompt: |
            REPO: ${{ github.repository }}
            PR NUMBER: ${{ github.event.pull_request.number }}

            Please review this pull request with a focus on:
            - Code quality and best practices
            - Potential bugs or issues
            - Security implications
            - Performance considerations

            Provide detailed feedback using inline comments for specific issues.
            Please respond in Simplified Chinese.

          claude_args: |
            --allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*)"

注意在上面两个文件中有 ANTHROPIC_BASE_URL 配置,默认配置的是glm网址,如果使用的kimi模型,需要更改为 https://api.moonshot.cn/anthropic。另外在配置文件最后有--allowedTools参数,claude code使用的是工具白名单,如果需要某个特定的工具,需要配置到该参数中。

可能有人不熟悉.github/workflows目录,它是github中的保留目录,用于保存自动化工作流程的配置文件,可以配置当触发某些事件时执行哪些操作。比如可以在此目录中配置实现当提交新的代码时自动运行打包镜像、测试代码等功能。claude code action也是通过此机制实现的让claude回答用户提问、自动审阅代码等功能。

让claude code参与开发

执行完上述步骤,claude code就已经接入到你的github仓库中了。你可以在github中issue中评论@claude,让ai回答你的问题甚至可以让ai直接提交代码完成你的需求。下面是一些例子

我创建了python包管理方案讨论的issue,在评论中询问claude,让claude推荐一个方案。

在一个后端接口需求中直接让claude实现该需求,claude code能够提交代码到某个分支,点击Create PR链接可以手动创建代码合并。

除了可以在issue/pr中@claude以外,claude-review.yml工作流还支持自动审阅代码。提交pr后,claude可以自动审阅提交的代码,查看代码中的问题,并给出改进建议。

 

相关推荐
TunerT_TQ13 小时前
L3 静态源码取证|工程评测】Serein :一个 Rust 原生 Discord 客户端的“扩展能力”与“平台红线”之间
github
智购科技智能售货柜13 小时前
自动售货机盐雾腐蚀与三防设计实战:从锈蚀到防护~YH
git·科技·单片机·嵌入式硬件·github
怕浪猫16 小时前
机器人是当下薪酬最高却几乎没人学的技能:一份 6 个月从 0 到上手的学习路线
算法·架构·github
qetfw17 小时前
EVE-NG 导入 Debian 虚拟机:VMDK 转 QCOW2 与启动验证
linux·前端·debian·github
Jay-r19 小时前
开源 zh-doc-quality-gate:检查 Markdown / Word 的五类文档质量问题
人工智能·学习·开源·github·skill
艺杯羹19 小时前
零基础看懂 GitHub(Gitee 与 GitCode 通用):找资源、认界面、4 种获取方式与开源协议
git·gitee·开源·github·版本控制
阿策小和尚20 小时前
干掉 better-sqlite3,把 5 万星的 Ghost 6.81 压成 67MB 单包跑进 Dopame OS
人工智能·设计模式·github
m4Rk_2 天前
【论文阅读】Agent 记忆机制(151):DUNE——让源事实更新沿依赖图传播到派生记忆
论文阅读·人工智能·学习·开源·github
这里是北京2 天前
GitHub仓库推荐:Obsidian Skills,让AI把项目笔记组织成可追溯的知识图
笔记·github