推理提供商文档

使用 GitHub Actions 自动化代码审查

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

使用 GitHub Actions 自动化代码审查

OpenCode 是一款运行在终端中的 AI 编程智能体,可以通过 Hugging Face 推理提供商(Inference Providers)使用开源模型。

您还可以将 OpenCode 安装为 GitHub App,以帮助自动化 GitHub 工作流!

在不到 5 分钟的时间内,您就可以将其配置为利用强大的开源语言模型来响应 Issue 和 Pull Request (PR)。

本指南将向您展示如何将 Hugging Face 推理提供商与 OpenCode 结合使用以驱动 GitHub 自动化。您将能够使用 DeepSeek、GLM-4.5 和 Kimi K2 等模型来分拣 Issue、实现功能以及审查代码。

本指南假设您已拥有 Hugging Face 账号和 GitHub 仓库。您可以在 huggingface.co 免费创建 Hugging Face 账号。

OpenCode GitHub App 的功能

设置完成后,OpenCode 将响应 GitHub Issue 和 Pull Request 中的 /oc/opencode 命令

  • 分拣并解释 Issue - 要求 OpenCode 分析并解释某个 Issue
  • 实现功能 - 请求修复 Bug 或开发新功能,OpenCode 会创建包含更改内容的 PR
  • 审查并修改 PR - 对 Pull Request 请求进行更改,OpenCode 将提交这些更改

整个工作流在您的 GitHub Actions 运行器中安全运行,您可以完全控制在哪些仓库中安装 OpenCode GitHub App。

步骤 1:安装 OpenCode

首先,请按照安装指南在您的本地机器上安装 OpenCode。

步骤 2:为 GitHub 配置 OpenCode

进入您本地克隆的 GitHub 仓库,然后运行 setup 命令

cd your-repository
opencode github install

这将触发一个交互式设置流程,引导您完成以下步骤:

  1. 安装 GitHub App - 您的浏览器将打开以授权 OpenCode 访问您的仓库。您可以选择特定的仓库或所有仓库。
  2. 选择提供商 - 从列表中选择 Hugging Face
  3. 选择模型 - 选择一个模型,例如 GLM-4.5-AirKimi-K2-Instruct
  4. 创建工作流 - 自动生成 .github/workflows/opencode.yml 文件

.github/workflows/opencode.yml 文件创建后,您需要将其提交(commit)并推送(push)到您的仓库

git add .github/workflows/opencode.yml
git commit -m "Add OpenCode workflow"
git push

设置完成后,您需要将 Hugging Face token 添加为仓库密钥(Repository Secret)

  1. 从您的 Hugging Face 设置中获取 token。该 token 需要具有 Make calls to Inference Providers 权限。
  2. 在您的 GitHub 仓库中,前往 Settings → Secrets and variables → Actions
  3. 点击 New repository secret
  4. 将其命名为 HF_TOKEN 并粘贴您的 token

请确保您的 Hugging Face token 已启用 Inference Providers 权限。我们建议专门为 OpenCode 使用创建一个独立的 token。

步骤 3:试用

工作流设置好且 token 配置完成后,可以通过在任何 Issue 中发表评论来测试它

/oc summarize

OpenCode 将分析该 Issue 并提供摘要。以下是您可以尝试的其他命令:

解释一个 Issue

/opencode explain this issue

实现修复

OpenCode 将创建新分支,实施更改并开启一个 Pull Request。

在 PR 上请求更改

/oc please add error handling

使用 /oc(简写)或 /opencode(全称)来触发命令。OpenCode 能够理解自然语言,因此请随意详细描述您的需求。

安全注意事项

在公开仓库中,任何人都可以通过评论 /oc/opencode 来触发机器人。这可能会导致意外的推理成本或不需要的 PR。建议:

  • 使用独立的 Hugging Face token 专门给 OpenCode 使用(不要使用主 token),这样在需要时可以撤销而不会影响其他服务
  • 监控您的 Hugging Face 使用情况以跟踪成本
  • 添加工作流条件来限制谁可以触发机器人(例如,仅限仓库协作人员)
  • 从私有仓库或测试仓库开始,以控制访问权限

工作流实际运行示例

这里有一个来自 Hugging Face datasets 仓库 fork 版 的真实案例。该 Issue 请求添加对 uv 安装的支持

当有人评论 /oc fix this 时,OpenCode 会分析该 Issue,创建新分支,实施更改并开启一个 Pull Request

该 PR 包含了所有必要的更改

切换模型

不同的模型在速度、成本和能力之间有不同的权衡。您可以通过编辑工作流文件来尝试不同的模型。

编辑 .github/workflows/opencode.yml 并更新 model 参数

with:
  model: huggingface/deepseek-ai/DeepSeek-V3         # Powerful reasoning
  # or
  model: huggingface/zai-org/GLM-4.5-Air             # Balanced performance

提交并推送更改

git add .github/workflows/opencode.yml
git commit -m "Switch to the DeepSeek model"
git push

理解工作流

安装向导会生成 .github/workflows/opencode.yml,该文件定义了 OpenCode 的运行时间与运行方式

name: opencode

on:
  issue_comment:
    types: [created] # Trigger on new comments

jobs:
  opencode:
    # Only run if comment contains /oc or /opencode
    if: |
      contains(github.event.comment.body, ' /oc') ||
      startsWith(github.event.comment.body, '/oc') ||
      contains(github.event.comment.body, ' /opencode') ||
      startsWith(github.event.comment.body, '/opencode')

    runs-on: ubuntu-latest

    permissions:
      id-token: write # Required for OpenCode authentication
      contents: read # Read repository contents
      pull-requests: read # Access PR information
      issues: read # Access issue information

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Run opencode
        uses: sst/opencode/github@latest
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }} # Your Hugging Face token
        with:
          model: huggingface/zai-org/GLM-4.5-Air # The model to use

每当有人在 Issue 或 PR 中通过 /oc/opencode 发表评论时,工作流就会被触发,随后它会检出您的仓库代码,并使用您从 Hugging Face 推理提供商(Inference Providers)中选择的模型运行 OpenCode。

后续步骤

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.