Opencode Gitlab
OpenCode can also be integrated into the GitLab workflow, automatically executing tasks in Issues, Merge Requests, and CI/CD pipelines.
Its core feature is: running on your GitLab Runner to achieve secure and controllable AI-powered automated development.
**OpenCode GitLab = Integrating AI into CI/CD, enabling automated code analysis, fixes, and reviews.**
* * *
## I. Overview of Integration Methods
There are mainly two ways to use OpenCode in GitLab:
* **GitLab CI (Recommended)**: Execute OpenCode via CI/CD pipelines
* **GitLab Duo (Comment Trigger)**: Trigger via @opencode in comments
* * *
## II. GitLab CI Integration (Core Method)
OpenCode can be integrated as a CI component into your pipeline for automated task execution.
Here we use the community component:
nagyv/gitlab-opencode
### 1. Feature Highlights
* **Custom Configuration per Task**: Supports independent configuration directories
* **Minimal Integration Cost**: No complex initialization required
* **Highly Customizable**: Supports multi-parameter control of behavior
### 2. Configuration Steps
#### β Configure Authentication Information
In the GitLab project:
* Go to Settings β CI/CD β Variables
* Add variable:
OPENCODE_AUTH_JSON
**Requirements:**
* Type: File
* Check Masked and Hidden
* * *
#### β‘ Configure Pipeline
Create or modify in the project root directory:
.gitlab-ci.yml
Example:
include: - component: $CI_SERVER_FQDN/nagyv/gitlab-opencode/opencode@2 inputs: config_dir: ${CI_PROJECT_DIR}/opencode-config auth_json: $OPENCODE_AUTH_JSON command: optional-custom-command message: "Explain the codebase structure"
* * *
#### β’ Parameter Description
| Parameter | Description |
| --- | --- |
| config_dir | OpenCode configuration directory |
| auth_json | Authentication information (API Key) |
| command | Execution command (optional) |
| message | Prompt |
### 3. Use Cases
* Automatically analyze code structure
* Batch refactor code
* Generate documentation automatically
* Automatically check code quality during CI stage
* * *
## III. GitLab Duo Integration (Comment Trigger)
Similar to GitHubβs `/opencode`, GitLab uses **@opencode** to trigger actions.
When you comment in an Issue or Merge Request:
@opencode your prompt
OpenCode will automatically execute tasks in GitLab CI.
### 1. Feature Highlights
* **Issue Analysis**: Automatically understand issues
* **Auto Fix**: Create branch and submit MR
* **Code Review**: Analyze merge requests
**Features:**
* Runs on GitLab Runner
* Secure, does not rely on external execution environments
### 2. Configuration Steps
To use @opencode, complete the following configurations:
* Configure GitLab CI/CD pipeline
* Prepare API Key
* Create Service Account
* Configure CI/CD Variables
* Create Flow configuration file
**Note:**
Refer to the official GitLab CLI Agents documentation for detailed instructions (differences may exist across versions).
* * *
## IV. Common Usage Patterns
### 1. Explain Issue
@opencode explain this issue
Automatically summarize and explain the issue
### 2. Auto-fix Issue
@opencode fix this
Automatically:
* Create new branch
* Modify code
* Submit Merge Request
### 3. Review Merge Request
@opencode review this merge request
Automatically analyze:
* Code quality
* Potential bugs
* Optimization suggestions
* * *
## V. Advanced Tips
### 1. Custom Trigger Words
You can replace `@opencode` with other trigger keywords.
### 2. Task-level Configuration
By using different config_dir, you can achieve:
* Different models for different tasks
* Different rules for different projects
### 3. Combine with CI Automation
You can implement:
* Scheduled scanning of code issues
* Automatic weekly report generation
* Automatic fixing of simple bugs
* * *
## VI. Security and Best Practices
* Store keys using CI/CD Variables
* Avoid hardcoding API Keys in code
* Control Runner permissions appropriately
* Limit the scope of automatic code modifications
YouTip