Appearance
OpenCode GitHub 集成让你在 Issue 或 PR 评论中用 /opencode 或 /oc 触发 AI Agent。Agent 在 GitHub Actions Runner 上运行,可以分析问题、新建分支、提交代码,并自动开 PR——代码始终在你的基础设施内执行。
OpenCode 与 GitHub 工作流深度集成。在 Issue 或 PR 评论中提及 /opencode 或 /oc,OpenCode 就会在 GitHub Actions Runner 上执行任务。
主要功能
- Issue 分析:让 OpenCode 分析 Issue,给出详细说明
- 修复与实现:让 OpenCode 修复 Bug 或实现功能,它会新建分支并提交包含所有改动的 PR
- 安全可控:OpenCode 运行在你自己的 GitHub Runner 上,代码不离开你的基础设施
快速安装
在 GitHub 仓库中执行:
bash
opencode github install这个命令会引导你完成安装 GitHub App、创建 Workflow 文件、配置 Secrets 的全过程。
手动配置
如果需要手动安装:
第一步:安装 GitHub App
访问 github.com/apps/opencode-agent,安装到目标仓库。
第二步:创建 Workflow 文件
在仓库的 .github/workflows/opencode.yml 中添加以下内容,根据需要修改 model 和 API 密钥:
yaml
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第三步:配置 Secrets
在仓库或组织的 Settings → Secrets and variables → Actions 中添加所需的 API 密钥。
配置参数
| 参数 | 说明 |
|---|---|
model | 必填。使用的模型,格式为 provider/model |
agent | 使用的 Agent,默认回退到配置中的 default_agent 或 "build" |
share | 是否分享会话,公开仓库默认为 true |
prompt | 自定义 Prompt,覆盖默认行为 |
token | GitHub Access Token,默认使用 OpenCode GitHub App 的安装令牌 |
如果不安装 GitHub App,可以使用 Actions 内置的 GITHUB_TOKEN,但需要在 Workflow 中声明权限:
yaml
permissions:
id-token: write
contents: write
pull-requests: write
issues: write支持的触发事件
| 事件类型 | 触发时机 | 说明 |
|---|---|---|
issue_comment | Issue 或 PR 评论 | 评论中包含 /opencode 或 /oc 时触发 |
pull_request_review_comment | PR 代码行评论 | 可获取文件路径、行号和 diff 上下文 |
issues | Issue 创建或编辑 | 需要提供 prompt 参数 |
pull_request | PR 打开或更新 | 无 prompt 时默认进行 Code Review |
schedule | Cron 定时任务 | 需要 prompt 参数,输出到日志和 PR |
workflow_dispatch | 手动触发 | 需要 prompt 参数 |
定时任务示例
每周一自动检查 TODO 注释:
yaml
name: Scheduled OpenCode Task
on:
schedule:
- cron: "0 9 * * 1"
jobs:
opencode:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
pull-requests: write
issues: write
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false
- uses: anomalyco/opencode/github@latest
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
with:
model: anthropic/claude-sonnet-4-20250514
prompt: |
检查代码库中所有 TODO 注释并整理摘要。
如果发现值得处理的问题,开一个 Issue 来跟踪。自动 PR 审查示例
PR 打开时自动进行代码审查:
yaml
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: |
审查这个 Pull Request:
- 检查代码质量问题
- 查找潜在 Bug
- 提出改进建议使用示例
分析 Issue:在 Issue 中评论
/opencode 解释这个问题的原因OpenCode 会读取整个 Issue 线程并给出清晰说明。
修复 Bug:在 Issue 中评论
/opencode fix thisOpenCode 会新建分支、实现修复并开 PR。
PR 代码修改:在 PR 评论中
删除笔记时同步从 S3 删除附件 /ocOpenCode 会实现这个改动并 commit 到同一个 PR。
精确行级修改:在 PR 的 "Files" Tab 中对具体代码行评论
/oc 这里加错误处理OpenCode 会自动获取文件路径、行号和 diff 上下文,无需手动说明。
常见问题
Q: OpenCode GitHub App 和使用 GITHUB_TOKEN 有什么区别?
A: 使用 GitHub App 时,提交和评论会以 OpenCode App 身份出现。使用 GITHUB_TOKEN 时以 github-actions bot 身份出现。两者功能相同,只是显示名称不同。
Q: 可以限制哪些用户能触发 OpenCode 吗?
A: 可以在 Workflow 的 if 条件中加入发起者检查,例如 github.actor == 'your-username',以限制允许触发的用户范围。
Q: schedule 触发时为什么必须提供 prompt?
A: 定时触发没有关联的 Issue 或 PR 评论,OpenCode 无法自动确定任务目标,因此必须手动指定 prompt。