k6_Integration with GitHub Actions_Grafana_Reporting_Publish Results
Kumar Jitendra
|Full Stack QA|Australian PR|Automation Specialist|SDET|Open source tool Advocate|WebDriverIO|Serenity BDD|Cypress |Financial Crime|Kafka Automation|Python |TypeScript|Playwright|JavaScript|k6 PerformanceTest|
Grafana k6 + GitHub Actions
Writing the first Performance Script :-
Thresholds are a powerful feature providing a flexible API to define various types of Pass/Fail criteria in the same test run. For example:
The 99th percentile response time must be below 700 ms.
The 95th percentile response time must be below 400 ms.
No more than 1% failed requests.
The content of a response must be correct more than 95% of the time.
Setting up the GitHub Actions workflow
We can define the Git Branch Name - and the Pull Request - Branch name - for which actions which we wanted to trigger the workflow . We can also define Cron Expressions inside the on Schedule - You can use on.schedule to define a time schedule for your workflows. You can schedule a workflow to run at specific UTC times using POSIX cron syntax . Scheduled workflows run on the latest commit on the default or base branch. The shortest interval you can run scheduled workflows is once every 5 minutes.
This example triggers the workflow every day at 5:30 and 17:30 UTC.
k6 has official GitHub actions to make it easy to execute your load tests. They are divided into two parts:
You can use the following options with this action to modify its behavior:
The following action inputs can be used along with the action:
Running cloud tests
There are two common execution modes to run k6 tests as part of the CI process.
You can also run k6 tests locally on the CI server, and then push the results to Grafana Cloud k6.
Now, we will show how to run cloud tests using GitHub Actions. If you do not have an account with Grafana Cloud already, you can sign up for a free one today.
After that, get your account token and add it to your GitHub project’s Secrets page by going to Settings > Security > Secrets and Variables > Actions > Secrets.
We also need to specify the project ID where the test run should be stored. To do this, grab your project ID and store it as a GitHub action secret or define it directly in the workflow.
We will pass the token and project ID to the k6 action via environment variables.
领英推荐
Pass the k6_Cloud_Token which we will generate from Grafana Labs and after placing it inside the GitHub Actions -> Secrets - pass it inside the GitHub Actions workflow yml file :-
The only change in the workflow compared to running test locally and pushing results to the cloud is to set cloud-run-locally for the action to false. This will now execute the test on Grafana Cloud k6.
Attaching the full version of k6_GitHubActions -workflow yml file -
name: k6 Cloud Load Test
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 5,17 * * *'
jobs:
run-cloud-test:
runs-on: ubuntu-latest
env:
K6_CLOUD_TOKEN: ${{ secrets.K6_CLOUD_TOKEN }}
K6_CLOUD_PROJECT_ID: ${{ secrets.K6_CLOUD_PROJECT_ID }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup K6
uses: grafana/setup-k6-action@v1
- name: Run local k6 test
uses: grafana/run-k6-action@v1
with:
path: |
./specs_folder/k6_Cloud_Demo.js
./specs_folder/Threshold_example_test.js
#flags: --vus 10 --duration 20s # optional: flags to pass to to each k6 test (default: none)
parallel: true # optional: run tests in parallel (default: false)
fail-fast: false # optional: fail the step early if any test fails (default: true)
cloud-run-locally: false
cloud-comment-on-pr: true
The following link contains all the steps to trigger the Git Hub Actions Workflow - and all the steps defined :-
Trigger the Workflow :-
Conclusion :-
Reference Links :-
Grafana k6_Actions - https://github.com/marketplace/actions/run-grafana-k6-tests
Grafana_k6_GItHub_Actions_Document - https://grafana.com/blog/2024/07/15/performance-testing-with-grafana-k6-and-github-actions/
GitHub Actions Syntax - https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
Git Hub Repository - https://github.com/log2jiten24/k6_Performance_Test_Jiten
Git Hub_Secrets_Manage Document - https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions