Skip to content

GitLab CI

This job reviews every merge request, prints the findings in its log and keeps them as an artifact of the pipeline. Add it to .gitlab-ci.yml:

kerpeten:
image: node:24-bookworm
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID == $CI_PROJECT_ID
variables:
GIT_DEPTH: 0
before_script:
- apt-get update && apt-get install -y ripgrep
- npm install -g kerpeten
script:
- kerpeten review --dry-run --base "$CI_MERGE_REQUEST_DIFF_BASE_SHA" > review.txt
- cat review.txt
artifacts:
when: always
paths:
- review.txt

This job needs no token and no glab. It names the change with --base, so it has no merge request to write to and runs with --dry-run: without it the review stops with exit code 2. To get the findings as comments on the merge request, see Writing the comments.

  1. Run kerpeten init in the repository on your machine and commit the .kerpeten.yml it writes. Getting started shows how.
  2. Add the key of your provider as a variable of the project: Settings → CI/CD → Variables, with the key OPENROUTER_API_KEY, Masked ticked and Protect variable not ticked. A protected variable is only given to pipelines of protected branches, and the branch of a merge request is not one. For another provider use its name: ANTHROPIC_API_KEY or OPENAI_API_KEY.
Part Why
image: node:24-bookworm Debian with Node.js and npm. Kerpeten has no build for Alpine, so an Alpine image does not work.
rules Runs the job for merge requests only, and not for one from a fork: a fork’s pipeline does not get the key.
GIT_DEPTH: 0 Fetches the whole history. The review needs the base commit, which a shallow clone does not have.
ripgrep The models search the code with it.
$CI_MERGE_REQUEST_DIFF_BASE_SHA The commit where the branch of the merge request left its target branch. The review is of the change from there to the checked out commit.
artifacts Keeps review.txt with the pipeline, also when the review fails.

In a merge request pipeline Kerpeten takes the number of the merge request from CI_MERGE_REQUEST_IID and reads the merge request itself, through glab. --mr <n> names another one; before 0.5.0, write --mr "$CI_MERGE_REQUEST_IID". The reviewer then gets the title and the description, and the output has the comment plan: the comments the review would write. With --dry-run, as here, it writes none of them, so a token that only reads is enough.

The job needs two more things:

  • glab in the image. The example installs its Debian package.
  • A token that can read the merge request, as a masked variable named GITLAB_TOKEN. A project access token with the scope read_api is enough.

allow_failure: true lets a failed review show as a warning on the pipeline without blocking the merge request. The reason is in the log. Remove it to make the pipeline fail.

kerpeten:
image: node:24-bookworm
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID == $CI_PROJECT_ID
allow_failure: true
variables:
GIT_DEPTH: 0
GLAB_VERSION: "1.132.0"
before_script:
- apt-get update && apt-get install -y ripgrep
- curl -fsSL -o glab.deb "https://gitlab.com/gitlab-org/cli/-/releases/v${GLAB_VERSION}/downloads/glab_${GLAB_VERSION}_linux_amd64.deb"
- dpkg -i glab.deb && rm glab.deb
- npm install -g kerpeten
script:
- kerpeten review --dry-run > review.txt
- cat review.txt
artifacts:
when: always
paths:
- review.txt

On gitlab.com glab takes the token from GITLAB_TOKEN. On your own GitLab host it does not send that token to a host it does not know, so sign in to the host first. Add this line before the review:

- glab auth login --hostname "$CI_SERVER_HOST" --token "$GITLAB_TOKEN"

A project with merged results pipelines checks out a merge commit, and HEAD is then another commit than the last one of the merge request. The review of the merge request stops there.

Use --base with the last commit of the target branch, which GitLab gives in such a pipeline:

script:
- kerpeten review --dry-run --base "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA" > review.txt

Take the job of With the title and the description and drop --dry-run. The job then writes its comments to the merge request:

script:
- kerpeten review
  • The token in GITLAB_TOKEN now needs the scope api; read_api only reads. CI_JOB_TOKEN is not enough.
  • Add GITLAB_TOKEN like the key: masked, and not protected. With a protected token the job cannot read the merge request and stops with merge request <n> was not found.
  • A project access token is the tidy choice: the comments come from a bot user named after the token, not from a person.
  • A run after a push reviews only the new commits.
  • The job ends with exit code 1 when a comment could not be written. The log says which one and why.

Reviewing has the details.

A review with findings ends with exit code 0. To make the job fail when a finding scores high enough for a comment on its line, write JSON and check it. jq is not in the image, so install it with ripgrep:

before_script:
- apt-get update && apt-get install -y ripgrep jq
- npm install -g kerpeten
script:
- kerpeten review --dry-run --base "$CI_MERGE_REQUEST_DIFF_BASE_SHA" --json > review.json
- jq -e '[.findings[] | select(.action == "inline")] | length == 0' review.json
  • The key variable follows the provider in .kerpeten.yml.
  • A model with its own base_url also needs the variable KERPETEN_TRUSTED_ENDPOINTS. Configuration says what to put there.