任務(wù))
OpenCode 如何在 GitLab 的 Issue 與 Merge Request 中通過 opencode 觸發(fā)任務(wù)【免費下載鏈接】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 中執(zhí)行任務(wù)。整個過程 OpenCode 運行在你的 GitLab runners 上。OpenCode 與 GitLab 的集成有兩條路徑GitLab Duo agent在 Issue/MR 評論中寫opencode觸發(fā)這是本文的主路徑普通 GitLab CI pipeline把 OpenCode 作為 CI 組件掛進 pipeline按 job 中的固定 prompt 執(zhí)行見文末的替代路徑。兩條路徑的共同前提是OpenCode 跑在你的 GitLab runners 上因此需要先有可用的 CI/CD 環(huán)境。Duo 路徑準備條件根據(jù) GitLab 集成文檔Duo 路徑的搭建需要完成以下六步。前 5 步的詳細操作以 GitLab 官方的 Duo agent assistant 文檔為準文檔明確提示以 GitLab 官方文檔獲取最新說明這里列出文檔給出的清單配置 GitLab 環(huán)境設(shè)置 CI/CD獲取一個 AI 模型 provider 的 API key創(chuàng)建一個 service account配置 CI/CD variables創(chuàng)建 flow config 文件。flow config 是 Duo 觸發(fā)后真正定義“OpenCode 做什么”的 YAML 文件下面給出文檔中的完整示例并說明需要替換的地方。創(chuàng)建 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 并執(zhí)行不啟動 TUI適合腳本與自動化見 CLI 文檔 中run命令一節(jié)。示例中$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對應(yīng)在準備條件第 3 步、GITLAB_TOKEN_OPENCODEservice account 的 GitLab token、GITLAB_HOST。glab是 GitLab CLIOpenCode 通過已認證的glab訪問 Issue/MR 數(shù)據(jù)。glab issue list這一步用于在正式運行前驗證 glab 認證是否正常。副作用提醒配置中的apt-get install、curl ... | bash會在 runner 容器中安裝系統(tǒng)包并下載執(zhí)行遠程腳本git push會把 OpenCode 產(chǎn)生的文件變更提交并推送到$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 中觸發(fā)任務(wù)flow config 配置完成后按文檔給出的示例在 GitLab 中直接評論即可觸發(fā)文檔原文解釋 Issue— 在 GitLab issue 中添加評論opencode explain this issueOpenCode 會讀取 issue 并回復(fù)一條清晰的解釋。修復(fù) Issue— 在 GitLab issue 中評論opencode fix thisOpenCode 會創(chuàng)建一個新分支實現(xiàn)修改并帶著這些變更發(fā)起一個 merge request。評審 Merge Request— 在 GitLab merge request 中留下評論opencode review this merge requestOpenCode 會評審該 MR 并給出反饋。文檔同時說明觸發(fā)詞可以配置成opencode以外的其他短語。結(jié)果驗證判斷任務(wù)是否按預(yù)期執(zhí)行依據(jù)文檔中明確給出的行為explain / review 類任務(wù)flow 配置中的 prompt 明確要求 OpenCode 把結(jié)論回帖到 MR/Issue 評論post back a note to the MR/Issue so that the user can see it因此驗證方式就是查看該 Issue/MR 下是否出現(xiàn)了 OpenCode 的回復(fù)評論fix 類任務(wù)檢查是否出現(xiàn)了新分支以及是否自動打開了帶變更的 merge request認證鏈路pipeline 中的glab issue list是文檔內(nèi)置的自檢步驟若這一步失敗說明GITLAB_TOKEN_OPENCODE未正確配置后續(xù)opencode run也無法訪問 GitLab 數(shù)據(jù)。另外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 步驟自動完成判斷推送是否發(fā)生可以看 runner 日志中是否出現(xiàn) Git changes detected, adding and pushing... 或 No git changes detected, skipping push。替代路徑普通 CI pipeline 中的 CI 組件如果你不需要在評論中觸發(fā)只想讓 OpenCode 作為 pipeline 的一部分按固定 prompt 執(zhí)行文檔給出了另一條路徑使用社區(qū)創(chuàng)建的 CI/CD 組件nagyv/gitlab-opencode把它組裝進 pipeline按 GitLab CI component 的方式引用。該組件的特點文檔原文按 job 使用自定義配置可用自定義配置目錄如./config/#custom-directory在每次 OpenCode 調(diào)用中啟用/禁用功能最小化配置組件在后臺完成 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 步中創(chuàng)建的認證 JSON 變量名示例用OPENCODE_AUTH_JSONmessage即該次執(zhí)行傳給 OpenCode 的 prompt。組件的其他輸入項文檔指向該組件在 GitLab CI/CD catalog 中的頁面獲取本文不展開。限制與邊界兩條路徑下 OpenCode 都運行在你的 GitLab runners 上runner 不可用時任務(wù)不會執(zhí)行Duo 路徑的前 5 步GitLab 環(huán)境、CI/CD、service account 等細節(jié)由 GitLab 官方 Duo agent 文檔維護OpenCode 文檔只給出清單和 flow config 示例升級 GitLab 版本時以官方文檔為準opencode是默認觸發(fā)詞更換觸發(fā)詞需要在 GitLab 側(cè)完成配置。【免費下載鏈接】opencodeThe open source coding agent.項目地址: https://gitcode.com/GitHub_Trending/openc/opencode創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考