Skip to content

GitLab CI template for k6 loading test

This project implements a generic GitLab CI template for loading test using k6 loading test.

It provides several features, usable in different modes (by configuration).

Usage

In order to include this template in your project, add the following to your gitlab-ci.yml:

include:
  - project: 'to-be-continuous/k6'
    ref: '2.0.1'
    file: '/templates/gitlab-ci-k6.yml'

# Pipeline steps
stages:
  - acceptance # required by Cypress template
  # TODO: add all other required stages

⚠️ depending on your needs and environment, you might have to use one of the template variants.

k6 job

This job starts k6 tests.

It uses the following variables:

Name description default value
K6_IMAGE The Docker image used to run k6 loadimpact/k6:latest
K6_TESTS_DIR The k6 tests directory k6
K6_EXTRA_ARGS k6 extra command-line none
REVIEW_ENABLED Set to true to enable k6 tests on review environments (dynamic environments instantiated on development branches) none (disabled)

Load performance report integration

k6 test reports are integrated to GitLab by generating load performance reports.

This is done using the following CLI options: --out json=reports/

Base URL auto evaluation

By default, the k6 template tries to auto-evaluate the base URL (i.e. the variable pointing at server under test) by looking either for a $environment_url variable or for an environment_url.txt file.

Therefore if an upstream job in the pipeline deployed your code to a server and propagated the deployed server url, either through a dotenv variable $environment_url or through a basic environment_url.txt file, then the k6 test will automatically be run on this server.

⚠️ all our deployment templates implement this design. Therefore even purely dynamic environments (such as review environments) will automatically be propagated to your k6 tests.

In order to use the auto-evaluated base URL, you shall use the base_url environment variable from your k6 scripts.

Example:

import http from 'k6/http';
import {check, sleep} from 'k6';

export default function() {
  const data = {username: 'username', password: 'password'};
  let res = http.post(__ENV.base_url, data);
    check(res, { 'success login': (r) => r.status === 200 });
    sleep(0.3);
}

Gitlab compatibility

ℹ️ This template is actually tested and validated on GitLab Community Edition instance version 13.12.11