jav spring boot mastery

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:
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

FeatureGitHub ActionsJenkins
HostedGitHub-hosted runnersSelf-hosted
Easy Setup✅ Very easy⚠️ Moderate
CustomizationBasic workflowsHighly customizable
CostFree for public reposRequires server resources

➡️ Next Up: Part 15 – Performance Optimization & Caching with Spring Boot

Similar Posts