GitHub
在 GitHub 问题和拉取请求中使用 opencode。
opencode 与您的 GitHub 工作流程集成。在评论中提及 /opencode 或 /oc,opencode 将在您的 GitHub Actions 运行器中执行任务。
Features
- Triage issues: 要求 opencode 调查问题并向您解释。
- Fix and implement: 要求 opencode 修复问题或实施功能。将在一个新的分支中工作并提交包含所有更改的 PR。
- Secure: opencode 在 GitHub 的运行器中运行。
安装
在 GitHub 存储库中的项目中运行以下命令:
opencode github install这将引导您完成安装 GitHub 应用程序、创建工作流程和设置机密。
Manual Setup
或者您可以手动设置。
-
Install the GitHub app
前往 github.com/apps/opencode-agent。确保它已安装在目标存储库上。
-
Add the workflow
将以下工作流程文件添加到存储库中的
.github/workflows/opencode.yml中。确保在model中设置适当的env和所需的 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 -
Store the API keys in secrets
在您的组织或项目的 Settings 中,展开左侧的 Secrets and variables,然后选择 Actions。并添加所需的 API 密钥。
Configuration
-
model:与 opencode 一起使用的模型。采用provider/model格式。这是 必需的。 -
agent:要使用的代理。必须是一级代理。如果未找到,则从配置回退到default_agent或"build"。 -
share:是否共享 opencode 会话。对于公共存储库,默认为 true。 -
prompt:可选的自定义提示以覆盖默认行为。使用它来自定义 opencode 处理请求的方式。 -
token:可选的 GitHub 访问 Token,用于执行创建评论、提交更改和打开拉取请求等操作。默认情况下,opencode 使用来自 opencode GitHub 应用程序的安装访问 Token,因此提交、评论和拉取请求显示为来自应用。或者,您可以使用 GitHub Action 运行程序的 内置
GITHUB_TOKEN,而无需安装 opencode GitHub 应用程序。只需确保在您的工作流程中所需的权限:permissions:id-token: writecontents: writepull-requests: writeissues: write您可以在 GitHub Actions 文档中了解有关配置工作流程的更多信息。
Supported Events
opencode 可以由以下 GitHub 事件触发:
| Event Type | Triggered By | Details |
|---|---|---|
issue_comment | 对问题或 PR 发表评论 | 在评论中提及 /opencode 或 /oc。 opencode 读取上下文并可以创建分支、打开 PR 或回复。 |
pull_request_review_comment | 对 PR 中的特定代码行进行评论 | 在检查代码时提及 /opencode 或 /oc。 opencode 接收文件路径、行号和 diff 上下文。 |
issues | 问题已打开或已编辑 | 创建或修改问题时自动触发 opencode。需要 prompt 输入。 |
pull_request | PR 已开启或已更新 | 当 PR 打开、同步或重新打开时自动触发 opencode。对于自动评论很有帮助。 |
schedule | 基于 Cron 的计划 | 按计划运行 opencode。需要 prompt 输入。输出进入日志和 PR(没有可评论的问题)。 |
workflow_dispatch | 从 GitHub UI 手动触发 | 通过“操作”选项卡触发 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 将默认审核拉取请求。
Issue 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 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这对于执行与您的项目相关的特定审查标准、编码标准或重点领域非常有用。
示例
以下是如何在 GitHub 中使用 opencode 的一些示例。
-
Explain an issue
在 GitHub 问题中添加此评论。
/opencode explain this issueopencode 将阅读整个线程,包括所有评论,并回复并提供清晰的解释。
-
Fix an issue
在 GitHub 问题中,说:
/opencode fix thisopencode 将创建一个新分支,实施更改,并使用更改打开 PR。
-
Review PRs and make changes
在 GitHub PR 上留下以下评论。
Delete the attachment from S3 when the note is removed /ocopencode 将实施请求的更改并提交到同一个 PR。
-
Review specific code lines
直接在 PR 的“文件”选项卡中的代码行上留下评论。opencode 自动检测文件、行号和差异上下文以提供准确的响应。
[Comment on specific lines in Files tab]/oc add error handling here当评论特定行时,opencode 接收:
- 正在审查的确切文件
- 具体代码行
- 周围的差异上下文
- 行号信息
这允许更有针对性的请求,而无需手动指定文件路径或行号。