Skip to content
Go back

Test Automation - How To Create an Independent Automation Infrastructure using GitHub Actions

Published:

Introduction

In this article, I will explain how to build an independent automation infrastructure using GitHub actions.

This article uses Java to describe the architecture process on other coding languages module and project might have different meanings, for example, on C# module equals to project and project equals to a solution.

Let’s explore three approaches to creating an automation infrastructure:

Infrastructure And Tests Are In The Same Module In The Same Project

This is the most simple and straightforward approach, it basically means there is no separation of concerns between our automated tests and our automation infrastructure. The division might be to different packages or classes or it might not be at all. This fits for small automation projects that focus only on a single domain (e.g only UI or Mobile) and only a single platform (e.g a single entry point to the application under test).

PROS:

CONS:

When more requirements for tests arise, we will probably need to go with the second approach:

Infrastructure And Tests Are In The Different Modules In The Same Project

In the following approach, our automation infrastructure lives in its unique module. Other modules consume it and might represent domains we want to test (e.g Web/Mobile/API) or multiple applications under test. This approach is best suited when the automation serves a single team of engineers.

PROS:

CONS:

When dealing with a huge and diverse team of automation engineers with different coding skills and a vast number of requirements from the automation infrastructure, we might want to go with the third approach:

Infrastructure And Tests Are In Different Project

In this approach, our automation infrastructure is hosted in its own repository. Multiple automation projects consume it. Every change can affect the consumers - so the infrastructure itself becomes a product of the testing realm. The infrastructure is probably written by infrastructure automation developers/automation architects who only develop and maintain the infrastructure and don’t write test cases.

PROS:

CONS:

After covering the theoretical aspects, let’s dive into the technical details of implementing the last approach:

The technological stack used to implement the solution is:

Programing language: Java

Build tool: Maven

link to the project.

What are GitHub Actions?

according to this article:

GitHub Actions is an API for cause and effect on GitHub: orchestrate any workflow, based on any event, while GitHub manages the execution, provides rich feedback, and secures every step along the way. With GitHub Actions, workflows and steps are just code in a repository, so you can create, share, reuse, and fork your software development practices.

GitHub actions are free and support public repositories. the article will be demonstrated using my public repository, which contains a single static method that is just saying hello :)

Implementing the Solution

We need to create a .github folder into the root of our project, inside it create a workflow folder. Inside it, we create our workflows, which are YAML files.

No alt text provided for this image

This is the workflow content:

name: Publish package to GitHub Packages

on:
  release:
    types: [created]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Publish package
        run: mvn --batch-mode deploy
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Walkthrough

We are running the workflow after a release is created

on:
  release:
    types: [created]

It runs the build on an Ubuntu instance provided by GitHub, here is the specification for GitHub hosted runners. Of course, there is also an option of creating custom runners. This workflow is pretty straightforward, so I used a hosted runner.

runs-on: ubuntu-latest

now a series of workflow steps are executed in order:

- uses: actions/checkout@v2
- uses: actions/setup-java@v1
  with:
    java-version: 1.8
- name: Publish package
  run: mvn --batch-mode deploy
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

please notice we are using our GitHub token as an environment variable in order for this step to work.

our pom.xml file contains a special tag called “distributionManagement” which pinpoints to our repository

<distributionManagement>
    <repository>
        <id>github</id>
        <name>GitHub nirtal85 Apache Maven Packages</name>
        <url>https://maven.pkg.github.com/nirtal85/githubPackageExample</url>
    </repository>
</distributionManagement>

after all of this is set we can continue our work on the automation infrastructure, once we are ready, we simply go to the release tab and create a new release

No alt text provided for this image

this will automatically trigger the workflow - once the build passed successfully, a new package will be created.

No alt text provided for this image

we will need to update our local settings.xml file located in the .m2 folder with our repository URL, our GitHub username and a personal token which we need to create

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <activeProfiles>
        <activeProfile>github</activeProfile>
    </activeProfiles>
    <profiles>
        <profile>
            <id>github</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://repo1.maven.org/maven2</url>
                </repository>
                <repository>
                    <id>github</id>
                    <url>https://maven.pkg.github.com/nirtal85/githubPackageExample</url>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <servers>
        <server>
            <id>github</id>
            <username>USERNAME</username>
            <password>TOKEN</password>
        </server>
    </servers>
</settings>

now on other testing projects, we can use the automation infrastructure package as a dependency in the pom.xml file:

<dependency>
    <groupId>github.nirtal85</groupId>
    <artifactId>automation-infra</artifactId>
    <version>0.1</version>
</dependency>

and use the sayHello method to get a greeting from the infrastructure project :)

infra.CommonOperations.sayHello();

In conclusion

In this article, we reviewed the creation of an independent automation infrastructure using GitHub actions Java and Maven. Some notable alternatives to implementing this solution for Java are JFrog Artifactory and Nexusserver.

Further reading on GitHub actions features can be found in this link.

Happy testing!


Suggest Changes

Have a challenge? Let's Talk


Previous Post
Test Automation - How To Attach Public IP Adress to Allure report using Pytest and Requests
Next Post
Test Automation - Selenium Example Python Project 2022 Updates