gitlab.mdx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ---
  2. title: GitLab
  3. description: Use OpenCode in GitLab issues and merge requests.
  4. ---
  5. OpenCode integrates with your GitLab workflow through your GitLab CI/CD pipeline or with GitLab Duo.
  6. In both cases, OpenCode will run on your GitLab runners.
  7. ---
  8. ## GitLab CI
  9. OpenCode works in a regular GitLab pipeline. You can build it into a pipeline as a [CI component](https://docs.gitlab.com/ee/ci/components/)
  10. Here we are using a community-created CI/CD component for OpenCode — [nagyv/gitlab-opencode](https://gitlab.com/nagyv/gitlab-opencode).
  11. ---
  12. ### Features
  13. - **Use custom configuration per job**: Configure OpenCode with a custom configuration directory, for example `./config/#custom-directory` to enable or disable functionality per OpenCode invocation.
  14. - **Minimal setup**: The CI component sets up OpenCode in the background, you only need to create the OpenCode configuration and the initial prompt.
  15. - **Flexible**: The CI component supports several inputs for customizing its behavior
  16. ---
  17. ### Setup
  18. 1. Store your OpenCode authentication JSON as a File type CI environment variables under **Settings** > **CI/CD** > **Variables**. Make sure to mark them as "Masked and hidden".
  19. 2. Add the following to your `.gitlab-ci.yml` file.
  20. ```yaml title=".gitlab-ci.yml"
  21. include:
  22. - component: $CI_SERVER_FQDN/nagyv/gitlab-opencode/opencode@2
  23. inputs:
  24. config_dir: ${CI_PROJECT_DIR}/opencode-config
  25. auth_json: $OPENCODE_AUTH_JSON # The variable name for your OpenCode authentication JSON
  26. command: optional-custom-command
  27. message: "Your prompt here"
  28. ```
  29. For more inputs and use cases [check out the docs](https://gitlab.com/explore/catalog/nagyv/gitlab-opencode) for this component.
  30. ---
  31. ## GitLab Duo
  32. OpenCode integrates with your GitLab workflow.
  33. Mention `@opencode` in a comment, and OpenCode will execute tasks within your GitLab CI pipeline.
  34. ---
  35. ### Features
  36. - **Triage issues**: Ask OpenCode to look into an issue and explain it to you.
  37. - **Fix and implement**: Ask OpenCode to fix an issue or implement a feature.
  38. It will create a new branch and raise a merge request with the changes.
  39. - **Secure**: OpenCode runs on your GitLab runners.
  40. ---
  41. ### Setup
  42. OpenCode runs in your GitLab CI/CD pipeline, here's what you'll need to set it up:
  43. :::tip
  44. Check out the [**GitLab docs**](https://docs.gitlab.com/user/duo_agent_platform/agent_assistant/) for up to date instructions.
  45. :::
  46. 1. Configure your GitLab environment
  47. 2. Set up CI/CD
  48. 3. Get an AI model provider API key
  49. 4. Create a service account
  50. 5. Configure CI/CD variables
  51. 6. Create a flow config file, here's an example:
  52. <details>
  53. <summary>Flow configuration</summary>
  54. ```yaml
  55. image: node:22-slim
  56. commands:
  57. - echo "Installing opencode"
  58. - npm install --global opencode-ai
  59. - echo "Installing glab"
  60. - export GITLAB_TOKEN=$GITLAB_TOKEN_OPENCODE
  61. - apt-get update --quiet && apt-get install --yes curl wget gpg git && rm --recursive --force /var/lib/apt/lists/*
  62. - curl --silent --show-error --location "https://raw.githubusercontent.com/upciti/wakemeops/main/assets/install_repository" | bash
  63. - apt-get install --yes glab
  64. - echo "Configuring glab"
  65. - echo $GITLAB_HOST
  66. - echo "Creating OpenCode auth configuration"
  67. - mkdir --parents ~/.local/share/opencode
  68. - |
  69. cat > ~/.local/share/opencode/auth.json << EOF
  70. {
  71. "anthropic": {
  72. "type": "api",
  73. "key": "$ANTHROPIC_API_KEY"
  74. }
  75. }
  76. EOF
  77. - echo "Configuring git"
  78. - git config --global user.email "[email protected]"
  79. - git config --global user.name "OpenCode"
  80. - echo "Testing glab"
  81. - glab issue list
  82. - echo "Running OpenCode"
  83. - |
  84. opencode run "
  85. You are an AI assistant helping with GitLab operations.
  86. Context: $AI_FLOW_CONTEXT
  87. Task: $AI_FLOW_INPUT
  88. Event: $AI_FLOW_EVENT
  89. Please execute the requested task using the available GitLab tools.
  90. Be thorough in your analysis and provide clear explanations.
  91. <important>
  92. Please use the glab CLI to access data from GitLab. The glab CLI has already been authenticated. You can run the corresponding commands.
  93. 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.
  94. You don't need to commit or push up changes, those will be done automatically based on the file changes you make.
  95. </important>
  96. "
  97. - git checkout --branch $CI_WORKLOAD_REF origin/$CI_WORKLOAD_REF
  98. - echo "Checking for git changes and pushing if any exist"
  99. - |
  100. if ! git diff --quiet || ! git diff --cached --quiet || [ --not --zero "$(git ls-files --others --exclude-standard)" ]; then
  101. echo "Git changes detected, adding and pushing..."
  102. git add .
  103. if git diff --cached --quiet; then
  104. echo "No staged changes to commit"
  105. else
  106. echo "Committing changes to branch: $CI_WORKLOAD_REF"
  107. git commit --message "Codex changes"
  108. echo "Pushing changes up to $CI_WORKLOAD_REF"
  109. git push https://gitlab-ci-token:$GITLAB_TOKEN@$GITLAB_HOST/gl-demo-ultimate-dev-ai-epic-17570/test-java-project.git $CI_WORKLOAD_REF
  110. echo "Changes successfully pushed"
  111. fi
  112. else
  113. echo "No git changes detected, skipping push"
  114. fi
  115. variables:
  116. - ANTHROPIC_API_KEY
  117. - GITLAB_TOKEN_OPENCODE
  118. - GITLAB_HOST
  119. ```
  120. </details>
  121. You can refer to the [GitLab CLI agents docs](https://docs.gitlab.com/user/duo_agent_platform/agent_assistant/) for detailed instructions.
  122. ---
  123. ### Examples
  124. Here are some examples of how you can use OpenCode in GitLab.
  125. :::tip
  126. You can configure to use a different trigger phrase than `@opencode`.
  127. :::
  128. - **Explain an issue**
  129. Add this comment in a GitLab issue.
  130. ```
  131. @opencode explain this issue
  132. ```
  133. OpenCode will read the issue and reply with a clear explanation.
  134. - **Fix an issue**
  135. In a GitLab issue, say:
  136. ```
  137. @opencode fix this
  138. ```
  139. OpenCode will create a new branch, implement the changes, and open a merge request with the changes.
  140. - **Review merge requests**
  141. Leave the following comment on a GitLab merge request.
  142. ```
  143. @opencode review this merge request
  144. ```
  145. OpenCode will review the merge request and provide feedback.