Building a Load Testing Framework using K6.io — Creating a GitHub Workflow (Part 8)

Building a Load Testing Framework using K6.io — Creating a GitHub Workflow (Part 8)

In Part-07 of this article series we integrated a custom logger to the framework.

In this article, we will create a GitHub Workflow action, which would help us to run the k6 execution on GitHub runners.


Create a workflow:

Let's begin by creating a new file:

.github > workflows > run_with_configFile.yaml

run_with_configFile.yaml

Now fill the file with following code:

name: run_with_configFile
on:
    workflow_dispatch:
      inputs:
        script_name:
          required: true
          type: string
          description: 'Name of the script to execute for ex. test.js:'
        virtual_users:
          required: true
          type: string
          description: 'The no of concurrent threads:'
        run_type:
           type: choice
           description: 'How to run either for a fixed number of execution or for a fixed time duration'
           options: 
           - iterations
           - duration
           required: true
           default: "iterations"
        iterations_or_duration_value:
          required: true
          type: string
          description: 'Iterations or Durations value (for iterations ex. 2, for duration ex. 30m, 1h)'
        config_file_name:
            required: true
            type: string
            description: 'Config file name: DEV.json or QA.json'

jobs:
  manual_run:
    runs-on: ubuntu-latest
    name: Manual Run
    steps:   
    # Checkout code from the repository
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}
    
    # Log Environment details
      - name: User Input Details
        run: |
          echo "virtual users: ${{ github.event.inputs.virtual_users }} from branch ${{ github.ref }}"
          echo "iterations_or_duration: ${{ github.event.inputs.run_type }}"
          echo "iterations_or_duration value: ${{ github.event.inputs.iterations_or_duration_value }}"
          echo "script name: ${{ github.event.inputs.script_name }}"
          echo "script path: code/scripts/${{ github.event.inputs.script_name }}"
          echo "config fileName: ${{ github.event.inputs.config_file_name }}"

    # Setup and Run K6 script
      - name: Setup k6
        uses: grafana/[email protected]
      - name: Run k6 test
        uses: grafana/[email protected]
        env:
          configFile: ${{ github.event.inputs.config_file_name }}
        with:
          path: |
            code/scripts/${{ github.event.inputs.script_name }}
          flags: --vus ${{ github.event.inputs.virtual_users }} --${{ github.event.inputs.run_type }} ${{ github.event.inputs.iterations_or_duration_value }} -e configFile=${{ github.event.inputs.config_file_name }} --out json=reports/result.json --out csv=reports/result.csv
      
    # Upload test artifacts 
      - name: Upload Test Artifacts
        uses: actions/[email protected]
        if: success()
        with:
          name: reports
          path: |
            reports/
            cypress/fixtures/state/*
          retention-days: 30


Workflow Overview

This GitHub Action workflow is named "run_with_configFile" and is triggered manually using the workflow_dispatch event. It allows users to run performance tests using k6 with customizable parameters.

Workflow Inputs

The workflow accepts several inputs:

  1. script_name: The name of the script to execute (e.g., test.js)
  2. virtual_users: The number of concurrent threads for the test
  3. run_type: How to run the test (either for a fixed number of iterations or a fixed time duration)
  4. iterations_or_duration_value: The value for iterations or duration
  5. config_file_name: The configuration file name (e.g., DEV.json or QA.json)

Job Configuration

The workflow defines a single job named "manual_run" that runs on a custom runner labeled "testing".

Job Steps

1. Checkout

Uses actions/checkout@v4 to fetch the code from the repository.

2. User Input Details

Logs the user-provided input details for debugging purposes.

3. Setup k6

Uses grafana/[email protected] to set up the k6 testing tool.

4. Run k6 test

Uses grafana/[email protected] to run the k6 test with the following configurations:

  • Specifies the script path
  • Sets the number of virtual users
  • Configures the run type (iterations or duration) and its value
  • Passes the config file name as an environment variable
  • Outputs results in JSON and CSV formats

5. Upload Test Artifacts

Uses actions/[email protected] to upload the test results (JSON and CSV files) and any state files from Cypress fixtures. The artifacts are retained for 30 days.

Key Features

  1. Flexibility: Allows users to specify various test parameters without modifying the workflow file.
  2. Configuration File Support: Enables the use of different configuration files for different environments.
  3. Custom Runner: Uses a specific runner labeled "testing", which might be configured for performance testing.
  4. Artifact Preservation: Saves test results and related files for later analysis.

This workflow provides a convenient way to run performance tests with customizable parameters, making it easier to test different scenarios and environments without changing the workflow definition.

Running the Workflow:

Run Results:



Ciao!


Note:

There is an open bug which prevents variables from config.json to read as environment variable in the run-k6-action

https://github.com/grafana/run-k6-action/issues/28


GitHub repo:

https://github.com/far11ven/k6-LoadTestingFramework/tree/main/Part%2008?ref=kushalbhalaik.xyz