Github Actions: Building Spring boot application

Github Actions: Building Spring boot application

Github now a days is not just used as a central Git repository by the organisations but also as a tool for orchestration of CI/CD pipelines. Github actions automate all the software development workflows and enable build, test and deployment right from Github. In the demo we will see how we can use Github actions for basic development tasks by automating them using some configuration files.

Demo

Resources

Github account : This will host the repository for our sample application which is to be built.

Sample spring boot application

Create a project from start.spring.io website as follows:

Note that we chose a Gradle project with spring web as a dependency.

Open the downloaded project in Intellij IDE

Create html page under static directory in resources folder.

Run the application and go to the browser to load the html page as follows:

Github Workflow Changes

Create a repository in Github

Push the local code to the repository using the commands mentioned as part of repository creation.

Once we push the local changes we will see our repository has all the code:

In our local repository lets create a new directory as .github/workflows:

Create a new file called demo.yml inside .github/workflows folder with the content as follows:

The demo.yml file created above will be executed by Github actions to run our build code. The code as text is as follows:

    name: Demo to setup a project with github actions
    
    on:
    push:
    branches:
    - main
    
    jobs:
    build:
    runs-on: ubuntu-latest
    
    steps:
    - name: checking out code
    uses: actions/checkout@v2
    
    - name: Setting up java env
    uses: actions/setup-java@v1
    with:
    java-version: 11
    
    - name: grant permissions
    run: chmod +x gradlew
    
    - name: build action
    run: ./gradlew build

Once the file is created, add it to local git by committing the changes:

Once committed, just push the code to the Github repo:

Once pushed we can to the Github UI console and check the Actions tab:

All the above demonstrated how we can build any Spring boot app using Github Actions.

Gaurav
Gaurav Software Developer
comments powered by Disqus