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
thresholdsblock to the k6 scriptoptions. - Run the script with the official
grafana/setup-k6-actionor 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.jsGotchas
- Add
abortOnFail: trueinside a threshold to stop the test early when it breaches. - Run load tests against staging, not production, to avoid skewing real traffic.
Related guides
How to Run Artillery Load Tests in GitHub ActionsRun an Artillery load test in GitHub Actions and fail the build on a breached SLO using ensure thresholds on…
How to Run a Performance Smoke Test After Deploy in GitHub ActionsRun a quick performance smoke test against a freshly deployed URL in GitHub Actions, failing the deploy job w…