Github Actions

In this guide, we'll explore how to set up a GitHub Actions workflow that triggers a test using the Niteco Performance Insights REST API. We'll focus on the /v1/tests/runtest endpoint.

Step 1: Create the GitHub Actions Workflow

In the root of your repository, create a directory called .github/workflows if it doesn't exist. Inside this directory, create a new file called run-tests.yml.

Edit the Workflow File:

Open the run-tests.yml file in your preferred text editor and add the following code:

name: Run Tests

on:
  push:
    branches:
      - main  # Change to your target branch

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Run Tests
        env:
          API_URL: https://perf.niteco.com/api/v1/tests/runtest
          PAGE_ID: your_page_id         # Replace with your actual page ID
          DEVICE: DESKTOP               # Adjust as necessary
          LOCATION: your_location       # Replace with your actual location
          FORM_FACTOR: DESKTOP          # Can be DESKTOP, MOBILE, etc.
        run: |
          RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null \
            -G "${API_URL}" \
            --data-urlencode "page_id=${PAGE_ID}" \
            --data-urlencode "device=${DEVICE}" \
            --data-urlencode "location=${LOCATION}" \
            --data-urlencode "formFactor=${FORM_FACTOR}")

          if [ "$RESPONSE" -eq 200 ]; then
              echo "Tests ran successfully!"
          else
              echo "Tests failed with status code: $RESPONSE"
              exit 1  # Fail the job if tests fail
          fi

Make sure you configure your test variables above:

PAGE_ID: Set this to the ID of the page you want to test.
DEVICE, LOCATION, FORM_FACTOR: Modify these values according to your testing requirements.

Make sure to give this script executable permissions:

chmod +x test-script.sh

Step 2: Create a GitLab CI/CD Configuration

Create a .gitlab-ci.yml:

Create a .gitlab-ci.yml file in the root of your repository. This file will define the CI/CD pipeline.:

#!/bin/sh

# Variables for API request
API_URL="https://perf.niteco.com/api/v1/tests/runtest"
PAGE_ID="your_page_id"         # Replace with your actual page ID
DEVICE="DESKTOP"               # Adjust as necessary
LOCATION="your_location"       # Replace with your actual location
FORM_FACTOR="DESKTOP"          # Can be DESKTOP, MOBILE, etc.

# Make the API request
RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null \
    -G "${API_URL}" \
    --data-urlencode "page_id=${PAGE_ID}" \
    --data-urlencode "device=${DEVICE}" \
    --data-urlencode "location=${LOCATION}" \
    --data-urlencode "formFactor=${FORM_FACTOR}")

# Check the response status
if [ "$RESPONSE" -eq 200 ]; then
    echo "Tests ran successfully!"
else
    echo "Tests failed with status code: $RESPONSE"
    exit 1  # Prevent push if tests fail
fi

Make sure you configure your test variables above:

PAGE_ID: Set this to the ID of the page you want to test.
DEVICE, LOCATION, FORM_FACTOR: Modify these values according to your testing requirements.


Step 2: Push Your Changes

Commit and push your changes to your GitLab repository. The pipeline will automatically trigger based on your configuration.

Make a Commit:

Create a new commit in your repository and push it.

git add .github/workflows/run-tests.yml
git commit -m "Add GitHub Actions workflow for running tests"
git push origin mainh

Step3: Monitor Workflow Results

After pushing, to the "Actions" tab in your GitHub repository and check the "CI/CD" section. Click on the most recent workflow run to see the details and logs. You should see the pipeline running. If the test passes, you’ll see a success message; if it fails, you’ll receive an error.