GitHub
在 GitHub issues 和 pull-requests 中使用 OpenCode。
OpenCode 集成到了你的 GitHub 工作流中。在你的评论中提及 /opencode 或 /oc,OpenCode 将在你的 GitHub Actions 运行器中执行任务。
功能 Features
- Triage issues (分类问题): 让 OpenCode 调查问题并向你解释。
- Fix and implement (修复和实现): 让 OpenCode 修复问题或实现功能。它会在一个新分支中工作并提交包含所有更改的 PR。
- Secure (安全): OpenCode 在你的 GitHub runner 内运行。
安装 Installation
在 GitHub 仓库的项目中运行以下命令:
opencode github install这将引导你安装 GitHub 应用程序,创建工作流并设置机密。
手动设置 Manual Setup
或者你可以手动设置它。
-
安装 GitHub 应用
前往 github.com/apps/opencode-agent。确保它已安装在目标仓库上。
-
添加工作流
将以下工作流文件添加到你仓库中的
.github/workflows/opencode.yml。确保在env中设置适当的model和所需的 API 密钥。.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: 1persist-credentials: false- 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 -
将 API 密钥存储在 secrets 中
在你的组织或项目 设置 中,展开左侧的 Secrets and variables 并选择 Actions。然后添加所需的 API 密钥。
配置 Configuration
-
model: 与 OpenCode 一起使用的模型。格式为provider/model。这是 必需的。 -
agent: 要使用的智能体。必须是主智能体。如果未找到,则回退到配置中的default_agent或"build"。 -
share: 是否分享 OpenCode 会话。对于公共仓库默认为 true。 -
prompt: 可选的自定义提示词,用于覆盖默认行为。使用此选项自定义 OpenCode 处理请求的方式。 -
token: 可选的 GitHub 访问令牌,用于执行创建评论、提交更改和打开拉取请求等操作。默认情况下,OpenCode 使用来自 OpenCode GitHub App 的安装访问令牌,因此提交、评论和拉取请求显示为来自该应用程序。或者,你可以使用 GitHub Action runner 的 内置
GITHUB_TOKEN而无需安装 OpenCode GitHub App。只需确保在你的工作流中授予所需的权限:permissions:id-token: writecontents: writepull-requests: writeissues: write如果愿意,你也可以拥使用 个人访问令牌(PAT)。
支持的事件 Supported Events
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。 |
计划任务示例 Schedule Example
按计划运行 OpenCode 以执行自动任务:
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 输入是 必需的,因为没有评论可供提取指令。计划的工作流在没有用户上下文的情况下进行权限检查运行,因此如果你期望 OpenCode 创建分支或 PR,工作流必须授予 contents: write 和 pull-requests: write。
拉取请求示例 Pull Request Example
当 PR 打开或更新时自动审查:
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 默认审查拉取请求。
Issues 分类示例 Issues Triage Example
自动分类新问题。此示例过滤掉超过 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-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 输入是 必需的,因为没有评论可供提取指令。
自定义提示词 Custom prompts
覆盖默认提示词以自定义 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这对强制执行特定审查标准、编码规范或与你的项目相关的关注领域非常有用。
示例 Examples
以下是如何在 GitHub 中使用 OpenCode 的一些示例。
-
Explain an issue (解释问题)
在 GitHub issue 中添加此评论。
/opencode explain this issueOpenCode 将阅读整个线程,包括所有评论,并回复清晰的解释。
-
Fix an issue (修复问题)
在 GitHub issue 中说:
/opencode fix thisOpenCode 将创建一个新分支,实施更改,并打开包含更改的 PR。
-
Review PRs and make changes (审查 PR 并进行更改)
在 GitHub PR 上留下以下评论。
Delete the attachment from S3 when the note is removed /ocOpenCode 将实施请求的更改并提交到同一 PR。
-
Review specific code lines (审查特定代码行)
在 PR 的“文件”选项卡中直接在代码行上发表评论。OpenCode 自动检测文件、行号和 diff 上下文以提供精确的回复。
[Comment on specific lines in Files tab]/oc add error handling here当评论特定行时,OpenCode 接收:
- 正在被审查的确切文件
- 特定的代码行
- 周围的 diff 上下文
- 行号信息
这允许更具针对性的请求,而无需手动指定文件路径或行号。