Exercise : 8 Create a dynamic grid on the fly - Raneesh02/ref_workshop_2 GitHub Wiki

GitHub Actions: Install Helm, kubectl, and Configure AWS

This GitHub Actions workflow automates the deployment of a Helm chart to a Kubernetes cluster on AWS EKS and runs a Maven test suite. It is triggered on pull requests to the main branch.

Prerequisites

  • You have access to run/create github actions
  • You have access to AWS credentials (Access Key ID and Secret Access Key).

Step 1: Create a workflow yml

Create a new actions and in the yaml include

name: Deploy to Kubernetes

'on':
  pull_request:
    branches: [ "main" ]

jobs:
  deploy:
    runs-on: ubuntu-latest

    permissions:
      statuses: write
      checks: write
      contents: write
      pull-requests: write
      actions: write

    env:
      # Define a dynamic release name using GitHub Actions run ID for uniqueness
      RELEASE_NAME: selenium-grid-${{ github.run_id }}

    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 22
        uses: actions/setup-java@v4
        with:
          java-version: '22'
          distribution: 'temurin'
          cache: maven
      - name: Install kubectl
        run: >
          curl -LO "https://dl.k8s.io/release/$(curl -L -s
          https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
          chmod +x ./kubectl
          sudo mv ./kubectl /usr/local/bin/kubectl
      - name: Verify kubectl version
        run: kubectl version --client
      - name: Install Helm
        run: >
          curl
          https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 |
          bash
      - name: Verify Helm version
        run: helm version
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v3
        with:
          aws-access-key-id: '${{ secrets.AWS_ACCESS_KEY_ID }}'
          aws-secret-access-key: '${{ secrets.AWS_SECRET_ACCESS_KEY }}'
          aws-region: ap-south-1
      - name: Check aws config done
        run: |
          aws sts get-caller-identity
      - name: Set up kubeconfig for EKS
        run: |
          aws eks update-kubeconfig --region ap-south-1 --name selenium-grid
      - name: Deploy with kubectl
        run: |
          kubectl get pods
      - name: Install Helm Chart
        run: |
         cd helm/selenium-helm
         helm install ${{ env.RELEASE_NAME }} selenium-grid --wait
      - name: Test Deployment
        run: kubectl get all

      - name: Get Service URL
        id: get_service_url
        run: |
          SERVICE_URL=$(kubectl get svc selenium-hub-service -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
          echo "SERVICE_URL=${SERVICE_URL}" >> $GITHUB_ENV
          echo "Service URL is ${{ env.SERVICE_URL }}"
      - name: Wait for selenium grid to be ready
        run: |
          ls
          chmod +x scripts/waitforseleniumhub.sh
          scripts/waitforseleniumhub.sh ${{ env.SERVICE_URL }}  
      - name: test with maven
        run: mvn clean test -Dselenium.gridurl=${{ env.SERVICE_URL }}
      - name: Report test results
        uses: dorny/[email protected]
        with:
          name: Maven Surefire
          path: "**/surefire-reports/TEST-*.xml"
          reporter: java-junit

      - name: Cleanup Helm Chart
        if: always()
        run: helm uninstall ${{ env.RELEASE_NAME }}
        continue-on-error: true  # This step will not fail the job

Workflow Overview

  • Name: Deploy to Kubernetes
  • Trigger: Pull requests to the main branch

Jobs

Deploy

Runs on: ubuntu-latest

Permissions:

  • statuses: write
  • checks: write
  • contents: write
  • pull-requests: write
  • actions: write

Environment Variables:

  • RELEASE_NAME: A dynamic release name using GitHub Actions run ID for uniqueness, formatted as selenium-grid-${{ github.run_id }}.

Steps:

  1. Checkout the Repository

    • Uses: actions/checkout@v4
    • Checks out the code from the repository.
  2. Set up JDK 22

    • Uses: actions/setup-java@v4
    • Sets up JDK 22 with Maven caching.
  3. Install kubectl

    • Runs a command to download and install the kubectl binary.
  4. Verify kubectl Version

    • Runs kubectl version --client to verify the installation.
  5. Install Helm

    • Runs a command to install Helm, the package manager for Kubernetes.
  6. Verify Helm Version

    • Runs helm version to verify the Helm installation.
  7. Configure AWS Credentials

    • Uses: aws-actions/configure-aws-credentials@v3
    • Configures AWS credentials for accessing AWS services.
  8. Check AWS Config

    • Runs aws sts get-caller-identity to verify AWS credentials.
  9. Set up kubeconfig for EKS

    • Runs aws eks update-kubeconfig --region ap-south-1 --name selenium-grid to configure kubectl to use the EKS cluster.
  10. Deploy with kubectl

    • Runs kubectl get pods to check the status of pods.
  11. Install Helm Chart

    • Changes directory to helm/selenium-helm and runs helm install ${{ env.RELEASE_NAME }} selenium-grid --wait to deploy the Helm chart.
  12. Test Deployment

    • Runs kubectl get all to verify that all Kubernetes resources are deployed correctly.
  13. Get Service URL

    • Extracts the service URL from the Kubernetes service and sets it as an environment variable. The URL is also printed to the logs.
  14. Wait for Selenium Grid to be Ready

    • Runs a script to wait for the Selenium Grid service to be ready using the obtained service URL.
  15. Test with Maven

    • Runs Maven tests with mvn clean test, passing the Selenium Grid URL as a system property.
  16. Report Test Results

  17. Cleanup Helm Chart

    • Uninstalls the Helm chart using helm uninstall ${{ env.RELEASE_NAME }}. This step always runs regardless of the success or failure of previous steps, and will not fail the job even if errors occur.

Usage

  1. Ensure you have the necessary secrets (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) configured in your GitHub repository.
  2. Customize the Helm chart directory path and service names as needed.
  3. The workflow will automatically execute on pull requests to the main branch, performing deployment, testing, and cleanup.

Step 2: Create a script to wait for Selenium Hub to be ready to take requests

This script checks the readiness of a Selenium Grid hub by repeatedly querying its status endpoint until it becomes available or the timeout is reached. It is designed to be used in automated workflows to ensure that the Selenium Grid hub is up and running before proceeding with further actions.

Script Overview

  • Filename: waitforseleniumhub.sh
  • Purpose: Check if the Selenium Grid hub is ready by polling its status endpoint.
#!/bin/bash

# Ensure a URL is provided
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <selenium-grid-url>"
  exit 1
fi

echo "Check for selenium hub status!"

SELENIUM_URL=$1
RETRY_INTERVAL=5
TIMEOUT=120
START_TIME=$(date +%s)

while true; do
  echo "http://$SELENIUM_URL:4444/wd/hub/status"
  STATUS=$(curl -s -o /dev/null -w "%{http_code}" "http://$SELENIUM_URL:4444/wd/hub/status")
  echo "Status $STATUS"

  if [ "$STATUS" -eq 200 ]; then
    echo "Selenium Hub is ready!"
    exit 0
  fi

  CURRENT_TIME=$(date +%s)
  ELAPSED_TIME=$((CURRENT_TIME - START_TIME))

  if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
    echo "Timed out waiting for Selenium Hub to be ready."
    exit 1
  fi

  echo "Selenium Hub not ready yet. Retrying in $RETRY_INTERVAL seconds..."
  sleep $RETRY_INTERVAL
done

Usage

./check_selenium_hub.sh <selenium-grid-url>

Step 3 : Push the changes and create a new PR

Notice for the pr raised the Github actions should first do all the required installations and then go on to install a new Selenium Grid Chart. Once selenium grid hub is ready. execution of tests should start

image

Note : Please make sure to delete eks cluster and check cost explorer, Details to delete eks cluster is present in exercise : 6

Congratulations!!! now you have been able to use

  • Github Actions : To Start the trigger for test cases
  • Selenium-Grid : To allow handling of multiple requests and session from tests to allow parallel runs
  • Docker and Docker Compose : To understand containers and their working to deploy selenium grid as hub and node for local debugging
  • Kubernetes : to handle different configs, containers , nodes to make sure selenium-grid is deployed with nodes running properly
  • Helm Charts : to deploy applications in kubernetes cluster with ease
  • AWS EKS : To start kubernetes cluster on the cloud and deploy selenium-grid
  • AWS CloudShell : to get terminal access for aws resources
  • Scripting in terminal to automate tasks like installing chrome or waiting for selenium hub