GitLab Merge Review with Ollama [Gitlab CI/CD]

Pepe - Feb 25 - - Dev Community

In our last article, we created a simple script designed to review our code in GitLab. Now, we're taking the next step by integrating that very script into our CI/CD pipeline. By embedding it into GitLab's workflow, we can automatically review each merge request before it goes into production.

Sourcing Essential Environment Variables

For the review bot to interact with external services like GitLab and Jira, several environment variables must be configured. Here’s a breakdown of what you need and where to get each one:

  1. GitLab Variables:

GITLAB_URL: The URL of your GitLab instance (e.g., https://gitlab.com or your self-hosted GitLab URL). This is usually known within your organization. For self-hosted instances, it will be your domain.

GITLAB_PRIVATE_TOKEN: A personal access token used for authenticating API calls. Go to your GitLab profile settings. Navigate to Access Tokens.Generate a token with the required scopes (e.g., api).

GITLAB_PROJECT_ID can be found in the project’s settings or as as $CI_PROJECT_ID.

GITLAB_MR_ID is available in merge request pipelines as $CI_MERGE_REQUEST_IID.

  1. Jira Variables:

JIRA_URL: The URL of your Jira instance (e.g., https://yourcompany.atlassian.net). This is provided by your Jira administrator or can be seen in your Jira login URL.

JIRA_USER: Your Jira username or email address.

JIRA_API_TOKEN: An API token for Jira to authenticate REST API requests. Log in to your Atlassian account. Go to the API tokens section. Generate a new token.

JIRA_PROJECT_KEY: The key for the Jira project that you want to link with the merge request or issue. This can be found on the Jira project’s overview page. Can be found in user stories too.

Storing the Variables in GitLab

Once you have all these values, add them as CI/CD variables in your GitLab project:

  1. Go to your project’s Settings > CI/CD.
  2. Expand the Variables section.
  3. Add each variable securely by clicking on Add Variable.

This ensures that your sensitive data is not hard-coded into the repository and is only accessible during pipeline execution.

Overview of the Pipeline Configuration

Below is an example GitLab pipeline configuration for a review bot:

variables:
  GITLAB_RUNNER: "gitlab-docker"
  LLM_MODEL: "mistral"

stages:
  - gitlab_mr_review

review:
  stage: gitlab_mr_review
  tags:
    - $RUNNER_TAG
  services:
    - name: ollama/ollama:latest
      alias: ollama
      command: ["serve"]
  image: python:3.11
  before_script:
    - curl ollama:11434/api/pull -sS -X POST -d "{\"model\":\"$LLM_MODEL\",\"stream\":false}"
  script:
    - curl -sS ollama:11434
    - pip install -r requirements.txt
    - python review-bot/main.py
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here?

  1. Stages & Job:

    The pipeline defines a single stage gitlab_mr_review with one job called review. This job is responsible for interacting with the review bot.

  2. Tags & Services:

    • Tags: The job uses a runner tag specified by the environment variable $RUNNER_TAG, ensuring that it runs on an appropriate runner.
    • Services: A service container (ollama/ollama:latest) is spun up to run the model serving environment. It’s aliased as ollama for easy reference within the job.
  3. Image & Scripts:

    • The job uses a Python 3.11 image.
    • Before Script: A curl command is executed to pull a specific model ($LLM_MODEL) from the service.
    • Script: After pulling the model, it tests ollama response, then the job installs dependencies and runs the review bot located at review-bot/main.py.

Integrating the Review Bot into Different Pipelines

To allow various projects or pipelines to trigger the review bot, you can use GitLab’s trigger functionality. This trigger needs to be implemented in .gitlab-ci.yml of the project where you want to have the merge request review. Here’s how you can set it up:

trigger_review_bot:
  trigger:
    project: "group/review-bot-repo"
    branch: "main"
    strategy: depend
  variables:
    GITLAB_PROJECT_ID: $CI_PROJECT_ID
    GITLAB_MR_ID: $CI_MERGE_REQUEST_IID
  only:
    - merge_requests
Enter fullscreen mode Exit fullscreen mode

In the end the response of the gitlab review bot might look like this (example for dummy project with python inch to cm converter):

Image description

I hope you found this useful and that it helps you improve your CI/CD pipeline. Happy coding!

. . . .