GitHub

在 GitHub issues 與 pull requests 中使用 OpenCode。透過 /opencode 觸發智慧代理,搭配 GitHub Actions 自動審查、修復問題。

本頁內文若連結到尚未提供繁體版本的頁面(例如 /ecosystem/plugins/、/news/),會開啟英文原頁;導覽與頁尾標有 EN 的項目即為這類頁面。

OpenCode 可以與你的 GitHub 工作流程整合。在評論中提及 /opencode 或 /oc,OpenCode 就會在你的 GitHub Actions runner 中執行任務。


功能

  • 分類 issue:讓 OpenCode 調查某個 issue 並向你解釋。
  • 修復和實作:讓 OpenCode 修復某個 issue 或實作某個功能。它將在新分支中工作,並提交包含所有變更的 PR。
  • 安全:OpenCode 在你的 GitHub runner 中執行。

安裝

在位於 GitHub 儲存庫的專案中執行以下命令:

opencode github install

該命令將引導你完成 GitHub App 的安裝、工作流程的建立以及 secrets 的設定。


手動設定

你也可以手動進行設定。

  1. 安裝 GitHub App

    前往 github.com/apps/opencode-agent。確保它已安裝到目標儲存庫上。

  2. 新增工作流程

    將以下工作流程檔案新增到儲存庫中的 .github/workflows/opencode.yml。請確保在 env 中設定合適的 model 和所需的 API key。

    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-5-5
  3. 將 API key 儲存到 secrets

    在你的組織或專案的 Settings 中,展開左側的 Secrets and variables,選擇 Actions,然後新增所需的 API key。


設定

  • model:OpenCode 使用的模型。格式為 provider/model。必填。

  • agent:要使用的智慧代理。必須是 primary 模式的智慧代理。若找不到,則回退到設定中的 default_agent,或回退到 "build"。

  • prompt:可選的自訂 prompt,用於覆蓋預設行為。可用於自訂 OpenCode 處理請求的方式。

  • GitHub 存取權限:預設使用 OpenCode GitHub App 的安裝 access token,因此提交、評論和 pull request 都顯示為來自該 App。若想改用 runner 自帶的 GITHUB_TOKEN,請設定 use_github_token: true(見下)並授予所需權限:

    或者,你也可以使用 GitHub Action runner 自帶的 GITHUB_TOKEN,而不必安裝 OpenCode GitHub App。只需確保在工作流程中授予所需的權限即可:

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

    如果你願意,也可以使用 personal access token(PAT)。

  • mentions:觸發短語的逗號分隔清單,不區分大小寫。預設為 /opencode,/oc。

  • variant:用於 provider 特定推理強度(reasoning effort)的模型變體,例如 high、max 或 minimal。

  • oidc_base_url:OIDC token 交換 API 的基礎 URL。僅在執行自訂 GitHub App 安裝時需要。預設為 https://api.opencode.ai。

  • use_github_token:設為 true 時使用呼叫方提供的 GITHUB_TOKEN,而不是透過 OIDC token 交換取得 OpenCode App 安裝 token。預設為 false。設定後無需 id-token: write 權限,因為略過了 OIDC 交換。


支援的事件

OpenCode 可以由以下 GitHub 事件觸發:

事件類型觸發方式說明
issue_comment在 issue 或 PR 上評論在評論中提及 /opencode 或 /oc。OpenCode 會讀取上下文,並可建立分支、發起 PR 或回覆。
pull_request_review_comment在 PR 中的特定程式碼行上評論在 review 程式碼時提及 /opencode 或 /oc。OpenCode 會接收檔案路徑、行號和 diff 上下文。
issuesissue 被開啟或編輯當 issue 被建立或修改時自動觸發 OpenCode。需要 prompt 輸入。
pull_requestPR 被開啟或更新當 PR 被開啟、同步或重新開啟時自動觸發 OpenCode。適用於自動化 review。
schedule以 cron 為基礎的排程任務依排程執行 OpenCode。需要 prompt 輸入。輸出會寫入日誌和 PR(沒有 issue 可供評論)。
workflow_dispatch從 GitHub UI 手動觸發透過 Actions 標籤頁依需求觸發 OpenCode。需要 prompt 輸入。輸出會寫入日誌和 PR。

排程任務範例

依排程執行 OpenCode 來執行自動化任務:

name: Scheduled OpenCode Task

on:
  schedule:
    - cron: "0 9 * * 1" # 每個週一 UTC 時間 9 點

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-5-5
          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 輸入是必填的,因為沒有評論可供擷取指令。排程工作流程執行時沒有使用者上下文用於權限檢查,因此如果你希望 OpenCode 建立分支或 PR,工作流程必須授予 contents: write 和 pull-requests: write。


Pull Request 範例

在 PR 被開啟或更新時自動進行 review:

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-5-5
          use_github_token: true
          prompt: |
            Review this pull request:
            - Check for code quality issues
            - Look for potential bugs
            - Suggest improvements

對於 pull_request 事件,如果沒有提供 prompt,OpenCode 預設會 review 該 pull request。


Issue 分類範例

自動對新 issue 進行分類。此範例會過濾掉註冊時間小於 30 天的帳號,以減少垃圾資訊:

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-5-5
          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 輸入是必填的,因為沒有評論可供擷取指令。


自訂 prompt

覆蓋預設 prompt,以針對你的工作流程自訂 OpenCode 的行為。

- 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

這對於強制執行特定的 review 標準、程式碼規範或與你的專案相關的關注點非常有用。


範例

以下是一些在 GitHub 中使用 OpenCode 的範例。

  • 解釋 issue

    在 GitHub issue 中新增如下評論。

    /opencode explain this issue

    OpenCode 會閱讀整個討論(包括所有評論),並給出清晰的解釋作為回覆。

  • 修復 issue

    在 GitHub issue 中說:

    /opencode fix this

    OpenCode 會建立一個新分支,實作相關變更,並發起一個包含這些變更的 PR。

  • Review PR 並修改

    在 GitHub PR 上留下以下評論:

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

    OpenCode 會實作所請求的變更,並將其提交到同一個 PR 中。

  • Review 特定的程式碼行

    在 PR 的 “Files” 標籤頁中直接對程式碼行進行評論。OpenCode 會自動識別檔案、行號和 diff 上下文,從而提供精準的回覆。

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

    當你對特定行進行評論時,OpenCode 會收到:

    • 正在 review 的具體檔案
    • 特定的程式碼行
    • 周圍的 diff 上下文
    • 行號資訊

    這樣你就可以發起更具針對性的請求,而無需手動指定檔案路徑或行號。