Spring Boot CI/CD with GitHub Actions

In today’s fast-paced development cycle, Continuous Integration (CI) and Continuous Deployment (CD) are essential for building reliable, testable, and deployable applications.

In this post, we’ll walk you through integrating Spring Boot CI/CD with GitHub Actions. You’ll learn how to:

  • Set up automated tests and builds
  • Generate test reports
  • Run integration and unit tests
  • Use best practices for GitHub workflow design

We’ll use a sample project in the package com.kscodes.springboot.

Spring Boot CI/CD with GitHub Actions

🧾 Project Structure


com.kscodes.springboot
β”œβ”€β”€ controller
β”‚   └── HelloController.java
β”œβ”€β”€ service
β”‚   └── GreetingService.java
└── KSCodesApplication.java

πŸ“˜ HelloController.java


package com.kscodes.springboot.controller;

import com.kscodes.springboot.service.GreetingService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class HelloController {

    private final GreetingService service;

    public HelloController(GreetingService service) {
        this.service = service;
    }

    @GetMapping("/hello")
    public String sayHello() {
        return service.greet();
    }
}

πŸ“˜ GreetingService.java


package com.kscodes.springboot.service;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public String greet() {
        return "Hello from KSCodes!";
    }
}

βœ… Test File Example

πŸ“— GreetingServiceTest.java


package com.kscodes.springboot.service;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class GreetingServiceTest {

    @Test
    public void shouldReturnGreeting() {
        GreetingService service = new GreetingService();
        assertEquals("Hello from KSCodes!", service.greet());
    }
}

πŸ§ͺ Step 1: Add GitHub Actions Workflow

Create a workflow file at:


.github/workflows/springboot-ci.yml

πŸ› οΈ springboot-ci.yml


name: Spring Boot CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '21'

      - name: Build with Maven
        run: mvn -B clean verify

      - name: Upload Test Report
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: junit-report
          path: target/surefire-reports/

πŸ“Š Output from Workflow

  • βœ… Runs mvn clean verify
  • βœ… Executes unit tests and integration tests
  • βœ… Uploads surefire-reports with test results
  • βœ… Fails PRs on test failures

🧠 Optional: Add Code Coverage with JaCoCo

Add to pom.xml:



  org.jacoco
  jacoco-maven-plugin
  0.8.8
  
    
      
        prepare-agent
      
    
    
      report
      test
      
        report
      
    
  


This will produce target/site/jacoco/index.html after test runs. You can optionally upload it to GitHub Artifacts:


      - name: Upload Coverage Report
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: jacoco-report
          path: target/site/jacoco/

🧠 Best Practices for Spring Boot CI/CD with GitHub Actions

PracticeBenefit
βœ… Use verify not just testIncludes linting, integration checks
βœ… Separate jobs for test/build/deployBetter control and caching
βœ… Use conditional upload of artifactsOnly when needed (if: always())
βœ… Add coverage badges in READMETransparency on code quality
βœ… Fail fast on test failuresPrevent bad code from merging

πŸ” Deployment (Optional CI/CD)

If you want to auto-deploy on a successful build:


- name: Deploy to Heroku (or DockerHub, AWS, etc)
  run: |
    echo "Add your deployment commands here"

Or use GitHub Actions integrations with:

  • DockerHub (docker/build-push-action)
  • AWS Elastic Beanstalk / ECS
  • Azure App Service
  • Kubernetes via kubectl

πŸ“š Summary

You now have a complete working pipeline for Spring Boot CI/CD with GitHub Actions! In this post, you:

  • Created a working Spring Boot project with tests
  • Set up GitHub Actions to automate builds and test reports
  • Learned best practices for reliable and maintainable CI/CD pipelines