gitlab.mdx 4.7 KB

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