How to Configure Java CI with Maven Checks in Github Action

Reading Time: 4 minutes

This blog will help you to have how to configure Java CI with Maven Checks in Github Action.

What is Github Action?

  • GitHub Actions is a CI/CD platform that allows you to automate your build, test, and deployment of pipelines.
  • You can create workflows that build and test and automate every pull request to your repository. And deploy merged pull requests to production.
  • All GitHub Actions automation is handle by workflows, which are YAML files places under the .github/workflows directory in a repository that defines automated processes.
  • Every workflow consists of several different core concepts.
    • Events: Events are defined triggers that kick off a workflow. They can configure to look for one or more triggers and qualify as needed by a developer. They can run on specific coding branches within a given repository on GitHub.
    • Jobs: Jobs are a set of steps that execute on the same runner. Each runs in its own VM and parallel to other jobs unless otherwise specified.
    • Steps: Steps are individual tasks that run commands in a job. These can be an action or a shell command. All steps in a job execute on the same runner.
    • Actions: An action is a command that’s execute on a runner—and the core element of GitHub Actions.
    • Runners: A runner is a GitHub Actions server. It listens for available jobs, runs each in parallel, and reports back progress, logs, and results.
  • Further, we will learn about different workflows.

Java CI with Maven workflow

  • Here will show you how to set up a CI/CD solution for a Java Maven project. this blog will start off with a simple GitHub Action that builds the Maven project and displays the result. 
  • To start you will need your own repository of our starting maven project. 
  • The pom file is used to configure the maven project and also add the maven plugins.

  • push all code on your repository.
  • Now go to the GitHub repository and there you see the action option, then click on the action option.

  • Click on Set up a workflow yourself.
  • Replace the pre-populated workflow code with the one below.
name: Java CI with Maven

on:
  push:
    branches:
      - main
      - 'feature/**'
  pull_request:
    branches:
      - main
jobs:
  build_and_test:
    runs-on: ubuntu-latest
    steps:
        uses: actions/checkout@v2

      - name: step 1 - Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: step 2 - Set up a cache for maven
        uses: actions/cache@v2
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2

      - name: step 3 - Build project with Maven
        run: mvn -B package --file pom.xml
  • The first part of the workflow means that it is trigger every time a push or a pull request is made to the master branch. We then have the jobs keyword, it is under this that we will state the name of all the different jobs we would like to run. In this case, we start off by stating build_and_test. Here we declare OS environment that it should run on ubuntu-latest.
  • Under steps, a sequence of steps are declare.
    • The first step uses an action called checkout. With the use of this other action, we give our job access to the code in our repository.
    • The second step also uses another action called setup-java. This action sets up a Java environment that our job can run in. We also use the keyword to tell setup-java to use a specific version of the JDK, which in this case is JDK-11. 
    • In the third step for setup the maven cache, it enables the cache dependencies If it finds a dependency in the cache it will restore to the argument you provided as the path. and in the last step, we will build our project using the maven package. This is done by using the run keyword.
  • Commit your new workflow file once all the workflow configuration is in place and using these steps we learned how to configure Java CI with Maven Checks in Github Action.

GitHub Action result

  • Java CI maven checks after raising the PR against the main branch.

Advantages

  • Everyone will follow the git rule or maven command
  • and will have to cut a feature branch from the main branch only. Otherwise, it fails or skips.
  • Commit/Push code will run from the Maven command and Run all the checks.
  • If the code is not execute by the Maven command, the build will fail.

Flow chart

References

https://github.com/features/actions

Written by 

Prajjawal is a QA Consultant having experience of more than 1.6 year. He is familiar with core concepts of manual & automation testing using tools like Contract test, Selenium, and Postman Also having knowledge of Core Java, Python and Data Science. He is always eager to learn new and advanced concepts in order to improve himself. He likes to watch web series and play cricket.

Discover more from Knoldus Blogs

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

Continue reading