Claude Code接入Github的实现步骤

作者:

文章目录
  • 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。在浏览器打开 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就已经接入到你的github仓库中了。你可以在github中issue中评论@claude,让ai回答你的问题甚至可以让ai直接提交代码完成你的需求。下面是一些例子 我创建了python包管理方案讨论的issue,在评论中询问claude,让claude推荐一个方案。 在一个后端接口需求中直接让claude实现该需求,claude code能够提交代码到某个分支,点击Create PR链接可以手动创建代码合并。 除了可以在issue/pr中@claude以外,claude-review.yml工作流还支持自动审阅代码。提交pr后,claude可以自动审阅提交的代码,查看代码中的问题,并给出改进建议。 到此这篇关于Claude Code接入Github的实现步骤的文章就介绍到这了,更多相关Claude Code接入Github内容请搜索风君子博客以前的文章或继续浏览下面的相关文章,希望大家以后多多支持风君子博客! 
  • 目录
    • 配置api key
    • 在github安装claude
    • 让claude code参与开发

    目前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。

    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。在浏览器打开 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就已经接入到你的github仓库中了。你可以在github中issue中评论@claude,让ai回答你的问题甚至可以让ai直接提交代码完成你的需求。下面是一些例子

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

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

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

    到此这篇关于Claude Code接入Github的实现步骤的文章就介绍到这了,更多相关Claude Code接入Github内容请搜索风君子博客以前的文章或继续浏览下面的相关文章,希望大家以后多多支持风君子博客! 

    站内搜索