Nikasa, home Request an invite
DocsGitLab CI

GitLab CI

A plain job in .gitlab-ci.yml that runs nk itself, on a runner whose Docker executor hands the job the host's Docker socket.

On this page

This page adds a job to .gitlab-ci.yml that compares the image in production with your pipeline's build, and keeps the reports as the job's artifacts.

The words sandbox, compare, run and driver are defined at the top of Get started.

What you need #

  • A Linux runner with Docker, registered with the Docker executor.
  • The release token from your invite, to download nk (the release token).
  • A registry that holds your production image.

Nikasa runs inside the job's container and drives the host's Docker. The rules for that apply: every folder Nikasa hands to Docker must have the same path on the host and inside the job. The sections below set this up.

Set up the runner #

Register the runner with the Docker executor. In its config.toml, give the job the Docker socket and one host folder at the same path inside the job:

toml
# The runner's config.toml.
[runners.docker]
  volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/srv/nk:/srv/nk"]

Build the job's image #

The job's image is docker:28-cli, which has Docker's CLI and compose plugin. It is Alpine, so you add Nikasa's static download to it.

  1. Download the static file from the release, with the token from your invite. This is the command in Install nk, with the static file named:

    shell
    gh release download v0.3.14 --repo <nikasa-repo> \
      --pattern nk-x86_64-unknown-linux-musl --pattern SHA256SUMS
  2. Check it:

    shell
    sha256sum -c --ignore-missing SHA256SUMS
  3. Put this Dockerfile next to the file:

    dockerfile
    FROM docker:28-cli
    COPY nk-x86_64-unknown-linux-musl /usr/local/bin/nk
    RUN chmod +x /usr/local/bin/nk
  4. Build the image once and push it where the runner can pull it.

Add the job #

This job compares the image in production with this pipeline's build:

yaml
# .gitlab-ci.yml
nk:
  image: registry.example.com/nk-job:28
  variables:
    NK_TOOLS: /srv/nk/tools
  script:
    - work="/srv/nk/$CI_JOB_ID" && mkdir -p "$work" && cp -r . "$work/src"
    # :production is the tag your deploy job pushes. GitLab sets these three for the project's registry.
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
    - docker pull "$CI_REGISTRY_IMAGE:production"
    - docker tag "$CI_REGISTRY_IMAGE:production" app:old
    - docker build -t app:new "$work/src"
    - export HOME=/srv/nk/home && mkdir -p "$HOME"
    - nk setup
    - cd "$work/src" && status=0
    # nk reads nk/app.yaml and nk/specs when they are there; with no specs the report covers startup only.
    - nk compare app:old app:new "$work/run" || status=$?
    - cp "$work"/run/report.* "$work"/run/information.jsonl "$CI_PROJECT_DIR/" || true
    - exit $status
  after_script:
    - rm -rf "/srv/nk/$CI_JOB_ID"
  artifacts:
    when: always
    paths: [report.txt, report.json, report.md, information.jsonl]

Where the files live #

The job copies the checkout under /srv/nk. That puts the output folder, the specs and the tools all under the folder shared with the host.

$CI_JOB_ID is new on every retry. after_script removes the job's folder once the reports are copied out. The folder holds the copy of the checkout and the output folder.

How the job stops #

Each line stops the job when it fails, so a pull the registry refuses ends the job there. The login comes before HOME moves, so the registry's credential stays in the job container's own home and never reaches the folder shared with the host.

The copy never hides Nikasa's status, because exit $status is a line of its own. When nk compare exits 2, the job fails with 2.

Add --fail-on-change to the nk compare line and a change in the report fails the job too.

The tools #

nk setup keeps what it made in NK_TOOLS, and notes there which Nikasa build made it, by version and commit.

  • The first job with a build makes the tools. This takes about 40 seconds with the images already on the runner.
  • Later jobs with that build find everything there and make nothing.
  • A new build makes them all again. nk setup --rebuild does the same whenever you ask.

Pipelines that run at once #

Pipelines running at once share the host's Docker, so the images tagged app:old and app:new collide. They also share NK_TOOLS and /srv/nk/home, and two nk setup runs at once must not share those.

Either run the job with resource_group: nk, or give each pipeline tags and folders of its own. Their sandbox networks do not collide, because each run takes a free one.

The app's config #

Nikasa reads settings in this order:

  1. The env file that nk/app.yaml records as x-nk: env_file. It is a path from nk/, as Compose reads a service's env_file:.
  2. The job's variables, which win, as in Compose.

GitLab hands a project variable to the job as an environment variable. So add each setting that nk init lists as to provide as a project variable of the same name.

The job above needs no other change.

Give a whole file #

To give the whole env file instead, add it as a File variable named NK_ENV_FILE. Here NK_ENV_FILE holds a path, not the file's text as the secret of that name does on GitHub. GitLab hands the job the path of a file that holds the text.

Name that path on the job's nk compare line, in place of the recorded file:

yaml
    - nk compare app:old app:new "$work/run" --env-file "$NK_ENV_FILE" || status=$?

The nk record and nk compare lines in the next section take the same flag through $ENV_FILE_ARG. It is empty when the job has no NK_ENV_FILE variable. The job under A saved baseline takes it through nk-ci.sh's variables.

Your tests drive the compare #

nk init --ci writes GitHub's job only, so on GitLab you add the recording by hand. It takes three steps.

  1. Say once where your tests are, under x-nk: tests in the app file. Give your tests what they need shows how.
  2. Record on the old build before the compare, from a checkout of the old build's code.
  3. Send what was recorded again, in the compare.

The script lines below replace the nk compare line above. They take the old build's commit from $CI_MERGE_REQUEST_DIFF_BASE_SHA.

Run the job on merge requests #

That variable is set only in merge request pipelines, and the job above runs on branch pipelines. So add these keys to the nk: job above, merged into it:

  • rules: says when the job runs.
  • variables: asks for the whole history, which the copied checkout needs for that commit. It also keeps NK_TOOLS.
yaml
nk:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  variables:
    GIT_DEPTH: 0
    NK_TOOLS: /srv/nk/tools

Record, then compare #

Then put these script lines where the nk compare line was:

yaml
    - OLD_COMMIT="$CI_MERGE_REQUEST_DIFF_BASE_SHA"
    # --env-file only when the job has a File variable named NK_ENV_FILE; with the settings given one by one, it stays empty.
    - ENV_FILE_ARG=""; if [ -n "${NK_ENV_FILE:-}" ]; then ENV_FILE_ARG="--env-file $NK_ENV_FILE"; fi
    - git -C "$work/src" worktree add -q "$work/base" "$OLD_COMMIT"
    # Nothing recorded does not fail the job: the compare goes on, with nk/specs or no requests.
    - pack=""; (cd "$work/base" && timeout -k 30 1800 nk record app:old "$work/pack" "$work/record" --app "$work/src/nk/app.yaml" $ENV_FILE_ARG) && pack="--pack $work/pack" || echo "nk record did not record your tests"
    - rm -rf "$work/record"
    - nk compare app:old app:new "$work/run" --app nk/app.yaml $ENV_FILE_ARG $pack || status=$?

What your tests read as ${NAME} comes from the job's masked variables.

A killed nk record leaves its containers running until the next run on its sandbox network.

Where the job's image has bash, the record step of nk-ci.sh does all of this. It also opens the report by saying how the tests went (Your tests).

What you get #

  • A passing job with the report. The job fails when Nikasa could not compare. With --fail-on-change on the nk compare line, it also fails when the report lists a change in every new run.
  • The reports as artifacts. They are kept whatever the job's result. Download them from the job's page. The artifact paths name only the four reports, so the output folder stays on the runner.
  • A comment, if you want one. Nikasa posts nothing itself, and report.md is written to be posted. Add a step of your own that sends it to the merge request's notes through GitLab's API, with a token you keep as a CI variable.
  • Ticket links. The link beside each change in report.md opens a new issue in the pipeline's own project and points at the job. There is nothing to set. For another tracker, see Tickets from a report.

Nikasa's exit status is:

Status When
0 It compared.
1 It compared with --fail-on-change, and a change showed in every new run.
2 It could not compare.

Exit status says more.

A saved baseline #

The job above runs the old build in full each time. A saved baseline keeps the old build's runs for later pipelines, and that needs a place to keep them.

GitLab sets a job's cache:key before its script runs, but Nikasa works out the baseline's key inside the script. So on GitLab the place is a baseline store: a bucket, or a folder shared by the runners.

Add the script to the image #

nk-ci.sh moves the baseline to and from the store. The job runs that script in place of nk setup and nk compare. It comes from the release, with the linux-asset.sh that it reads.

The script needs:

  • bash and curl;
  • unzip, for a store. It unpacks the rclone that moves the baseline.

docker:28-cli is Alpine, and its BusyBox has an unzip, so the image only adds bash and curl.

  1. Download the two scripts from the release the same way as nk, by adding --pattern nk-ci.sh --pattern linux-asset.sh to the command above.

  2. Run the same sha256sum line over them.

  3. Put all three files next to the Dockerfile:

    dockerfile
    FROM docker:28-cli
    RUN apk add --no-cache bash curl
    COPY nk-x86_64-unknown-linux-musl /usr/local/bin/nk
    COPY nk-ci.sh linux-asset.sh /usr/local/lib/nk/
    RUN chmod +x /usr/local/bin/nk
  4. Add the secret key as a masked CI variable named NK_CI_SECRET_KEY.

Change the job #

The job then becomes:

yaml
# .gitlab-ci.yml
nk:
  image: registry.example.com/nk-job:28
  resource_group: nk
  variables:
    NK_TOOLS: /srv/nk/tools
    NK_CI_NK: /usr/local/bin/nk
    NK_CI_WORK: /srv/nk/$CI_JOB_ID/nk-ci
    NK_CI_OLD: app:old
    NK_CI_NEW: app:new
    NK_CI_APP: nk/app.yaml
    # A folder on the host here. A bucket works the same: see Baseline storage for signing in.
    NK_CI_BASELINE_STORE: file:///srv/nk/baselines
  script:
    - work="/srv/nk/$CI_JOB_ID" && mkdir -p "$work" && cp -r . "$work/src"
    # :production is the tag your deploy job pushes. GitLab sets these three for the project's registry.
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
    - docker pull "$CI_REGISTRY_IMAGE:production"
    - docker tag "$CI_REGISTRY_IMAGE:production" app:old
    - docker build -t app:new "$work/src"
    - export HOME=/srv/nk/home && mkdir -p "$HOME"
    - cd "$work/src" && status=0
    - bash /usr/local/lib/nk/nk-ci.sh || status=$?
    - cp "$NK_CI_WORK"/nk-reports/* "$CI_PROJECT_DIR/" || true
    - exit $status
  after_script:
    - rm -rf "/srv/nk/$CI_JOB_ID"
  artifacts:
    when: always
    paths: [report.txt, report.json, report.md, information.jsonl]

The script uses nk/specs when it is there, and otherwise sends no requests, as the job above does.

Pass more arguments #

Two variables add words to what the script runs:

Variable What it adds to
NK_CI_ARGS The script's nk compare.
NK_CI_RECORD_ARGS The script's nk record.

To fail on a change here, set NK_CI_ARGS: --fail-on-change.

To give the whole env file as the NK_ENV_FILE variable of the app's config, set both:

yaml
  variables:
    NK_CI_ARGS: --env-file $NK_ENV_FILE
    NK_CI_RECORD_ARGS: --env-file $NK_ENV_FILE

NK_CI_ARGS is one value that the script splits at spaces. To do both in one job, put both words in it:

yaml
    NK_CI_ARGS: --fail-on-change --env-file $NK_ENV_FILE

The variables section of Any other CI lists every variable.

How many old runs it keeps #

The job that saves a baseline makes 8 old runs. That is the most a compare ever makes to confirm a change (How many runs). A later job that reads the baseline never makes old runs to confirm a change.

When the store cannot be reached #

A store that cannot be reached, or a store named with no secret key, only warns. The job stays green with Nikasa's own status and the compare runs in full. The script's last line is:

output
nk-ci: the baseline was not saved; the warning above says why

Next steps #

Written for nk 0.3.14. Something wrong or unclear? Write to hello@nikasa.dev.