GitHub
在 GitHub Issue 和 PR 中使用 OpenCode。
OpenCode 可以集成到你的 GitHub 工作流中。在评论里提到 /opencode 或 /oc,OpenCode 就会在你的 GitHub Actions Runner 中执行任务。
功能
- Issue 分析与归类:让 OpenCode 帮你阅读 Issue 并用更清晰的语言解释。
- 修复与实现功能:让 OpenCode 修复 Issue 或实现新功能。它会在新分支上工作,并提交包含所有更改的 PR。
- 安全:OpenCode 运行在你的 GitHub Runner 内部。
安装
在一个已连接 GitHub 仓库的项目中运行:
opencode github install这会引导你安装 GitHub App、创建 workflow,并配置必要的 secrets。
手动配置
你也可以手动完成配置。
-
安装 GitHub App
前往 github.com/apps/opencode-agent,将其安装到目标仓库。
-
添加工作流文件
在仓库中新增
.github/workflows/opencode.yml,并根据需要设置model及所需的 API Key(通过env传入):.github/workflows/opencode.yml name: opencodeon: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-latestpermissions:id-token: writesteps:- name: Checkout repositoryuses: actions/checkout@v6with:fetch-depth: 1- name: Run OpenCodeuses: anomalyco/opencode/github@latestenv:ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}with:model: anthropic/claude-sonnet-4-20250514# share: true# github_token: xxxx -
在 Secrets 中保存 API Key
打开组织或仓库 Settings → Secrets and variables → Actions,添加所需的 API Key,并在上面的 workflow 中通过
secrets.XXX引用。
配置项
-
model:OpenCode 使用的模型,格式为provider/model,必填。 -
agent:要使用的 Agent,必须是主 Agent;若未指定,会回退到配置中的default_agent,再不行则使用内置"build"。 -
share:是否分享 OpenCode 会话。对公开仓库默认 true。 -
prompt:可选,自定义提示词,用于覆盖默认行为,定制 OpenCode 处理请求的方式。 -
token:可选,GitHub Access Token,用于创建评论、提交、开 PR 等操作。默认情况下会使用 OpenCode GitHub App 的安装 Token,所以提交和评论会以 App 身份出现。你也可以只使用 GitHub Action Runner 自带的
GITHUB_TOKEN,不安装 App。需要在 workflow 中明确授予权限:permissions:id-token: writecontents: writepull-requests: writeissues: write或者使用 Personal Access Token(PAT)。
支持的事件
OpenCode 可以通过以下 GitHub 事件触发:
| 事件类型 | 触发方式 | 说明 |
|---|---|---|
issue_comment | 在 Issue 或 PR 中评论 | 评论中包含 /opencode 或 /oc 时触发;OpenCode 会读取上下文、创建分支、开 PR 或回复。 |
pull_request_review_comment | 在 PR 的具体代码行上写 Review 评论 | 在评论中提到 /opencode 或 /oc 即可;会收到文件路径、行号和 diff 上下文。 |
issues | Issue 被创建或编辑 | Issue 事件自动触发 OpenCode;必须在 workflow 中提供 prompt。 |
pull_request | PR 被创建或更新 | PR 打开、同步、重新打开时触发,常用于自动代码审查。 |
schedule | 定时任务(Cron) | 定时执行;需要提供 prompt;输出写入日志和 PR(没有 Issue 可评论时)。 |
workflow_dispatch | GitHub UI 中手动触发 | 在 Actions 页按需触发;需要提供 prompt;输出写入日志和 PR。 |
定时任务示例
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
- 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.对于 schedule 事件,必须提供 prompt,因为没有评论可供提取指令。由于定时任务没有用户上下文用于权限确认,如果需要创建分支或 PR,workflow 中必须授予 contents: write 和 pull-requests: write 权限。
Pull Request 审查示例
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 - uses: anomalyco/opencode/github@latest env: ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} with: model: anthropic/claude-sonnet-4-20250514 prompt: | Review this pull request: - Check for code quality issues - Look for potential bugs - Suggest improvements对 pull_request 事件,如果没有提供 prompt,OpenCode 会默认做一次 PR 代码审查。
Issue 分析示例
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'
- 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.这里对新建 Issue 做自动分流示例,并通过账号创建时间简单过滤垃圾账号。
自定义 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
在 Issue 中评论:
/opencode explain this issueOpenCode 会阅读整个对话(包括所有评论),然后回复一段更易理解的解释。
-
修复 Issue
在 Issue 中评论:
/opencode fix thisOpenCode 会创建新分支、实现修复,并打开一个包含更改的 PR。
-
审查 PR 并直接修改
在 PR 里发表评论:
Delete the attachment from S3 when the note is removed /ocOpenCode 会在同一个 PR 上直接实现该修改,并推送提交。
-
审查指定代码行
在 PR 的 “Files changed” 标签页里,对具体代码行发表评论:
[Comment on specific lines in Files tab]/oc add error handling here此时 OpenCode 会收到:
- 正在审查的文件
- 具体的代码行
- 相关 diff 上下文
- 行号信息
这样你无需手动写文件路径或行号,也能发起精准的修改请求。