Spring Boot CI/CD Pipeline with Jenkins

In modern software development, Continuous Integration and Continuous Deployment (CI/CD) is a must-have for delivering reliable features quickly. Jenkins, a powerful open-source automation tool, makes it easy to implement CI/CD for your Spring Boot microservices.

In this post, you’ll learn how to:

  • Set up a Jenkins pipeline for Spring Boot
  • Automate testing, Docker builds, and K8s deployments
  • Store build artifacts
  • Trigger builds on git commits or pull requests

We’ll use a sample app with base package:
com.kscodes.springboot.containers

Spring Boot CI/CD Pipeline with Jenkins

🧰 Prerequisites

Before you begin, make sure you have:

  • Docker installed on Jenkins host
  • Kubernetes cluster access (Minikube, GKE, etc.)
  • Jenkins with Docker, Git, and Kubernetes CLI tools installed
  • A Spring Boot project with a working Dockerfile
  • GitHub repository for code

🏗️ Step 1: Sample Spring Boot Project Structure


springboot-ci-cd/
├── src/
├── pom.xml
├── Dockerfile
├── Jenkinsfile
└── deployment.yaml

📜 Jenkinsfile (Declarative Pipeline)

Create a Jenkinsfile in your project root:


pipeline {
    agent any

    environment {
        IMAGE_NAME = "kscodes/springboot-ci-cd"
        VERSION = "1.0.${BUILD_NUMBER}"
    }

    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/your-repo/springboot-ci-cd.git'
            }
        }

        stage('Build') {
            steps {
                sh './mvnw clean package -DskipTests'
            }
        }

        stage('Test') {
            steps {
                sh './mvnw test'
            }
        }

        stage('Docker Build & Push') {
            steps {
                script {
                    sh "docker build -t ${IMAGE_NAME}:${VERSION} ."
                    sh "docker push ${IMAGE_NAME}:${VERSION}"
                }
            }
        }

        stage('Deploy to Kubernetes') {
            steps {
                script {
                    sh "kubectl set image deployment/springboot-app springboot=${IMAGE_NAME}:${VERSION} --record"
                }
            }
        }
    }

    post {
        success {
            echo "✅ Deployment successful: ${IMAGE_NAME}:${VERSION}"
        }
        failure {
            echo "❌ Build or deployment failed"
        }
    }
}

🐳 Step 2: Dockerfile


FROM eclipse-temurin:21-jdk as builder
WORKDIR /app
COPY . .
RUN ./mvnw clean package -DskipTests

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

📄 Step 3: Kubernetes Deployment Template


apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot
  template:
    metadata:
      labels:
        app: springboot
    spec:
      containers:
        - name: springboot
          image: kscodes/springboot-ci-cd:latest
          ports:
            - containerPort: 8080

Initially apply this once:


kubectl apply -f deployment.yaml

Later, Jenkins will just update the image version.

⚙️ Step 4: Jenkins Configuration

In Jenkins:

  • Create a new Pipeline project
  • Set GitHub repo URL
  • Ensure:
  • Add Docker Hub credentials (if private) to Jenkins

🎯 Bonus: GitHub Webhooks

Automate builds on every push:

  • In GitHub → Settings → Webhooks
  • URL: http://<jenkins-url>/github-webhook/
  • Content type: application/json
  • Trigger: push events

Now Jenkins triggers CI/CD on every push or PR merge.

🛡️ Best Practices

PracticeWhy It Helps
Use semantic versioningMakes tracking and rollback easier
Use --record in kubectl set imageTracks changes in kubectl rollout
Separate test & deploy stagesPrevents broken code from going live
Monitor with Prometheus + GrafanaReal-time pipeline monitoring

📘 Summary

In this post, you built a Spring Boot Jenkins CI/CD pipeline to automate the entire software delivery process. From building and testing the app to Dockerizing it and deploying on Kubernetes — all without manual steps.

This setup enhances delivery speed, reduces risk, and ensures repeatable deployments across environments.

In the next post, we’ll explore how to deploy Spring Boot on AWS Elastic Beanstalk as an alternative cloud deployment strategy.