Spring boot application with GitHub actions

Spring boot application with GitHub actions

This article will show you the advantages of the GitHub action with the Spring Boot application.Please Imagine this flow, If you want to create the PR for review by your team member, and at the same time your team needs to ensure the PR works and verify the team techniques within the team’s standards and that the test cases are work, and you can include other steps on this flow to verify more. So in such cases, we need to use tools to trigger the repository has PR and run the specific flows to give us an alert if the PR did not achieve the requirements for merging the PR.

Github Action:

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.

Let's Go:

Created a spring boot project like the following structure, and I have a GitHub repository for it.


Article content

To prepare a workflow on GitHub we need to define a new file called .github/workflow in the root project directory and push them to the repository.

To prepare the workflow we need to define this directory inside the repository.

.github/workflows, and inside the workflows file will define all the workflows.

In this article we need to define one workflow, to verify PRs.

and our file structure will be like the following.


Article content

After preparing everything, we need to write our workflow, which will work on the specific event, and the event in our case will be any PR created to develop the branch. If you see the file called verify_PR.yml, I will create the workflow steps inside it.

Before showing the full file, I will write a separate code to explain all important lines.

  • We need to define the name of the workflow, and we need to display the brief description for display on GitHub, the following lines of code will do it for us.

name: Verify the PR
run-name: ${{ github.actor }} is verify the PR.        

name we need this key to define the workflow name.

run-name when the workflow runs on GitHub, will display the author name and what the flow will do for us. It is a preferred description for the Other users.

  • Now, we define the settings and put the event, the workflow will work when the event comes, for example, pull-request or merge, and so on. here we defined the trigger with pull-request for the event and on the develop branch.

Note: We can select many branches here

on:
  pull_request:
    branches:
      - develop        

  • Jobs flow: here we will write all the jobs that will execute when running this workflow.

The important sections, we need to define

1- build: prepare the environment for running the steps.

  build:

    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        os: [ubuntu-latest]        

Here we prepared the Ubuntu environment.

2- steps: will run it per order, on the environment you are prepared.

and for the steps: we need to prepare many steps:

  • the first one check out the branch

   - name: check out branch
        uses: actions/checkout@v3        

  • because we are working on the Spring boot application, and we need to install JDK on the environment because the environment is a virtual environment and the environment will be destroyed after finishing the workflow.

  - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: maven        

Installed JDK 17 because I work on it, so you need to select the compatible version with your application.

  • Now we will play with Maven to check if the code will compile or not and run all the test cases and so on.

 - name: compile the project
        run: mvn compile

      - name: run test case
        run: mvn test

      - name: Build with Maven
        run: mvn clean install package           

you can see the file content from the following snippet code.

# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Verify the PR
run-name: ${{ github.actor }} is verify the PR.

on:
  pull_request:
    branches:
      - develop

jobs:
  build:

    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        os: [ubuntu-latest]

    steps:

      - name: check out branch
        uses: actions/checkout@v3

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: maven

      - name: clear project
        run: mvn clean

      - name: compile the project
        run: mvn compile

      - name: run test case
        run: mvn test

      - name: Build with Maven
        run: mvn clean install package


#      # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
#      - name: Update dependency graph
#        uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6        

Finally:

Now after preparing the workflow, you need to trigger it you need to create a PR from x branch to develop branch .

And you will see the pipeline result on GitHub using action tab.


Article content


To view or add a comment, sign in

Others also viewed

Explore topics