Running k6 in GitHub Actions

One workflow, one Docker image, automatic on PR.

8 minmedium

A load test you remember to run is a load test you run twice. The value is in running it on every pull request, so the regression is attributed to the change that caused it rather than discovered three weeks later with forty commits to bisect.

The workflow

.github/workflows/loadtest.yml
name: Load test
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
k6:
name: smoke + average load
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_USER: grit
POSTGRES_PASSWORD: grit
POSTGRES_DB: grit
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: apps/api/go.mod
cache-dependency-path: apps/api/go.sum
# What "grit migrate" shells out to, from the same directory.
- name: Migrate
working-directory: apps/api
env:
DB_DRIVER: postgres
DATABASE_URL: postgres://grit:grit@localhost:5432/grit?sslmode=disable
run: go run cmd/migrate/main.go
- name: Start the API
working-directory: apps/api
env:
DB_DRIVER: postgres
DATABASE_URL: postgres://grit:grit@localhost:5432/grit?sslmode=disable
APP_ENV: production
run: |
go run cmd/server/main.go &
for i in $(seq 1 60); do
curl -sf http://localhost:8080/api/health && break
sleep 1
done
- uses: grafana/setup-k6-action@v1
- name: Smoke
run: k6 run tests/k6/smoke.js
- name: Average load
run: k6 run tests/k6/average-load.js

Smoke first, as its own step. When the average load step fails you want the summary to say which one broke, and a green smoke step means the script and the deployment are fine and the finding is real.

The health check loop is not optional

go run returns as soon as the process starts, not when it is serving. Without the loop, k6 begins while the API is still connecting to Postgres and running migrations, and you get a run of connection refused that looks exactly like a system that cannot handle two users.

Poll /api/health, not /health. The unversioned health route is /api/health and the versioned one is /api/v1/health; a bare /health 404s, and curl -sf treats a 404 as a failure, so the loop runs its full sixty seconds and then starts k6 anyway.

Gate on errors, not on latency

This is the part people get wrong, and the project's own CI already says why about benchmarks: timing a shared runner tells you about the runner. A GitHub hosted runner is a noisy neighbour environment, and its p95 varies by more between two identical runs than most real regressions do.

a CI-specific threshold set
// In CI, these two are trustworthy and the latency one is not.
thresholds: {
http_req_failed: ['rate<0.01'],
checks: ['rate>0.99'],
// Kept deliberately loose: it catches an order-of-magnitude regression
// and ignores the runner being busy.
http_req_duration: ['p(95)<2000'],
}

An error rate is a property of your code. A p95 on a shared runner is a property of whoever else is on the machine. Gate the merge on the first, and keep the tight latency threshold for the runs you do on known hardware.

Which tests belong in CI

smoke.js every PR 30 seconds
average-load.js every PR 9 minutes
stress.js nightly 9 minutes, and it is meant to fail things
spike.js before a launch 4 minutes
soak.js before a release 4 hours
breakpoint.js when capacity planning, by hand

Nine minutes on every pull request is a real cost. If that is too much, shorten the hold rather than dropping the test: two minutes at a hundred users still catches an N+1 that was not there yesterday.

Keep the k6 summary as an artifact with --summary-export=summary.json and upload it. A regression is much easier to argue about when you can diff this week's numbers against the merge that caused them.

Try it

One. Add the workflow above to your project and push it on a branch. Confirm both k6 steps run green.

Two. Break it on purpose: add a time.Sleep(3 * time.Second) to a handler the journey hits, and push. Confirm the workflow fails and read which threshold it names.

Three. Remove the sleep. Then delete the health check loop and push. Note what the failure looks like now, and how long it takes to understand it.

Quick check

Your CI load test fails about one run in four on p95, and the failures are not reproducible locally. What is the right fix?

Spot a typo? Have an idea?

Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.

Suggest an improvement on GitHub