Spring Boot with CI/CD using GitHub Actions or Jenkins [Java Spring Boot Mastery Series – Part 14]
Continuous Integration and Continuous Deployment (CI/CD) helps automate the testing, building, and deployment process. We’ll explore both:
- 🟦 GitHub Actions (free for public repositories, integrated with GitHub)
- 🟠 Jenkins (self-hosted, highly customizable)
🟦 1. CI/CD Using GitHub Actions
🔧 Step 1: Create Workflow File
Create .github/workflows/ci.yml in your project:
name: Spring Boot CI/CD
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Build with Maven
run: mvn clean install
- name: Build Docker Image
run: docker build -t myapp .
- name: Push Docker Image (optional)
run: echo "Push to DockerHub or ECR"
🔍 Description:
- Triggers on push/pull to
main. - Builds with Maven and tests your Spring Boot project.
- Optional: Integrate with DockerHub or AWS ECR for deployment.
🟠 2. CI/CD Using Jenkins
🔧 Step 1: Install Jenkins Plugins
Install these plugins in Jenkins:
- Git Plugin
- Maven Integration Plugin
- Docker Pipeline Plugin (if using Docker)
🧪 Step 2: Configure Jenkins Job
Use either Freestyle Project or Pipeline.
a. Freestyle Project Configuration:
- Source Code Management: Git → Add repository URL
- Build Trigger: GitHub hook trigger
- Build Steps:
- Invoke top-level Maven targets:
clean install - Execute shell:
- Invoke top-level Maven targets:
docker build -t myapp .
docker run -d -p 8080:8080 myapp
b. Pipeline Script Example:
pipeline {
agent any
tools {
maven 'Maven3'
jdk 'JDK17'
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/spring-boot-app.git'
}
}
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Docker Build & Run') {
steps {
sh 'docker build -t springboot-app .'
sh 'docker run -d -p 8080:8080 springboot-app'
}
}
}
}
✅ Benefits of CI/CD
| Feature | GitHub Actions | Jenkins |
|---|---|---|
| Hosted | GitHub-hosted runners | Self-hosted |
| Easy Setup | ✅ Very easy | ⚠️ Moderate |
| Customization | Basic workflows | Highly customizable |
| Cost | Free for public repos | Requires server resources |
➡️ Next Up: Part 15 – Performance Optimization & Caching with Spring Boot
