Skip to content
Latchkey

How to Gate a PR on k6 Load Test Thresholds in GitHub Actions

k6 exits with a non-zero code when a thresholds rule fails, which turns a load test into a hard CI gate.

Declare thresholds in the k6 script options (for example http_req_duration: p(95) < 500). When the run breaches a rule, k6 returns exit code 99 and the step fails.

Steps

  • Add a thresholds block to the k6 script options.
  • Run the script with the official grafana/setup-k6-action or the k6 binary.
  • A breached threshold returns a non-zero exit and fails the job.

k6 script

load-test.js
import http from 'k6/http';
export const options = {
  vus: 20,
  duration: '30s',
  thresholds: {
    http_req_duration: ['p(95)<500'],
    http_req_failed: ['rate<0.01'],
  },
};
export default function () {
  http.get('https://staging.example.com/');
}

Workflow

.github/workflows/ci.yml
jobs:
  k6:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: grafana/setup-k6-action@v1
      - run: k6 run load-test.js

Gotchas

  • Add abortOnFail: true inside a threshold to stop the test early when it breaches.
  • Run load tests against staging, not production, to avoid skewing real traffic.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →