~/content/github

GitHub

Use OpenCode in GitHub issues and pull-requests.

last_updated: "2026-01-20"

OpenCode 与你的 GitHub 工作流程集成。在你的评论中提及 /opencode/oc,OpenCode 将在你的 GitHub Actions runner 中执行任务。


##功能特性

  • 问题分流 (Triage issues): 让 OpenCode 调查一个 issue 并向你解释。
  • 修复和实现 (Fix and implement): 让 OpenCode 修复一个 issue 或实现一个功能。它将在一个新的分支中工作,并提交一个包含所有更改的 PR。
  • 安全 (Secure): OpenCode 在你的 GitHub runner 内部运行。

##安装

在 GitHub 仓库中的项目中运行以下命令:

bash
opencode github install

这将引导你完成安装 GitHub App、创建 workflow 和设置 secrets 的过程。


###手动设置

或者你可以手动设置。

  1. 安装 GitHub App

    前往 github.com/apps/opencode-agent。确保它已安装在目标仓库上。

  2. 添加 workflow

    将以下 workflow 文件添加到你仓库的 .github/workflows/opencode.yml 中。 确保在 env 中设置适当的 model 和所需的 API 密钥。

    bash
    name: opencode
    
    on:
      issue_comment:
        types: [created]
      pull_request_review_comment:
        types: [created]
    
    jobs:
      opencode:
        if: |
          contains(github.event.comment.body, '/oc') ||
          contains(github.event.comment.body, '/opencode')
        runs-on: ubuntu-latest
        permissions:
          id-token: write
        steps:
           - name: Checkout repository
             uses: actions/checkout@v6
             with:
               fetch-depth: 1
               persist-credentials: false
    
           - name: Run OpenCode
            uses: anomalyco/opencode/github@latest
            env:
              ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
            with:
              model: anthropic/claude-sonnet-4-20250514
              # share: true
              # github_token: xxxx
    
  3. 将 API 密钥存储在 secrets 中

    在你的组织或项目设置中,展开左侧的 Secrets and variables,然后选择 Actions。 并添加所需的 API 密钥。


##配置

  • model: 与 OpenCode 一起使用的模型。 采用 provider/model 的格式。 这是必需的。

  • agent: 要使用的 Agent。 必须是主 Agent。 如果找不到,则回退到配置中的 default_agent"build"

  • share: 是否共享 OpenCode 会话。 对于公共仓库,默认为 true

  • prompt: 可选的自定义 prompt,用于覆盖默认行为。 使用此选项可自定义 OpenCode 处理请求的方式。

  • token: 可选的 GitHub access token,用于执行创建评论、提交更改和打开 pull request 等操作。 默认情况下,OpenCode 使用来自 OpenCode GitHub App 的安装 access token,因此提交、评论和 pull request 显示为来自该 App。

    或者,你可以使用 GitHub Action runner 的 内置 GITHUB_TOKEN 而无需安装 OpenCode GitHub App。 只需确保在你的 workflow 中授予所需的权限:

    bash
    permissions:
      id-token: write
      contents: write
      pull-requests: write
      issues: write
    

    如果需要,你也可以使用 personal access tokens(PAT)。


##支持的事件

OpenCode 可以由以下 GitHub 事件触发:

| 事件类型 | 触发者 | 详细信息 | | ----------------------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | | issue_comment | 对 issue 或 PR 发表评论 | 在你的评论中提及 /opencode/oc。 OpenCode 读取上下文,并且可以创建分支、打开 PR 或回复。 | | pull_request_review_comment | 对 PR 中特定代码行发表评论 | 在审查代码时提及 /opencode/oc。 OpenCode 接收文件路径、行号和 diff 上下文。 | | issues | Issue 被打开或编辑 | 在创建或修改 issue 时自动触发 OpenCode。 需要 prompt 输入。 | | pull_request | PR 被打开或更新 | 在打开、同步或重新打开 PR 时自动触发 OpenCode。 适用于自动审查。 | | schedule | 基于 Cron 的计划任务 | 按照计划运行 OpenCode。 需要 prompt 输入。 输出将转到日志和 PR(没有 issue 可评论)。 | | workflow_dispatch | 从 GitHub UI 手动触发 | 通过 Actions 选项卡按需触发 OpenCode。 需要 prompt 输入。 输出将转到日志和 PR。 |

###计划任务示例

按计划运行 OpenCode 以执行自动化任务:

bash
name: Scheduled OpenCode Task

on:
  schedule:
    - cron: "0 9 * * 1" # Every Monday at 9am UTC

jobs:
  opencode:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
      pull-requests: write
      issues: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Run OpenCode
        uses: anomalyco/opencode/github@latest
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        with:
          model: anthropic/claude-sonnet-4-20250514
          prompt: |
            Review the codebase for any TODO comments and create a summary.
            If you find issues worth addressing, open an issue to track them.

对于计划事件,由于没有可从中提取指令的评论,因此 需要 prompt 输入。 计划的 workflow 在没有用户上下文的情况下运行以进行权限检查,因此如果你希望 OpenCode 创建分支或 PR,则 workflow 必须授予 contents: writepull-requests: write


###Pull Request 示例

在打开或更新 PR 时自动审查 PR:

bash
name: opencode-review

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  review:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
      pull-requests: read
      issues: read
    steps:
      - uses: actions/checkout@v6
        with:
          persist-credentials: false
      - uses: anomalyco/opencode/github@latest
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          model: anthropic/claude-sonnet-4-20250514
          use_github_token: true
          prompt: |
            Review this pull request:
            - Check for code quality issues
            - Look for potential bugs
            - Suggest improvements

对于 pull_request 事件,如果没有提供 prompt,OpenCode 默认审查 pull request。


###Issues 分流示例

自动分流新的 issue。 此示例会过滤掉 30 天以上的帐户,以减少垃圾邮件:

bash
name: Issue Triage

on:
  issues:
    types: [opened]

jobs:
  triage:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
      pull-requests: write
      issues: write
    steps:
      - name: Check account age
        id: check
        uses: actions/github-script@v7
        with:
          script: |
            const user = await github.rest.users.getByUsername({
              username: context.payload.issue.user.login
            });
            const created = new Date(user.data.created_at);
            const days = (Date.now() - created) / (1000 * 60 * 60 * 24);
            return days >= 30;
          result-encoding: string

      - uses: actions/checkout@v6
        if: steps.check.outputs.result == 'true'
        with:
          persist-credentials: false

      - uses: anomalyco/opencode/github@latest
        if: steps.check.outputs.result == 'true'
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        with:
          model: anthropic/claude-sonnet-4-20250514
          prompt: |
            Review this issue. If there's a clear fix or relevant docs:
            - Provide documentation links
            - Add error handling guidance for code examples
            Otherwise, do not comment.

对于 issues 事件,由于没有可从中提取指令的评论,因此 需要 prompt 输入。


##自定义 prompts

覆盖默认 prompt 以自定义 OpenCode 的行为以适应你的 workflow。

bash
- uses: anomalyco/opencode/github@latest
  with:
    model: anthropic/claude-sonnet-4-5
    prompt: |
      Review this pull request:
      - Check for code quality issues
      - Look for potential bugs
      - Suggest improvements

这对于强制执行特定的审查标准、编码标准或与你的项目相关的重点领域非常有用。


##示例

以下是一些如何在 GitHub 中使用 OpenCode 的示例。

  • 解释一个 issue

    在 GitHub issue 中添加此评论。

    bash
    /opencode explain this issue
    

    OpenCode 将读取整个线程,包括所有评论,并回复清晰的解释。

  • 修复一个 issue

    在 GitHub issue 中,说:

    bash
    /opencode fix this
    

    OpenCode 将创建一个新分支,实现更改,并打开一个包含更改的 PR。

  • 审查 PR 并进行更改

    在 GitHub PR 上留下以下评论。

    bash
    Delete the attachment from S3 when the note is removed /oc
    

    OpenCode 将实现请求的更改并将其提交到同一 PR。

  • 审查特定代码行

    直接在 PR 的“Files(文件)”选项卡中的代码行上留下评论。 OpenCode 会自动检测文件、行号和 diff 上下文,以提供精确的响应。

    bash
    [Comment on specific lines in Files tab]
    /oc add error handling here
    

    在评论特定行时,OpenCode 会收到:

    • 正在审查的确切文件
    • 特定的代码行
    • 周围的 diff 上下文
    • 行号信息

    这允许更有针对性的请求,而无需手动指定文件路径或行号。

Comments (Coming Soon)

Configure Giscus in environment variables to enable comments.