---
layout: doc
title: Publish Kubb snapshots with GitLab CI
description: Add a GitLab CI job that runs kubb studio snapshot on each merge request and compares the generated files with the target branch and the previous pipeline.
order: 4
navigation:
  title: GitLab CI
  icon: i-simple-icons-gitlab
---

> Index of every page on this site: [llms.txt](https://kubb.dev/llms.txt)

# Publish Kubb snapshots with GitLab CI

A GitLab job runs `kubb studio snapshot` on each merge request and protected branch. Meet the [prerequisites](/docs/5.x/integrations/ci#prerequisites) first: Kubb 5.6.2 or later, `tsdown`, and a CI key stored as `KUBB_TOKEN`.

## Add the job

Store `KUBB_TOKEN` as a masked and **Protected** CI/CD variable, so only pipelines for protected branches get it. Commit your npm lockfile, so `npm ci` is the only setup:

```yaml [.gitlab-ci.yml]
snapshot:
  stage: build
  image: node:22
  script:
    - npm ci
    - npx kubb studio snapshot
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $KUBB_TOKEN
    - if: $CI_COMMIT_BRANCH && $CI_COMMIT_REF_PROTECTED == "true" && $KUBB_TOKEN
```

- The second rule snapshots every protected branch, such as `main` or a release branch, so a merge request has a snapshot of its target branch to compare with.
- Kubb names a branch snapshot after GitLab's branch slug, so `release/1.0` and `release-1.0` share one. Give protected branches names that stay distinct as slugs.
- Both rules need `KUBB_TOKEN`, so a pipeline without the key, such as one for an unprotected branch or a fork, skips the job before `npm ci` runs.

> [!IMPORTANT]
> By default, GitLab does not pass protected variables to merge request pipelines. In GitLab 18.1 or later, select **Allow merge request pipelines to access protected variables and runners** under **Settings › CI/CD › Variables**. The pipeline then gets `KUBB_TOKEN` only when the source and target branches are both protected and in the same project, and the user who starts it can merge into the target branch. See [merge request pipelines](https://docs.gitlab.com/ci/pipelines/merge_request_pipelines/#control-access-to-protected-variables-and-runners).

## Post the result as a merge request note

Save the `--json` output and run this script. It updates one note per merge request and needs only Node:

```yaml [.gitlab-ci.yml]
  script:
    - npm ci
    - npx kubb studio snapshot --json > snapshot.json
    - '[ -z "$CI_MERGE_REQUEST_IID" ] || [ -z "$GITLAB_API_TOKEN" ] || node scripts/kubb-note.mjs'
```

> [!IMPORTANT]
> Writing a note needs a project access token with the `api` scope, stored as `GITLAB_API_TOKEN`. `CI_JOB_TOKEN` does not carry that scope.
>
> Mark `GITLAB_API_TOKEN` as **Protected** too, like `KUBB_TOKEN`. Masking only hides the value in job logs: any code the job runs can read it, including install scripts that `npm ci` runs. The `rules` skip the job without `KUBB_TOKEN`, and the last script line skips the note without `GITLAB_API_TOKEN`.
>
> Give the token the lowest role that can comment on merge requests, such as Reporter, and an expiry date.

::code-collapse{name="scripts/kubb-note.mjs"}

```js [scripts/kubb-note.mjs]
import { readFileSync } from 'node:fs'

const snapshot = JSON.parse(readFileSync('snapshot.json', 'utf8'))
const marker = '<!-- kubb-snapshot -->'
const counts = (c) => `${c.added.length} added · ${c.changed.length} changed · ${c.removed.length} removed`
const lines = [marker, `Kubb snapshot: \`npm i ${snapshot.url}\``]

const { branchChanges: branch, changes } = snapshot
if (branch) {
  if (branch.base) lines.push(`Changes against \`${branch.branch}\`: ${counts(branch)}`)
  else if (branch.baseFound) lines.push(`No snapshot of \`${branch.branch}\` for this package yet`)
  else lines.push(`No snapshot of \`${branch.branch}\` to compare with`)
}
if (changes?.base) lines.push(`Changes since ${changes.base.commit?.slice(0, 7) ?? changes.base.createdAt}: ${counts(changes)}`)

const { CI_API_V4_URL, CI_PROJECT_ID, CI_MERGE_REQUEST_IID, GITLAB_API_TOKEN } = process.env
const notes = `${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}/notes`
const headers = { 'PRIVATE-TOKEN': GITLAB_API_TOKEN, 'Content-Type': 'application/json' }
const existing = (await (await fetch(`${notes}?per_page=100`, { headers })).json()).find((note) => note.body.startsWith(marker))
const response = await fetch(existing ? `${notes}/${existing.id}` : notes, {
  method: existing ? 'PUT' : 'POST',
  headers,
  body: JSON.stringify({ body: lines.join('\n\n') }),
})
if (!response.ok) throw new Error(`GitLab answered ${response.status}`)
```

::

To install a snapshot or read it in Studio, see [Review the snapshot](/docs/5.x/integrations/ci#review-the-snapshot) and [Install the snapshot](/docs/5.x/integrations/ci#install-the-snapshot).

## Sitemap

See the full [sitemap](https://kubb.dev/sitemap.md) for all pages.
