How To Use GitHub Actions

In this article, we will explore the GitHub actions and workflows and how can we use Github Actions in the Selenium framework.

What are GitHub Actions

Github action is a platform provided by Github to automate various tasks and workflows such as build, test and deployment in the software development lifecycle. GitHub actions are very handy in automating time-consuming, repetitive tedious tasks within the GitHub repository.

Components Of Github Actions

Major components of GitHub actions are explained below:

1)Actions

An action is a predefined or customised script that completes an assigned task or group of tasks. Actions are the building blocks of the automation workflows and are called inside a job. These actions are a reusable unit of codes and useful in the automation of repetitive tasks.

2)Workflows

Workflow can be defined as a set of logically connected actions or tasks available in the GitHub repository. Workflows are defined using YAML files, stored in the ".github/workflows” directory at the root of the repository making them easy to configure and customize. A GitHub repository can have multiple workflows.

Events or triggers are written on the top of workflows.

3)Events

A Github event is anything that may take place in a repository. This includes tasks like submitting code, creating a branch, opening a pull request, and even commenting on an issue. Events are the triggers that start a workflow.

4)Jobs

A job is a unit of work within a workflow. Each job can contain one or more actions. Jobs can run in parallel or sequentially, depending on your workflow requirements. Every workflow must contain at least one job.

5)Runner

Runners are those virtual machines that execute the actions in the workflows. GitHub provides two types of runners.

a) Self-Hosted Runners: Self-hosted runners are customizable, so we can use the environment of our choice like an on-premise server or a cloud provider of our choice. However, maintaining and running these can be costly.

b) GitHub Hosted Runners: GitHub-hosted runners are fully managed and maintained by GitHub but the user has less control over these.

Advantages Of GitHub Actions

There are several advantages of GitHub Actions. Some of them are as follows

  1. No installation is needed.
  2. Multiple Jobs can be executed in parallel with no or minimal effort.
  3. Actions are already available for almost all possible steps in the GitHub marketplace.
  4. GitHub actions are easily sharable with others using the GitHub marketplace.

Understand the GitHub Workflow File Structure

A workflow YAML file contains mainly events, jobs, steps, Runners and actions.

name: Name of the workflow
on: Workflow Trigger [Push,Pull,fork]
jobs: [Collection of steps/actions]
  nameOfTheJob1:
  runs-on: runner [OS, Docker]
  
  steps: 
    stepname:
    uses:
    env: 

name: This is the name of the workflow and is optional.

on: This defines the workflow trigger. The event on which this workflow started execution.

Jobs: jobs are collections of steps that are executed after the workflow is triggered.

runs-on: This defines the runner on which the actions will be performed. We can also define docker containers here if want to execute on docker. Refer to the below sample workflow code for basic understanding.

name: Sample workflow
on:
  push:
    branches:
      - main

jobs:
  print_hello:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Hello World!"

Let’s try to understand the above code.

This workflow will trigger when a user “pushes” any changes in the “main” branch. A job will execute on Ubuntu’s latest version and will echo “Hello World”. Pretty Simple, Right?

GitHub actions for Java and Maven Project

To start with GitHub actions we need a GitHub account and a repository hosted on GitHub. I am using this sample selenium code repository for demo purposes. This repo contains the Selenium Java maven framework. Which is ideal for our purpose.

How to Integrate Selenium Test Suit With Github Actions?

Go to your repository and create a folder “.github/workflows” inside it. Open the folder and create a YAML file, name it “githubactiondemo.yml” and save it.

Add the below code in the YAML file and save it.

name: Demo Code

on:
  workflow_dispatch:
    inputs:
      browser:
        type: choice
        description: Choose browser to test
        required: true
        options:
          - chrome
          - firefox
          - edge

jobs:

  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - uses: browser-actions/setup-chrome@latest
    - run: chrome --version
    
    - name: Build with Maven
      run: mvn -B package --file pom.xml
  test_1:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Test with Maven
      run: mvn clean test
  test_2:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Test with Maven
      run: mvn -B package --file pom.xml test
      env:
          BROWSER: ${{ github.event.inputs.browser }}

In the above code workflow trigger is “workflow_dispatch”. That means the user can manually trigger this workflow. We can add automatic triggers as well like [push, pull, fork] etc. If an automatic trigger applies then the workflow execution starts as soon as an event occurs.

Let’s understand this by an example, suppose you add a trigger on the fork event then if any user forked your repository, the workflow will be triggered automatically.

inputs:
      browser:
        type: choice
        description: Choose browser to test
        required: true
        options:
          - chrome
          - firefox
          - edge

With the above code, we are also allowing users to choose the browser from the available options.

Now Go to the repository and click on actions.

GitHub Actions in Selenium

Under the actions section, your workflow name should appear. In the right section, you can see “Run workflow”. Click on this, a dropdown will open. Choose the browser to open and click Run workflow.

Once the workflow is executed then it displays the screen that shows the execution status, and total time taken to execute the workflow along with the steps of the workflow.

GitHub Actions

So we created a simple CI workflow for our Java Selenium framework. A similar approach can be used to set up other GitHub actions for the Selenium automation framework.

Note: A Repository can contain multiple GitHub workflows. It is recommended to provide an appropriate name for the workflow if we want to create multiple workflows in the Selenium GitHub actions CI/CD pipeline. Although the workflow name is optional, if not provided by the user then a default name will be picked.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading