ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

OpenCode 如何在 GitLab 的 Issue 与 Merge Request 中通过 @opencode 触发任务

2026/9/10 13:20:12 拓冰建站 浏览量
OpenCode 如何在 GitLab 的 Issue 与 Merge Request 中通过 @opencode 触发任务 OpenCode 如何在 GitLab 的 Issue 与 Merge Request 中通过 opencode 触发任务【免费下载链接】opencodeThe open source coding agent.项目地址: https://gitcode.com/GitHub_Trending/openc/opencode如果你希望不离开 GitLab就能让 AI 在 Issue 或 Merge RequestMR里解释问题、修 Bug、评审 MROpenCode 提供了这种集成方式在 GitLab Issue 或 MR 的评论中提及opencodeOpenCode 就会在你的 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 opencodegitlab.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 dont 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_OPENCODEservice account 的 GitLab token、GITLAB_HOST。glab是 GitLab CLIOpenCode 通过已认证的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 dont 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/opencode2 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_JSONmessage即该次执行传给 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),仅供参考