Building a Spring Boot Microservice with GitHub Actions is as easy as doing it locally. Create a Microservice and build it on Github in under 5 minutes.

Introduction

One of the best things that happened to Java was the mainstream adoption of testing and CI/CD pipelines. This started many engineering practices and ultimately banished the dreaded “Golden Machine” anti-pattern.

However, it generally relied on a CI/CD pipeline to be available. What I like about GitHub actions is that no setup is required. You create the GitHub action file locally and push it. The remote build then starts working.

So now you can have a remote CI build from your first commit.

The Four Steps

You won’t find an easier blog than this. It consists of just four steps.

Continuous improvement dictates that once you know the steps and understand what they do, you must optimise them.

So, at the end of this blog, there is a script you can run to do all this.

Code

An example of the code and the complete build can be found here.
https://github.com/johndobie/spring-boot-github-action

Step 1 – Create a GitHub Repo.

You’ll need the GitHub CLI installed locally if you want to do this from the command line. https://cli.github.com

Once installed, it is as simple as running the following commands

Create a new repository in Github and make a local copy
gh repo create spring-boot-action --public --clone
cd spring-boot-action

Otherwise, create and check out a repo as you would normally.

Step 2 – Use Spring Initializr to create your code.

Spring Initializr allows you to create the skeleton code for your Spring Boot Microservices, and we will use it to make a skeleton to test our action.

https://start.spring.io/

Most people use Spring Initializr from the web interface or directly in their IDE, but you can use it from the command line.

The commands below will create a simple Spring Application that you can use for your Microservices and push to GitHub.

Create a new Spring Boot project and push the code
curl https://start.spring.io/starter.tgz -d dependencies=web,actuator,devtools -d type=maven-project -d language=java -d bootVersion=3.4.2 -d groupId=com.example -d artifactId=springboot-github-action -d name=springboot-github-action -d packageName=com.example.github.action -d baseDir=spring-boot-action | tar -xzvf -
git add --all
git commit -m "add spring boot code"

It will create the following Skelton code.

You can build it as follows.

Bash
mvn clean install
..
..
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.135 s
[INFO] Finished at: 2025-02-17T14:05:41Z
[INFO] ------------------------------------------------------------------------

We want to recreate this local build remotely on GitHub.

Step 3 – Download a GitHub Action.

There is a standard GitHub action that will replicate the local Maven build on GitHub. You can use curl to download it as below.

Create the Github Workflow directory and download the Github Action
mkdir -p .github/workflows
curl -o .github/workflows/maven.yml https://raw.githubusercontent.com/actions/starter-workflows/refs/heads/main/ci/maven.yml

This action looks like this

YAML
# 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: Java CI with Maven

on:
  push:
    branches: [ $default-branch ]
  pull_request:
    branches: [ $default-branch ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: maven
    - name: Build with Maven
      run: mvn -B package --file pom.xml

    # 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

You’re going to change lines 13 and 14 to this

YAML
on:
  push:
    branches: [ "**" ]
  pull_request:
    branches: [ "**" ]

and delete lines 32-35

YAML

    # 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

That’s your action, ready to go.

It will simply check out the code and build it on GitHub with Maven and Java 17.

Step 4 – Push Your Code and Check Your Build.

Commit and push the code including the Github Action
git add --all
git commit -m "add github action"
git push --set-upstream origin master

The moment of truth – when you push your code, you should see your first build.

Go to the actions tab on GitHub.

https://github.com/johndobie/spring-boot-github-action/actions

https://github.com/johndobie/spring-boot-github-action/actions

From now on, a new build will run every time you commit.

Scripting The Steps

Now that you know and understand the steps, we can optimise the process.
You can see a full script here that will perform all the above steps.

https://github.com/johndobie/spring-boot-github-action/blob/master/script/create-application.sh

Bash
./create-application.sh <repository_name>

Follow On

We have not gone through the ins and outs of GitHub actions but instead have concentrated on how to create your first one.

The next step is to look at all they have to offer.

https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-java-with-maven

Other Blogs

If you liked this blog, you may be interested in these other blogs.

https://johndobie.com/blog/testing-spring-boot-microservices-cheat-sheet/

https://johndobie.com/blog/validation-errors-in-spring-boot-microservices/