k6 run: Execute a Load Test Script in CI
k6 run script.js executes a load test, spinning up the configured virtual users and printing latency and error metrics.
k6 scripts are plain JavaScript with a default exported function. k6 run executes them locally or in CI, with load defined either on the CLI or in the script's options object.
What it does
k6 run loads a script, runs its default function across N virtual users (--vus) for a duration or set of stages, and reports metrics like http_req_duration and http_req_failed. CLI flags override the options exported by the script.
Common usage
k6 run script.js
k6 run --vus 50 --duration 30s script.js
# ramp via stages defined on the CLI
k6 run --stage 30s:50 --stage 1m:50 --stage 30s:0 script.js
# pass data in via env
k6 run -e BASE_URL=https://staging.example.com script.jsOptions
| Flag | What it does |
|---|---|
| --vus <n> | Number of concurrent virtual users |
| --duration <d> | How long to run, e.g. 30s, 5m |
| --stage <d>:<vus> | A ramp stage (repeatable) |
| -i, --iterations <n> | Total iterations instead of a duration |
| -e KEY=VALUE | Set an env var readable via __ENV |
| --quiet | Suppress the progress bar |
In CI
Add --quiet (or --no-color) so logs stay readable, and pass the target URL with -e so the same script runs against staging or production. Pin the k6 version in the workflow; metric output formatting has changed across releases.
Common errors in CI
"ERRO[0000] could not initialize 'script.js': ... SyntaxError" means the script used an unsupported import or syntax (k6 runs on its own JS runtime, not Node). "level=error msg=\"GoError: ... dial tcp: connection refused\"" means the target is unreachable from the runner. k6 exits 0 even with failing requests unless a threshold is set; add thresholds to make failures non-zero.