GitLab
在 GitLab Issue 和 MR 中使用 OpenCode。
OpenCode 可以通过 GitLab CI/CD Pipeline 或 GitLab Duo 集成到你的 GitLab 工作流中。
无论哪种方式,OpenCode 都会运行在你的 GitLab Runner 上。
GitLab CI
OpenCode 可以像普通 Job 一样运行在 GitLab Pipeline 中。你可以将它封装为一个 CI 组件。
这里我们使用一个社区维护的 OpenCode CI/CD 组件 —— nagyv/gitlab-opencode。
功能
- 按 Job 使用自定义配置:为不同 Job 指定不同的 OpenCode 配置目录(如
./config/#custom-directory),按调用场景启用/禁用功能。 - 最小化配置:组件会在后台帮你安装和启动 OpenCode,你只需准备 OpenCode 配置和初始 Prompt。
- 灵活:支持多种输入参数,用来定制行为。
配置示例
-
将 OpenCode 的认证 JSON 以“文件类型”的 CI 变量存储在 Settings → CI/CD → Variables 中,并勾选 “Masked and hidden”。
-
在
.gitlab-ci.yml中添加:.gitlab-ci.yml include:- component: $CI_SERVER_FQDN/nagyv/gitlab-opencode/opencode@2inputs:config_dir: ${CI_PROJECT_DIR}/opencode-configauth_json: $OPENCODE_AUTH_JSON # 这里是你在 CI 变量中保存的 OpenCode auth JSONcommand: optional-custom-commandmessage: "Your prompt here"
更多输入项和使用方式可参考该组件的文档。
GitLab Duo 集成
OpenCode 也可以和 GitLab Duo 一起工作。
在评论中 @ OpenCode 触发:
@opencode do somethingOpenCode 会在你的 GitLab CI Pipeline 中执行任务。
功能
- Issue 分析:让 OpenCode 阅读 Issue 并给出解释或建议。
- 修复与实现功能:让 OpenCode 修复 Issue 或实现新功能,它会创建新分支并发起 Merge Request。
- 安全:OpenCode 运行在你的 GitLab Runner 上。
配置步骤
OpenCode 会在你的 GitLab CI/CD Pipeline 中运行,整体需要:
-
配置 GitLab 环境
-
设置 CI/CD
-
获取 AI 模型提供商 API Key
-
创建 Service Account
-
配置 CI/CD 变量
-
创建 Flow 配置文件(示例):
Flow 配置示例
image: node:22-slimcommands:- 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.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_CONTEXTTask: $AI_FLOW_INPUTEvent: $AI_FLOW_EVENTPlease 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)" ]; thenecho "Git changes detected, adding and pushing..."git add .if git diff --cached --quiet; thenecho "No staged changes to commit"elseecho "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_REFecho "Changes successfully pushed"fielseecho "No git changes detected, skipping push"fivariables:- ANTHROPIC_API_KEY- GITLAB_TOKEN_OPENCODE- GITLAB_HOST
更多详情参考 GitLab CLI Agents 文档。
使用示例
下面是一些在 GitLab 中使用 OpenCode 的示例:
-
解释 Issue
在 GitLab Issue 中评论:
@opencode explain this issueOpenCode 会阅读 Issue 并回复一段更清晰的解释。
-
修复 Issue
在 Issue 中评论:
@opencode fix thisOpenCode 会创建一个新分支,实现修改并发起 Merge Request。
-
审查 Merge Request
在 MR 中评论:
@opencode review this merge requestOpenCode 会审查 MR,并给出反馈和建议。