Utilizing GitHub Actions for Continuous Integration

Utilizing GitHub Actions for Continuous Integration

Continuous Integration and Delivery (CI/CD) has undergone radical changes in recent years. It ensures the efficient and frequent delivery of products through automated builds, testing, and deployment.

As automation testers, we must understand how to configure CI/CD pipelines. This capability allows us to conduct tests more frequently without imposing additional manual work on developers or operators.

While various CI/CD tools, like Jenkins, are commonly used by automation testers, this blog will specifically focus on GitHub Actions. Nowadays, many engineers use GitHub for version control and collaboration, making GitHub Actions the preferred choice for automation testers and developers when implementing CI/CD.

In this blog, I will cover the following topics:

  1. What is GitHub Actions?

  2. The components of GitHub Actions

  3. How to set up GitHub Actions in a GitHub repository

  4. How to view GitHub Actions activity

What is GitHub Actions:

GitHub Actions is a continuous integration (build, test, merge) and continuous delivery(automatically release to the repository) (CI/CD) platform. It automates and executes the software development workflows right from GitHub. It helps to customize how actions work and automate tasks all along in the software development lifecycle.

With GitHub Actions, you can build, test, and publish across multiple platforms, operating systems, and languages all within the same workflow and then see the status checks displayed within the pull request.

GitHub Actions works with various languages and frameworks, and its configurations are in YAML. This means you can easily edit, reuse, share, and copy them like regular code.

You’ll find a wide variety of ready-made templates on the GitHub marketplace, saving you time and preventing the need to rewrite existing code. GitHub Actions are triggered by events and can execute a series of commands when a specific event occurs.

One example of GitHub Actions is :

If you’ve configured GitHub Actions in your Git repository and you push code to the remote branch, the GitHub Action will automatically trigger the predefined workflow.

It supports various events such as opening pull requests, merging code, or pushing to a branch, making it a powerful and flexible tool for different scenarios. GitHub Actions, being event-driven, responds to these events by executing the specified workflows.

GithubActions uses .yml syntax to define the events, jobs, and steps.

Components Of GitHub Actions:

Components of GitHub Actions include workflows, events, and actions. These components enable the creation of powerful automation pipelines for building, testing, and deploying projects on GitHub.

Below is a detailed explanation of the components of GitHub Actions:

a. Events :

Events is an activity that triggers a workflow to run. Events can be triggered by opening a pull request, merging a pull request, etc. It can be configured to run on schedule.

b. Workflows (Jobs, Runner, Steps) :

Job is a set of steps in a workflow that execute on the same runner

Runner is a server that runs your workflows when they’re triggered. Each runner can run a single job at a time.

Steps means actions to perform/control the order in which actions are run

c. Actions:

It is a custom application/command for the GitHub Actions platform that performs a complex but frequently repeated task that automates the workflow process.

How to set up GitHub action in GitHub repository:

Pre-Requisite: One needs to have a GitHub repository, to create and run a GitHub Actions workflow. GitHub Actions uses YAML syntax to define the workflow.

In the below section, I have explained how one can add Github Action to the GitHub repository. I have taken an example of my Github repository which already has some code pushed in to master branch.

In the GitHub repository, one needs to create.github/workflows/directory and add a .yml file.

There are two ways to create a *.yml file

  1. Through Web Application (Github) directly

a. Go to repository

b. Click on Add File > Create New File

c. Enter .github/workflows/main.yml ( It has to be in the same sequence. You can pass any name of your choice for .ym file)

d. Now, you can add the code to the main.yml file and commit changes to your GitHub repository

Sample Code :

COPY

# This workflow will build a Java project with Maven and cache/restore any dependencies to improve the workflow execution time
name: GitHub Actions Demo Test Execution

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  schedule:
    - cron: '*/15 * * * *'

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
          cache: maven
      - name: Build with Maven
        run: mvn clean install

2. Through Terminal:

a. Clone your GitHub repository into your system (git clone [Githubrepo])

b. Navigate to your GitHub repo (cd githubactions). Create a directory .github (command: mkdir .github)

\github actions is the repository name used here*

c. Go to .github (cd .github)

d. Create a folder workflow (command: mkdir workflows)

e. Go to workflows (cd workflows)

f. Create *.yml file (command: vi main.yml). You can give any name. I have created a file as main.yml

COPY

# This workflow will build a Java project with Maven and cache/restore any dependencies to improve the workflow execution time
name: GitHub Actions Demo Test Execution

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  schedule:
    - cron: '*/15 * * * *'

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
          cache: maven
      - name: Build with Maven
        run: mvn clean install

How to view GitHub action activity:

Once the GitHub actions are configured and start running, you can view each step’s activity on GitHub.

  1. Go to your repository on Github.com

  2. Under the repository name, click on the Actions tab

3. Select the workflow you want to see from the left sidebar.

4. Under “Workflow runs”, click the name of the run you want to see.

5. One can view the result of each step by clicking on the workflow

I appreciate you spending the time to read my blog! 📝

Let's communicate again:

  • Reach out to me on LinkedIn 🔗

  • For additional updates and insights, follow to my blog channel on Hashnode