OpenCode GitLab
OpenCode can also integrate into GitLab workflows, automatically executing tasks in Issues, Merge Requests, and CI/CD pipelines.
Its core feature: runs on your GitLab Runner, enabling secure and controllable AI-automated development.
OpenCode GitLab = connect AI to CI/CD, automating code analysis, fixes, and reviews.
I. Integration overview
OpenCode has two main usage modes 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 a community component is used:
nagyv/gitlab-opencode
1. Features
- Custom configuration per task: supports independent config directories
- Minimize integration cost: no complex initialization needed
- Highly customizable: supports multiple parameters to control behavior
2. Configuration steps
① Configure authentication info
In your GitLab project:
- Go to Settings → CI/CD → Variables
- Add a variable:
OPENCODE_AUTH_JSON
Requirements:
- Type: File
- Check Masked and Hidden
② Configure the pipeline
Create or modify in the project root:
.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
| Parameters | Description |
|---|---|
| config_dir | OpenCode config directory |
| auth_json | Authentication info (API Key) |
| command | Execution command (optional) |
| message | Prompt |
3. Use cases
- Automatically analyze code structure
- Batch refactor code
- Automatically generate documentation
- Automatically check code quality in the CI stage
III. GitLab Duo integration (comment trigger)
Similar to GitHub's /opencode, GitLab uses @opencode .
When you comment in an Issue or Merge Request:
@opencode your prompt
OpenCode will automatically execute tasks in GitLab CI
1. Features
- Issue analysis: automatically understand the problem
- Auto-fix: create a branch and submit an MR
- Code review: analyze the Merge Request
Characteristics:
- Runs on GitLab Runner
- Secure, no reliance on external execution environments
2. Configuration steps
To use @opencode, you need to complete the following configuration:
- Configure the GitLab CI/CD pipeline
- Prepare an API Key
- Create a Service Account
- Configure CI/CD Variables
- Create a Flow config file
Explanation:
For details, refer to the official GitLab CLI Agents documentation (slightly different between versions).
IV. Common usage patterns
1. Explain an Issue
@opencode explain this issue
Automatically summarize problems and give explanations
2. Auto-fix an Issue
@opencode fix this
Automatically:
- Create a new branch
- Modify the code
- Submit a Merge Request
3. Review a Merge Request
@opencode review this merge request
Automatically analyze:
- Code quality
- Potential bugs
- Optimization Suggestions
V. Advanced tips
1. Custom trigger words
You can think of @opencode Replace with other trigger keywords.
2. Task-level configuration
Through different config_dir, achieve:
- Different tasks use different models
- Different projects use different rules
3. Combined with CI automation
You can achieve:
- Scheduled scanning of code issues
- Automatically generate weekly reports
- Automatically fix simple bugs
VI. Security and best practices
- Use CI/CD Variables to store secrets
- Avoid hardcoding API Keys in code
- Control Runner permissions appropriately
- Limit the scope of automatic code modification