Running k6 in GitHub Actions
One workflow, one Docker image, automatic on PR.
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
name: Load teston:pull_request:push:branches: [main]permissions:contents: readjobs:k6:name: smoke + average loadruns-on: ubuntu-latestservices:postgres:image: postgres:17env:POSTGRES_USER: gritPOSTGRES_PASSWORD: gritPOSTGRES_DB: gritports: ['5432:5432']options: >---health-cmd pg_isready--health-interval 10s--health-timeout 5s--health-retries 5steps:- uses: actions/checkout@v4- uses: actions/setup-go@v5with:go-version-file: apps/api/go.modcache-dependency-path: apps/api/go.sum# What "grit migrate" shells out to, from the same directory.- name: Migrateworking-directory: apps/apienv:DB_DRIVER: postgresDATABASE_URL: postgres://grit:grit@localhost:5432/grit?sslmode=disablerun: go run cmd/migrate/main.go- name: Start the APIworking-directory: apps/apienv:DB_DRIVER: postgresDATABASE_URL: postgres://grit:grit@localhost:5432/grit?sslmode=disableAPP_ENV: productionrun: |go run cmd/server/main.go &for i in $(seq 1 60); docurl -sf http://localhost:8080/api/health && breaksleep 1done- uses: grafana/setup-k6-action@v1- name: Smokerun: k6 run tests/k6/smoke.js- name: Average loadrun: 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.
// 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 secondsaverage-load.js every PR 9 minutesstress.js nightly 9 minutes, and it is meant to fail thingsspike.js before a launch 4 minutessoak.js before a release 4 hoursbreakpoint.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
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