Hi folks,
In this blog, we will learn about K6, GitHub Actions and how to integrate k6 with GitHub Actions.
What is K6?
K6 is a load-testing tool for APIs that is open-source, free, and focused on developers. By testing your systems’ dependability and performance using K6, you may identify performance regressions and issues early. When writing their tests, users may establish goals using Thresholds in K6’s goal-oriented testing paradigm.
What is GitHub Actions?
A CI/CD platform called GitHub Actions enables you to automate your workflow for building, testing, and deploying software. We design processes in GitHub Actions that deploy merged pull requests to production or build and test each pull request you submit to your repository.
Writing a Test Script in K6:
Add this script to your file in k6 and save the file as demo.js.
import { check } from 'k6';
import http from 'k6/http';
import { Trend } from 'k6/metrics';
import { htmlReport } from 'https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js';
import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';
const baseUrl = 'https://reqres.in';
const createUserAPITrend = new Trend('POST /createUser Create User API ');
export let options = {
stages: [
{ duration: '1m', target: 50 },
{ duration: '2m', target: 100 },
{ duration: '2m', target: 0 },
],
thresholds: {
'http_req_failed': ['rate<0.10'],
'http_req_duration': ['p(95)<1000'], // 95 percent of response times must be below 1000ms
'http_req_duration{status:200}': ['max>=0'],
'http_req_duration{status:201}': ['max>=0'],
'http_req_duration{status:400}': ['max>=0'],
'http_req_duration{status:500}': ['max>=0'],
},
'summaryTrendStats': ['min', 'med', 'avg', 'p(90)', 'p(95)', 'max', 'count'],
};
export default function goRestAPI() {
let createPayload = JSON.stringify({
name: 'Test user',
job: 'leader'
});
// Create cart
const createUser_response = httpPost(
`${baseUrl}/api/users`,
createPayload,
201
);
createUserAPITrend.add(createUser_response.timings.duration);
check(createUser_response, {
'Create user response success ': (r) => r.status === 201,
'Create user response has user id ': (r) => r.json().id != undefined,
});
}
function httpPost(url, payload, params, expectedResponseCode) {
var res;
for (var retries = 2; retries > 0; retries--) {
res = http.post(url, payload, params);
if (res.status == expectedResponseCode) {
return res;
}
}
return res;
}
Integrate k6 with GitHub Actions:
To integrate k6 with GitHub Actions and execute our load test, first, we need to create a workflow and place it in .github/workflows. Once this file has been pushed to our repository, the workflow will begin to execute after each commit to our repository.
name: Cypress GitHub Actions
on: [push]
jobs:
k6_load_test:
name: k6 Load Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Run local k6 test
uses: grafana/k6-action@v0.2.0
with:
filename: demo.js
- name: Store performance test results
uses: actions/upload-artifact@v3
with:
name: k6-report
path: result.html
Report Generation in K6:
K6 is also capable of reporting an overall summary of the test findings.To accomplish this, we will need to export a handleSummary function as demonstrated in the code snippet below:
export function handleSummary(data) {
return {
'result.html': htmlReport(data),
stdout: textSummary(data, { indent: ' ', enableColors: true }),
};
}
In the handleSummary function, we have specified the result.html file to store the results. An illustration of a GitHub workflow showing how to post the summary findings to GitHub is provided below:
- name: Store performance test results
uses: actions/upload-artifact@v3
with:
name: k6-report
path: results.json
How to manage your workflow run in GitHub Actions?
The process becomes active when a push or pull request is submitted using code from the repository, and the most recent build runs of GitHub commits in PR are immediately connected.
Let’s learn more about the workflow before we manage it.

With a single click on the selected workflow, we can access the whole build run and the detailed view of anyone created, as shown in the image above.

In the above image, we have k6 Load Test, the job name, and as you can see, all of the stages beneath the jobs are passed, indicating that the workflow build is successful. If any of the stages in the given jobs fail, the workflow fails as well.

In the above image, we have k6-report, the report name. By downloading this artifact, we can view the report and details of the recently built run.