How to setup GitHub Actions for Java with Maven project?

Mohammad Faisal Khatri
7 min readOct 19, 2022

--

cover image

Introduction

I recently completed a POC on Selenium 4 latest features where I have all the example codes of How to use Selenium WebDriver with Java. I was running the tests in my local, however there was a need to have a CI/CD pipeline in place to keep a check that the new code merges are not breaking any existing code.
And since the repository is setup on GitHub, it was easy to configure the pipeline using GitHub Actions.

GitHub Repository

Github Repository — Selenium4poc

What is GitHub actions?

As per the GitHub actions website, GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.

GitHub Actions goes beyond just DevOps and lets you run workflows when other events happen in your repository. For example, you can run a workflow to automatically add the appropriate labels whenever someone creates a new issue in your repository.

GitHub provides Linux, Windows, and macOS virtual machines to run your workflows, or you can host your own self-hosted runners in your own data center or cloud infrastructure.

How to setup GitHub actions workflow?

Setting up GitHub actions is very easy, there is no complex tasks or files that you need to create. It can be setup in the following simple steps:

Step 1 — Create a GitHub Repo and check-in all your code on GitHub

Creating and checking all your code on GitHub is again simple task. Checkout the link here which will help you to create a repository and check in your code on GitHub.

Step 2 — Go To Actions Tab and select appropriate workflow depending on your programming language

Once the GitHub repository is created and all the code is checked-in. You will have to click on the Actions Tab as shown in the screenshot below.

GitHub Actions — Workflow — Step 1

Next, you need to select the workflow depending on the programming language of your project. Here, I would be selecting Java with Maven since my project is Maven based. I have selected the second option in the first row and clicked on Configure.

GitHub Actions — Workflow — Step 2

Once you click on Configure you will be taken to a page where GitHub will auto create a workflow file as per the option you selected and accordingly if you want, you can make the changes in the file as per your needs to install packages, build the project and run the tests. It is fully customizable and provides all the implementation in your hands to choose and add the steps as per your requirement.

Once you have updated all the steps and the commands you want to run within the workflow you can commit the changes by clicking on Green button called Start Commit button on top right hand.
It will take you to the Actions Tab where you will be able to see the Workflow in Action which you just created.

On the left hand side you will be shown the workflow name, in my case it is Java CI with Maven and on the right hand side in the Tab you should be seeing the workflow running and it would be a yellow dot with commit message. My workflow is successful and complete hence in the image below you see a green tick beside the workflow run.

Github Actions — Workflow — Running the workflow

Now, if you click on the workflow run, it will take you deeper in the run and you should be able to checkout what all things are cooking inside in the run. In my case, it shows 3 jobs running, i.e.

1. Build and Test
2. Code Analysis
3. Test Results

Let’s discuss about these jobs in details:

Build and Test Job

As mentioned earlier, this repository has the code related to web automation and we are using selenium to test the website, hence it is necessary to have the setup accordingly so our tests run successfully without any hiccups.
The important thing to note here is we need to have the browsers installed in the workflow machine to make our tests work.

GitHub Actions — Build and Test Job

This job performs the following steps:

  1. Checkout the current Repository.
- name: checkout Git repository
uses: actions/checkout@v2
  1. Install Java and Maven in the workflow machine and setup the environment to run the tests.
- name: Install Java and Maven
uses: actions/setup-java@v2
with:
java-version: '15'
distribution: 'adopt'
cache: maven

Let me talk about the next step in detail here as it is related to the end to end tests running in the pipeline. I have configured the end to end tests to run on localhost. The tests are related to OWASP juice-shop website and since its code is available on GitHub, I decided to use it and pull the docker image and run the website locally inside the container. Github Actions has a cool feature to run the docker containers easily as its workflow machines comes bundled with docker. You can just type docker commands in the run step and it will work like a charm.

So, In Step 3, I am running docker commands to pull the image of Juice Shop website and in Step 4, using docker run, I am running the juice shop website in detached mode so it keeps on running in background and we would be able to run the tests using localhost.

  1. Pull the juice Shop docker image — This step will pull the docker image of juice-shop website which we can use in next step to run the website.
- name: Pull Juice Shop Image
run: docker pull bkimminich/juice-shop
  1. Run the Juice Shop Website using docker run — This step will run the juice shop website in detached mode and keep it running in background.
- name: Run Juice Shop
run: docker run -d --rm -p 3000:3000 bkimminich/juice-shop
  1. Install Chrome — This step will install latest Chrome version inside the workflow machine.
- name: Install Chrome
uses: browser-actions/setup-chrome@latest
  1. Install Firefox — This step will install latest Firefox version inside the workflow machine.
- name: Install Firefox
uses: browser-actions/setup-firefox@latest
  1. Build the Project — Here it will install all the dependencies and build the project. Note, I am not running the tests in this step as it will be done in next step.
- name: Build the Project
run: mvn clean install -DskipTests
  1. Coverage Per Test Execution
- name: Coverage per Test Execution
run: mvn org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test

I have integrated SonarQube with this repository where it performs static analysis of the code and also checks for the coverage per test in this step.

  1. Upload Target Folder — This step uploads target folder so it could be used in further steps for generating reports.
- name: Upload target folder
uses: actions/upload-artifact@v2
with:
name: target
path: |
${{ github.workspace }}/target
${{ github.workspace }}/reports

This concludes the steps for Build and Test Job, next we move on to the next job — Code Analysis.

Code Analysis Job

I have integrated this repository with SonarQube which helps in performing static analysis of the code and helps in writing quality code and use best practices. This job will mostly focus on those steps. In this job we would again have to install everything from scratch as it is a different job. So, lets begin with the details on the step this job does, I will be skipping the setup and installation steps and focus on the important and unique steps in this job:

GitHub Actions — Code analysis job
  1. Sonar Cloud Analysis
- name: Sonar Code Analysis
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_KEY: ${{ secrets.SONAR_KEY }}
run: |
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
-Dsonar.projectKey=$SONAR_KEY

This step will perform the sonar code analysis. It requires GitHub Token, Sonar Token and Sonar Key for the analysis to run, I have stored these values as GitHub Secret and hence calling it as a variable here in this step. There are other configuration setting as well which is required for sonar code analysis which you can find in pom.xml.

Test Report Job

This is the final step in the process where the summary of the tests would be taken in to account and a test report would be generated which would show the summary of the tests that ran.

- name: Test Report
uses: dorny/test-reporter@v1
if: success() || failure()
with:
name: Test Results
path: ${{ github.workspace }}/target/surefire-reports/TEST-TestSuite.xml
reporter: java-junit
java-version: 11

Checkout the Test Result image, it shows 46 tests ran successfully in 76 seconds and also provides the summary of tests and its respective test methods that were run.

GitHub Actions — Test Report

Conclusion

In this blog, we learnt about GitHub actions and how to setup a pipeline using it. We further discussed about how to set up multiple jobs, run sonar and code analysis and also generate and view test reports.

I hope you learned something new today and got better understanding about GitHub actions.Please feel free to reach out to me if you have any query.

Happy Learning!

Freelance Work / Paid Trainings/Mentoring

Contact me for Paid trainings/Mentoring related to Test Automation and Software Testing, ping me using any of the social media site listed on LinkTree or email me @mohammadfaisalkhatri@gmail.com.

--

--

Mohammad Faisal Khatri

QA with 14+ years of experience in automation as well as manual testing. Freelancer, blogger and open source contributor.