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

🧰 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
1 2 3 4 5 6 7 8 9 |
springboot-ci-cd/ ├── src/ ├── pom.xml ├── Dockerfile ├── Jenkinsfile └── deployment.yaml |
📜 Jenkinsfile (Declarative Pipeline)
Create a Jenkinsfile
in your project root:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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:
1 2 3 4 |
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:
- Jenkins has Docker access
- Jenkins can run shell commands
kubectl
anddocker
are in Jenkins PATH
- 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
Practice | Why It Helps |
---|---|
Use semantic versioning | Makes tracking and rollback easier |
Use --record in kubectl set image | Tracks changes in kubectl rollout |
Separate test & deploy stages | Prevents broken code from going live |
Monitor with Prometheus + Grafana | Real-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.