中文
文档
GitLab 集成

GitLab

在 GitLab issue 和 merge request 中使用 OpenCode。

OpenCode 通过你的 GitLab CI/CD 流水线或 GitLab Duo 集成到你的 GitLab 工作流中。

无论哪种情况,OpenCode 都会在你的 GitLab runner 上运行。


GitLab CI

OpenCode 可在常规的 GitLab 流水线中工作。你可以将它作为一个 CI 组件 (opens in a new tab) 集成到流水线中。

这里我们使用一个由社区创建的 OpenCode CI/CD 组件——nagyv/gitlab-opencode (opens in a new tab)


特性

  • 每个 job 使用自定义配置 :使用自定义配置目录来配置 OpenCode,例如 ./config/#custom-directory,以便按每次 OpenCode 调用来启用或禁用功能。
  • 极简设置 :CI 组件会在后台设置好 OpenCode,你只需要创建 OpenCode 配置和初始提示。
  • 灵活 :CI 组件支持多个输入项来自定义其行为。

设置

  1. 将你的 OpenCode 认证 JSON 存储为 File 类型的 CI 环境变量,位置在 Settings > CI/CD > Variables。务必将它们标记为 "Masked and hidden"。

  2. 将以下内容添加到你的 .gitlab-ci.yml 文件中。

    .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"

要了解更多输入项和用例,请查看该组件的文档 (opens in a new tab)


GitLab Duo

OpenCode 集成到你的 GitLab 工作流中。 在评论中提及 @opencode,OpenCode 就会在你的 GitLab CI 流水线中执行任务。


特性

  • issue 分类 :让 OpenCode 调查某个 issue 并向你解释它。
  • 修复与实现 :让 OpenCode 修复某个 issue 或实现某个功能。 它会创建一个新分支并提出一个带有改动的 merge request。
  • 安全 :OpenCode 在你的 GitLab runner 上运行。

设置

OpenCode 在你的 GitLab CI/CD 流水线中运行,以下是设置它所需的内容:

提示: 查看 GitLab 文档 (opens in a new tab) 获取最新的说明。

  1. 配置你的 GitLab 环境

  2. 设置 CI/CD

  3. 获取一个 AI 模型 provider 的 API key

  4. 创建一个服务账号

  5. 配置 CI/CD 变量

  6. 创建一个 flow 配置文件,以下是一个示例:

    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 "[email protected]"
      - 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

你可以参考 GitLab CLI agents 文档 (opens in a new tab) 获取详细说明。


示例

以下是一些在 GitLab 中使用 OpenCode 的示例。

提示: 你可以配置使用与 @opencode 不同的触发短语。

  • 解释一个 issue 在 GitLab issue 中添加这条评论。

    @opencode explain this issue

    OpenCode 会读取该 issue 并回复一个清晰的解释。

  • 修复一个 issue 在 GitLab issue 中说:

    @opencode fix this

    OpenCode 会创建一个新分支、实现改动,并打开一个带有改动的 merge request。

  • 审查 merge request 在 GitLab merge request 上留下以下评论。

    @opencode review this merge request

    OpenCode 会审查该 merge request 并给出反馈。