OpenCode 如何在 GitLab 的 Issue 与 Merge Request 中通过 @opencode 触发任务
【免费下载链接】opencodeThe open source coding agent.项目地址: https://gitcode.com/GitHub_Trending/openc/opencode
如果你希望不离开 GitLab,就能让 AI 在 Issue 或 Merge Request(MR)里解释问题、修 Bug、评审 MR,OpenCode 提供了这种集成方式:在 GitLab Issue 或 MR 的评论中提及@opencode,OpenCode 就会在你的 GitLab CI pipeline 中执行任务。整个过程 OpenCode 运行在你的 GitLab runners 上。
OpenCode 与 GitLab 的集成有两条路径:
- GitLab Duo agent:在 Issue/MR 评论中写
@opencode触发,这是本文的主路径; - 普通 GitLab CI pipeline:把 OpenCode 作为 CI 组件挂进 pipeline,按 job 中的固定 prompt 执行,见文末的替代路径。
两条路径的共同前提是:OpenCode 跑在你的 GitLab runners 上,因此需要先有可用的 CI/CD 环境。
Duo 路径:准备条件
根据 GitLab 集成文档,Duo 路径的搭建需要完成以下六步。前 5 步的详细操作以 GitLab 官方的 Duo agent assistant 文档为准(文档明确提示以 GitLab 官方文档获取最新说明),这里列出文档给出的清单:
- 配置 GitLab 环境;
- 设置 CI/CD;
- 获取一个 AI 模型 provider 的 API key;
- 创建一个 service account;
- 配置 CI/CD variables;
- 创建 flow config 文件。
flow config 是 Duo 触发后真正定义“OpenCode 做什么”的 YAML 文件,下面给出文档中的完整示例并说明需要替换的地方。
创建 flow config 文件
以下是 gitlab.mdx 中给出的 flow 配置示例(文档示例,供参考):
image: node:22-slim commands: - echo "Installing opencode" - npm install --global opencode-ai - echo "Installing glab" - export GITLAB_TOKEN=$GITLAB_TOKEN_OPENCODE - apt-get update --quiet && apt-get install --yes curl wget gpg git && rm --recursive --force /var/lib/apt/lists/* - curl --silent --show-error --location "https://raw.githubusercontent.com/upciti/wakemeops/main/assets/install_repository" | bash - apt-get install --yes glab - echo "Configuring glab" - echo $GITLAB_HOST - echo "Creating OpenCode auth configuration" - mkdir --parents ~/.local/share/opencode - | cat > ~/.local/share/opencode/auth.json << EOF { "anthropic": { "type": "api", "key": "$ANTHROPIC_API_KEY" } } EOF - echo "Configuring git" - git config --global user.email "opencode@gitlab.com" - git config --global user.name "OpenCode" - echo "Testing glab" - glab issue list - echo "Running OpenCode" - | opencode run " You are an AI assistant helping with GitLab operations. Context: $AI_FLOW_CONTEXT Task: $AI_FLOW_INPUT Event: $AI_FLOW_EVENT Please execute the requested task using the available GitLab tools. Be thorough in your analysis and provide clear explanations. <important> Please use the glab CLI to access data from GitLab. The glab CLI has already been authenticated. You can run the corresponding commands. If you are asked to summarize an MR or issue or asked to provide more information then please post back a note to the MR/Issue so that the user can see it. You don't need to commit or push up changes, those will be done automatically based on the file changes you make. </important> " - git checkout --branch $CI_WORKLOAD_REF origin/$CI_WORKLOAD_REF - echo "Checking for git changes and pushing if any exist" - | if ! git diff --quiet || ! git diff --cached --quiet || [ --not --zero "$(git ls-files --others --exclude-standard)" ]; then echo "Git changes detected, adding and pushing..." git add . if git diff --cached --quiet; then echo "No staged changes to commit" else echo "Committing changes to branch: $CI_WORKLOAD_REF" git commit --message "Codex changes" echo "Pushing changes up to $CI_WORKLOAD_REF" git push https://gitlab-ci-token:$GITLAB_TOKEN@$GITLAB_HOST/gl-demo-ultimate-dev-ai-epic-17570/test-java-project.git $CI_WORKLOAD_REF echo "Changes successfully pushed" fi else echo "No git changes detected, skipping push" fi variables: - ANTHROPIC_API_KEY - GITLAB_TOKEN_OPENCODE - GITLAB_HOST使用前需要清楚这几点:
opencode run是非交互模式:它直接接收 prompt 并执行,不启动 TUI,适合脚本与自动化(见 CLI 文档 中run命令一节)。示例中$AI_FLOW_CONTEXT、$AI_FLOW_INPUT、$AI_FLOW_EVENT是 GitLab flow 机制注入的运行时变量,无需手动赋值。- auth.json:示例把 Anthropic 的 API key 写入
~/.local/share/opencode/auth.json。variables列表声明了需要注入的三个变量:ANTHROPIC_API_KEY(模型 provider 的 key,对应在准备条件第 3 步)、GITLAB_TOKEN_OPENCODE(service account 的 GitLab token)、GITLAB_HOST。 glab是 GitLab CLI:OpenCode 通过已认证的glab访问 Issue/MR 数据。glab issue list这一步用于在正式运行前验证 glab 认证是否正常。- 副作用提醒:配置中的
apt-get install、curl ... | bash会在 runner 容器中安装系统包并下载执行远程脚本,git push会把 OpenCode 产生的文件变更提交并推送到$CI_WORKLOAD_REF分支。 - 必须替换的示例值:
git push地址中的gl-demo-ultimate-dev-ai-epic-17570/test-java-project.git是文档示例中的仓库路径,替换为你自己项目的group/project路径;$GITLAB_HOST由 CI/CD variables 提供,保持原样即可。
在 Issue 与 Merge Request 中触发任务
flow config 配置完成后,按文档给出的示例,在 GitLab 中直接评论即可触发(文档原文):
解释 Issue— 在 GitLab issue 中添加评论:
@opencode explain this issueOpenCode 会读取 issue 并回复一条清晰的解释。
修复 Issue— 在 GitLab issue 中评论:
@opencode fix thisOpenCode 会创建一个新分支,实现修改,并带着这些变更发起一个 merge request。
评审 Merge Request— 在 GitLab merge request 中留下评论:
@opencode review this merge requestOpenCode 会评审该 MR 并给出反馈。
文档同时说明:触发词可以配置成@opencode以外的其他短语。
结果验证
判断任务是否按预期执行,依据文档中明确给出的行为:
- explain / review 类任务:flow 配置中的 prompt 明确要求 OpenCode 把结论回帖到 MR/Issue 评论("post back a note to the MR/Issue so that the user can see it"),因此验证方式就是查看该 Issue/MR 下是否出现了 OpenCode 的回复评论;
- fix 类任务:检查是否出现了新分支,以及是否自动打开了带变更的 merge request;
- 认证链路:pipeline 中的
glab issue list是文档内置的自检步骤,若这一步失败,说明GITLAB_TOKEN_OPENCODE未正确配置,后续opencode run也无法访问 GitLab 数据。
另外,prompt 中声明了"You don't need to commit or push up changes, those will be done automatically based on the file changes you make"——提交与推送由 flow config 末尾的 git 步骤自动完成,判断推送是否发生可以看 runner 日志中是否出现 "Git changes detected, adding and pushing..." 或 "No git changes detected, skipping push"。
替代路径:普通 CI pipeline 中的 CI 组件
如果你不需要在评论中触发,只想让 OpenCode 作为 pipeline 的一部分按固定 prompt 执行,文档给出了另一条路径:使用社区创建的 CI/CD 组件nagyv/gitlab-opencode,把它组装进 pipeline(按 GitLab CI component 的方式引用)。
该组件的特点(文档原文):
- 按 job 使用自定义配置:可用自定义配置目录(如
./config/#custom-directory)在每次 OpenCode 调用中启用/禁用功能; - 最小化配置:组件在后台完成 OpenCode 的安装,你只需要准备 OpenCode 配置和初始 prompt;
- 灵活:组件支持多个输入项用于定制行为。
搭建步骤(文档原文):
- 把你的 OpenCode 认证 JSON 以File 类型保存到Settings > CI/CD > Variables中,并标记为 "Masked and hidden";
- 在项目的
.gitlab-ci.yml中加入:
include: - component: $CI_SERVER_FQDN/nagyv/gitlab-opencode/opencode@2 inputs: config_dir: ${CI_PROJECT_DIR}/opencode-config auth_json: $OPENCODE_AUTH_JSON # The variable name for your OpenCode authentication JSON command: optional-custom-command message: "Your prompt here"其中auth_json指向第 1 步中创建的认证 JSON 变量名(示例用OPENCODE_AUTH_JSON),message即该次执行传给 OpenCode 的 prompt。组件的其他输入项,文档指向该组件在 GitLab CI/CD catalog 中的页面获取,本文不展开。
限制与边界
- 两条路径下 OpenCode 都运行在你的 GitLab runners 上,runner 不可用时任务不会执行;
- Duo 路径的前 5 步(GitLab 环境、CI/CD、service account 等)细节由 GitLab 官方 Duo agent 文档维护,OpenCode 文档只给出清单和 flow config 示例,升级 GitLab 版本时以官方文档为准;
@opencode是默认触发词,更换触发词需要在 GitLab 侧完成配置。
【免费下载链接】opencodeThe open source coding agent.项目地址: https://gitcode.com/GitHub_Trending/openc/opencode
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考