Nikasa, home Request an invite
DocsAzure Pipelines

Azure Pipelines

The nk-ci.sh script does the work, and the pipeline adds what is Azure's own, the cache, the artifact and the run's summary.

On this page

This page adds a pipeline that compares the image in production with this build. The nk-ci.sh script does the work, and the pipeline adds what is Azure's own: the cache, the artifact and the run's summary.

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

What you need #

A Linux x86_64 agent with:

  • bash and curl;
  • Docker and Docker's compose plugin;
  • the GitHub CLI (gh), for the step below that downloads nk-ci.sh.

Add the secrets #

The pipeline needs two secret pipeline variables:

Variable What it holds
NK_RELEASE_TOKEN The release token, for downloading nk and nk-ci.sh.
NK_SECRET_KEY The secret key for saved baselines.

A pipeline #

This pipeline compares the image in production with this build:

yaml
# azure-pipelines.yml
pool:
  vmImage: ubuntu-latest

steps:
  - script: |
      docker pull registry.example.com/app:production
      docker tag registry.example.com/app:production app:old
      docker build -t app:new .
    displayName: Production image and this build

  # The script and the file it sources, from the release, checked.
  - script: |
      v=0.3.14
      gh release download "v$v" --repo <nikasa-repo> --dir nk-ci-script \
        --pattern nk-ci.sh --pattern linux-asset.sh --pattern SHA256SUMS
      (cd nk-ci-script && sha256sum -c --ignore-missing SHA256SUMS)
    displayName: Get nk-ci.sh
    env:
      GH_TOKEN: $(NK_RELEASE_TOKEN)

  - script: bash nk-ci-script/nk-ci.sh install setup record key
    displayName: nk setup, your tests and the saved baseline's key
    env:
      NK_CI_VERSION: 0.3.14
      NK_CI_REPOSITORY: <nikasa-repo>
      NK_CI_TOKEN: $(NK_RELEASE_TOKEN)
      NK_CI_SECRET_KEY: $(NK_SECRET_KEY)
      NK_CI_OLD: app:old
      NK_CI_NEW: app:new
      NK_CI_APP: nk/app.yaml

  # Keyed on the contents of the key file, so a new key is a new cache entry.
  - task: Cache@2
    inputs:
      key: 'nk-baseline | "$(Agent.OS)" | nk-ci/nk-baseline.key'
      path: nk-ci/nk-baseline
    displayName: Saved baseline

  - script: bash nk-ci-script/nk-ci.sh compare save reports
    displayName: Compare
    env:
      NK_CI_OLD: app:old
      NK_CI_NEW: app:new
      NK_CI_APP: nk/app.yaml

  - task: PublishPipelineArtifact@1
    condition: succeededOrFailed()
    inputs:
      targetPath: nk-ci/nk-reports
      artifact: nk-report
    displayName: Upload the reports

  - script: echo "##vso[task.uploadsummary]$PWD/nk-ci/nk-reports/report.md"
    condition: succeededOrFailed()
    displayName: Summary

Where files go #

nk-ci.sh keeps its files in nk-ci/, in the folder each step runs in.

Nikasa runs from the checkout. So NK_CI_APP, NK_CI_SPECS and an accepted-changes file named with --accepted are relative to it.

nk reads nk/specs when it is there. With no specs, the report covers startup only.

Pass more arguments #

Two variables add words to what nk-ci.sh runs:

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

To make a change fail the run, set NK_CI_ARGS: --fail-on-change in the env of both nk-ci.sh steps. Both need it because the baseline's key is worked out from the compare's own arguments.

The Any other CI page lists every variable.

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 step's environment, which wins, as in Compose.

Azure maps no secret variable into a script by itself. So map each setting that nk init lists as to provide into the env of both steps that run nk-ci.sh, as they are the ones that run Nikasa:

yaml
    env:
      APP_SECRET: $(APP_SECRET)

Give a whole file #

To give a whole file instead:

  1. Add it under Pipelines, Library, Secure files.
  2. Download it with Azure's DownloadSecureFile@1 task, before the first nk-ci.sh step.
  3. Hand its path to both steps through NK_CI_ARGS and NK_CI_RECORD_ARGS, as Any other CI says.
yaml
  - task: DownloadSecureFile@1
    name: nkEnv
    inputs:
      secureFile: nk.env

  # In the env of both nk-ci.sh steps, and with no space in the path:
  #   NK_CI_ARGS: --env-file $(nkEnv.secureFilePath)
  #   NK_CI_RECORD_ARGS: --env-file $(nkEnv.secureFilePath)

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.

Then add two things to the pipeline above.

Check out the old build #

Check the old build's code out into .nk-base. Do it after the images are built and before nk setup, because .nk-base sits inside the new image's build context.

Here it is a worktree of the base branch, which needs the checkout's full history. Any checkout of the old build's code works:

yaml
  - checkout: self
    fetchDepth: 0

  - script: git worktree add .nk-base origin/main
    displayName: The old build's code

Turn recording on #

Add NK_CI_RECORD: .nk-base to the env of both nk-ci.sh steps, the one that runs record and the compare.

NK_CI_RECORD records your tests from that folder on the old build. The compare sends them again to both builds, and the reports open by saying how they went. With no NK_CI_RECORD, nothing is recorded.

Without x-nk: tests, or with a folder that is not there, or after a failed recording, the run still passes. It runs the specs or sends no requests, and the reports say so (Your tests).

Map each secret your tests read into the env of the step that records.

What you get #

  • A passing run with the report. The run fails when Nikasa could not compare or a step before it failed. With --fail-on-change it also fails when the report lists a change in every new run.
  • The reports. They are the pipeline artifact nk-report, and report.md is the run's summary. The output folder, nk-ci/nk-run, stays on the agent.
  • A comment, if you want one. Nikasa and nk-ci.sh post nothing themselves, and report.md is written to be posted. Add a step of your own that sends it to the pull request through Azure DevOps' API, with a token you keep as a secret variable.
  • Ticket links. The link beside each change in report.md opens a new Azure Boards work item in the pipeline's project and points at the run. 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 #

With the secret key set, nk-ci.sh key works out the baseline's key into nk-ci/nk-baseline.key. The Cache@2 step keeps nk-ci/nk-baseline under that key.

Without a secret key there is no key file, and the compare runs in full. In that case, drop the cache step.

A bucket of your own works instead, through NK_CI_BASELINE_STORE. The cache step is then not needed. See Baseline storage.

Next steps #

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