Commit 1f7174f4 authored by Pierre SMEYERS's avatar Pierre SMEYERS
Browse files

Merge branch 'initial-k6' into 'master'

feat: add k6 template

See merge request to-be-continuous/k6!1
parents 19b1dc55 4a38e3ec
Loading
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+22 −0
Original line number Diff line number Diff line
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
 No newline at end of file

.gitlab-ci.yml

0 → 100644
+45 −0
Original line number Diff line number Diff line
include:
  - project: "to-be-continuous/tools/gitlab-ci"
    ref: "master"
    file: "/templates/validation.yml"
  - project: "to-be-continuous/kicker"
    ref: "master"
    file: "/templates/validation.yml"
  - project: "to-be-continuous/bash"
    ref: "1.0.0"
    file: "templates/gitlab-ci-bash.yml"

stages:
  - build
  - publish

variables:
  GITLAB_CI_FILES: "templates/gitlab-ci-k6.yml"
  BASH_SHELLCHECK_FILES: "*.sh"

# extract the Bash script from template (for ShellCheck job)
extract-script:
  stage: .pre
  script:
    - echo "#!/bin/bash" > script.sh
    - sed -n '/BEGSCRIPT/,/ENDSCRIPT/p' "$GITLAB_CI_FILES" | sed 's/^  //' >> script.sh
    - export LC_ALL=C.UTF-8
  artifacts:
    when: always
    name: extracted template script
    expire_in: 1h
    paths:
      - script.sh

release:
  image: node:12
  stage: publish
  before_script:
    - npm install -g semantic-release @semantic-release/gitlab @semantic-release/exec @semantic-release/git
  script:
    - semantic-release
  only:
    refs:
      - master
    variables:
      - $TMPL_RELEASE_ENABLED

.releaserc.yml

0 → 100644
+20 −0
Original line number Diff line number Diff line
plugins: [
  "@semantic-release/commit-analyzer",
  "@semantic-release/release-notes-generator",
  "@semantic-release/gitlab",
  [
    "@semantic-release/exec",
    {
      "prepareCmd": "./bumpversion.sh \"${lastRelease.version}\" \"${nextRelease.version}\" \"${nextRelease.type}\""
    }
  ],
  [
    "@semantic-release/git",
    {
      "assets": ["*.md", "templates/*.yml"]
    }
  ]
]
branches:
  - "master"
tagFormat: "${version}"
 No newline at end of file
+67 −4
Original line number Diff line number Diff line
# GitLab CI template Skeleton
# GitLab CI template for k6 loading test

This is a skeleton project for starting a new _to be continuous_ template.
This project implements a generic GitLab CI template for loading test using [k6 loading test](https://k6.io/).

You shall fork it when you want to start developing a new template.
It provides several features, usable in different modes (by configuration).

Based on the kind of template (build, analyse, hosting, acceptance, ...), you should start working from one of the available `initial-xxx` branches, that each implement basic stuff.
## Usage

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

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

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

```

:warning: depending on your needs and environment, you might have to use [one of the template variants](#variants).

## `k6` job

This job starts [k6](https://k6.io/) 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](https://k6.io/docs/getting-started/running-k6)                                   | _none_                 |
| `REVIEW_ENABLED` | Set 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](https://docs.gitlab.com/ee/user/project/merge_requests/load_performance_testing.html).

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](https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#artifactsreportsdotenv) variable `$environment_url`
or through a basic `environment_url.txt` file, then the k6 test will automatically be run on this server.

:warning: 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:

```javascript
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);
}
```

bumpversion.sh

0 → 100755
+41 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

function log_info() {
  >&2 echo -e "[\\e[1;94mINFO\\e[0m] $*"
}

function log_warn() {
  >&2 echo -e "[\\e[1;93mWARN\\e[0m] $*"
}

function log_error() {
  >&2 echo -e "[\\e[1;91mERROR\\e[0m] $*"
}

# check number of arguments
if [[ "$#" -le 2 ]]; then
  log_error "Missing arguments"
  log_error "Usage: $0 <current version> <next version>"
  exit 1
fi

curVer=$1
nextVer=$2
relType=$3

if [[ "$curVer" ]]; then
  log_info "Bump version from \\e[33;1m${curVer}\\e[0m to \\e[33;1m${nextVer}\\e[0m (release type: $relType)..."

  # replace in README
  sed -e "s/ref: '$curVer'/ref: '$nextVer'/" README.md > README.md.next
  mv -f README.md.next README.md

  # replace in template and variants
  for tmpl in templates/*.yml
  do
    sed -e "s/\"$curVer\"/\"$nextVer\"/" "$tmpl" > "$tmpl.next"
    mv -f "$tmpl.next" "$tmpl"
  done
else
  log_info "Bump version to \\e[33;1m${nextVer}\\e[0m (release type: $relType): this is the first release (skip)..."
fi
Loading