Jenkins
A Jenkins shared library gives a pipeline one step, nkCompare, which gets and checks nk, runs nk setup, keeps the saved baseline, compares two images and archives the four reports.
On this page
The words sandbox, compare, run and driver are defined at the top of Get started.
What you need #
Jenkins plugins:
- Pipeline and Git.
- Job Cacher, for the saved baseline. Without it the step still compares, in full each time.
- GitHub Checks, if the repository is on GitHub.
An agent with:
- Linux x86_64 and bash.
- curl.
- Docker that Nikasa can drive, and its compose plugin.
- unzip, for a baseline store. It unpacks the rclone that the store is reached with. The
jenkins/jenkinsimage has one.
Windows agents do not work, because nk-ci.sh is Linux only.
Add the library #
Nikasa gives Jenkins the step as a shared library.
Go to Manage Jenkins, System, Global Trusted Pipeline Libraries, and fill in:
| Field | Value |
|---|---|
| Name | nk |
| Default version | A Nikasa release tag, such as v0.3.14. The library and the script it carries are then the ones of that release. |
| Retrieval method | Modern SCM, Git, with the project repository https:// and a credential that can read it. |
| Library Path | jenkins |
To use your own copy instead, copy the library's jenkins/ folder to the root of a library repository of your own, and leave Library Path empty.
Add credentials #
The pipeline below uses three credentials:
| Credential | Kind | What it holds |
|---|---|---|
nk-release-token |
Secret text | The release token, for downloading nk. |
nk-secret-key |
Secret text | The secret key for saved baselines. |
registry-login |
Username with password | The registry's own login. A pull with no login fails the stage there. |
A pipeline #
This pipeline compares the image in production with this build:
@Library('nk@v0.3.14') _
pipeline {
agent { label 'docker' }
stages {
stage('Production image and this build') {
steps {
// Log in as your deploy job does; here, a Username with password credential.
withCredentials([usernamePassword(credentialsId: 'registry-login',
usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
sh 'echo "$REGISTRY_PASSWORD" | docker login -u "$REGISTRY_USER" --password-stdin registry.example.com'
}
sh '''
docker pull registry.example.com/app:production
docker tag registry.example.com/app:production app:old
docker build -t app:new .
'''
}
}
stage('nk') {
steps {
nkCompare(
old: 'app:old',
new: 'app:new',
app: 'nk/app.yaml',
version: '0.3.14',
repository: '<nikasa-repo>',
tokenId: 'nk-release-token',
secretKeyId: 'nk-secret-key',
// A folder every agent mounts keeps the saved baseline; leave this out to use Job Cacher.
baselineStore: 'file:///srv/jenkins/baselines'
)
}
}
}
}The agent label #
agent { label 'docker' } runs the build on a node labelled docker. Give that label to the nodes that have Docker (Manage Jenkins, Nodes, the node's Labels). Give it to the built-in node too when that is the one. Or write agent any.
Where Nikasa reads files #
Nikasa runs in the workspace, so app, specs and an accepted-changes file named with --accepted are relative to the checkout.
nk reads nk/specs when it is there. With no specs, the report covers startup only.
The store folder #
A store folder must be under a path the agent sees on the host too. Where the agent is in a container, that is under the folder its docker run mounts, as /srv/jenkins/baselines is here.
What you get #
- A passing build with the report. The build fails when Nikasa could not compare or a step before it failed. Reports are archived either way.
- Artifacts.
nk-reports/report.txt,report.json,report.mdandinformation.jsonl. The output folder is never archived. It stays inworkDir, not in the workspace that the Workspace page shows. - A build description. It is the report's first line, which counts the changes by area (The first line).
- A GitHub check, named
nk. Its summary is that first line and its text is the report. It appears when the build comes from a GitHub-backed multibranch project with a GitHub App. Otherwise nothing is published and the step goes on. A failure to publish only warns. - Ticket links. Each counted change in
report.mdhas a link that opens a new issue when Nikasa knows the tracker. Ticket links below says when it does.
A gate on changes #
With args: '--fail-on-change', the build also fails when the report lists a change in every new run. The failure reads nkCompare: nk exited 1.
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.
The build description #
The description counts the changes by area. For example:
Nikasa: 1 change, left behind; none in responses or requestsThat says there is 1 change, in the state the run left behind (database rows, changed files and how containers ended), and none in responses or requests.
When a named store did not get the baseline, the description says that too, and the build stays green:
Nikasa: 2 changes in responses and requests; the baseline was not savedThe nk-ci: warning line in the log says why.
Ticket links #
Unlike GitHub Actions, GitLab CI and Azure Pipelines, Jenkins does not know its own project on any host. It gives Nikasa only the checkout's address, GIT_URL. Nikasa reads the tracker from it only where the checkout is on github.com or gitlab.com.
For a self-managed GitLab, GitHub Enterprise or any other host, the Ticket column shows the change's key with no link until you name the tracker. Name it either way:
tracker:andtracker_kind:underx-nk:in the app file;args: '--tracker <project url> --tracker-kind gitlab'.
The issue points at the build with Jenkins' BUILD_URL. Jenkins sets that only when Manage Jenkins, System, Jenkins URL is filled in. Nikasa files nothing itself.
The app's config #
Nikasa reads settings in this order:
- The env file that
nk/app.yamlrecords asx-nk: env_file. It is a path fromnk/, as Compose reads a service'senv_file:. - The build's environment, which wins, as in Compose.
Give each setting that nk init lists as to provide as a credential of kind Secret text, in the environment of the stage:
stage('nk') {
environment {
APP_SECRET = credentials('app-secret')
}
steps {
nkCompare(/* as above */)
}
}Give a whole file #
Or give the whole file as a credential of kind Secret file. Its variable holds the path of a temporary copy. Here NK_ENV_FILE holds a path, not the file's text as the secret of that name does on GitHub.
Around nkCompare, hand the path to the compare and to the recording with args and recordArgs. nk reads it in place of the recorded file:
withCredentials([file(credentialsId: 'nk-env', variable: 'NK_ENV_FILE')]) {
nkCompare(
/* as above, and */
args: "--env-file ${env.NK_ENV_FILE}",
recordArgs: "--env-file ${env.NK_ENV_FILE}"
)
}Jenkins masks credentials in the log as best it can.
Your tests drive the compare #
Say once where your tests are, under x-nk: tests in the app file. Give your tests what they need shows how.
-
Check the old build's code out into a folder of the workspace, in a stage before the
nkone. Here it is a worktree of the base branch. Any checkout of the old build's code works:stage('Old build') { steps { // The base branch, in a folder of the workspace; any checkout of the old build's code works. sh 'rm -rf .nk-base && git worktree prune && git worktree add --detach .nk-base origin/main' } } -
Name that folder with
nkCompare(record: '.nk-base', ...).
The step then records your tests on the old build. The compare sends them again to both builds, and the reports open by saying how they went.
With no settings, or a failed recording, the build still passes. It runs specs, or sends no requests, and the reports say so (Your tests).
Give the secrets your tests read with withCredentials around the step.
A saved baseline #
With secretKeyId set, a build keeps the old build's runs. A later build that compares the same old build then runs only the new one. Without secretKeyId, the compare runs in full. How it decides says when a saved baseline is used.
The build 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 build that reads it never makes old runs to confirm one.
The baseline is kept in one of two places.
Job Cacher #
Job Cacher is used when no store is named.
- It keeps the baseline per job, in the job's
cachefolder on the controller, unless you configured other storage. - A new branch starts from
cacheDefaultBranch. - It saves a cache only when none exists for the key. So a baseline checked again after its check age is not saved again.
A store of your own #
A store is used when baselineStore, or the app file's baseline_store, names one. A store saves each change as a new object, so it has no such limit.
- Agents often share a disk. Then
file:///shared/nk-baselinesis the simplest. It costs nothing, every job and agent that mounts it shares it, and it is encrypted with the secret key. - For a bucket, sign in with whatever Jenkins step gives the build its cloud credentials, around
nkCompare. Examples arewithAWS, the OpenID Connect Provider plugin andwithCredentials.
Baseline storage lists the stores and how to sign in.
When the agent is in a container #
Nikasa may run in a container that drives the host's Docker through its socket. Then every folder it hands to Docker needs the same path on the host.
On Jenkins, those folders are:
- the workspace;
- its
@tmpfolder; NK_TOOLS;$HOME/.config/nk/build.
The image's HOME is its JENKINS_HOME, so move both to one shared path:
# /srv/jenkins is the same folder inside the container and on the host.
docker run -d --name jenkins -p 127.0.0.1:8080:8080 \
--user 1000:1000 --group-add "$(stat -c %g /var/run/docker.sock)" \
-v /srv/jenkins:/srv/jenkins \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker:ro \
-v /usr/libexec/docker/cli-plugins/docker-compose:/usr/libexec/docker/cli-plugins/docker-compose:ro \
-e JENKINS_HOME=/srv/jenkins/home -e HOME=/srv/jenkins/home \
jenkins/jenkins:lts-jdk21Keep the tools in one place #
Set NK_TOOLS to a folder under that path, around the step:
withEnv(['NK_TOOLS=/srv/jenkins/tools']) {
nkCompare(/* as above */)
}The tools are then made once for each Nikasa version, and later builds find them there. nk setup notes in that folder which build made them, by version and commit. It keeps them for that build and makes them again for another.
Builds that run at once #
Two builds that share NK_TOOLS must not run nk setup at once, because the second fails reading a half-written file. Either add options { disableConcurrentBuilds() }, or give each build its own NK_TOOLS.
The images the builds tag (app:old, app:new) live in the host's Docker. So concurrent builds of one pipeline need tags of their own too, such as app:new-${env.BUILD_NUMBER}.
Parameters #
| Parameter | Default | Meaning |
|---|---|---|
old, new |
required | The two images, already built or pulled on the agent. |
app |
required | The app file, as nk compare --app takes it. |
specs |
nk/ when it is there |
The folder of . files that drive the app. With none, the compare sends no requests and the report says so. A folder you name must exist. |
format |
text |
What the console shows. The reports are always written. |
jobs |
1 |
Runs at once. |
args |
none | More nk compare arguments, split on whitespace, such as --fail-on-change. |
version |
none | The Nikasa release to download, such as 0.3.14. Set it to the release in your @Library tag, without the v. |
repository |
none | Required in practice: the GitHub repository that release is in, <nikasa-repo> from your invite. |
releaseDir |
none | A folder on the agent holding a release's files, used instead of a download. |
nk |
none | An nk already on the agent, used as it is, unchecked. |
tokenId |
none | A Secret text credential holding a GitHub token that can read the release. |
secretKeyId |
none | A Secret text credential holding the secret key. Needed for a saved baseline. |
baseline |
true |
Keep the old build's runs for later builds. Only with secretKeyId. |
record |
none | The checkout to run your tests from (above). |
recordMinutes |
30 |
How long the recording may take. |
recordArgs |
none | More nk record arguments, split on whitespace. |
baselineExpiry |
7d |
The check age: how long a saved baseline is used before a fresh old run checks it. |
baselineStore |
the app file's x-nk: baseline_store |
Keep the saved baseline there instead of in Job Cacher: s3://, gs://, az:// or file:///. |
baselineStoreEndpoint, baselineStoreRegion |
none | An S3-compatible store's endpoint and region. |
rclone |
none | An rclone already on the agent, for a store. |
checks |
true |
Publish the report as a GitHub check, when the build comes from GitHub. |
cache |
true |
Keep the saved baseline in Job Cacher when no store is named. |
cacheDefaultBranch |
main |
The branch whose cache a new branch starts from. |
cacheMaxMb |
Job Cacher's | The most Job Cacher keeps for the baseline, in MB. |
workDir |
<workspace>@tmp/ |
The folder the step keeps everything in. It is emptied at the start of each call. |
One of version, releaseDir or nk says which Nikasa runs, and the first one set is used.
A parameter the step does not have stops the build with the list of those it has. The step returns [status: 0, reports: '<folder>'].
Next steps #
- Saved baselines: how a baseline is kept and when it is used again.
- Baseline storage: name a bucket and sign in to it.
- Record your tests: what Nikasa needs to run your tests.
- Tickets from a report: link each change to your tracker.
Written for nk 0.3.14. Something wrong or unclear? Write to hello@nikasa.dev.