Cloud Native 11 min read

Building a Jenkins CI/CD Pipeline for Java Applications with Docker and Helm

This article provides a step‑by‑step guide to creating a Jenkins CI/CD pipeline for Java applications, covering environment setup, required plugins, project structure, CI stages such as code checkout, dependency installation, testing, SonarQube scanning, Docker image building and pushing, as well as CD stages including Helm chart creation, deployment, health checks, rollback, and maintenance‑window management.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Building a Jenkins CI/CD Pipeline for Java Applications with Docker and Helm

Abstract

As continuous integration (CI) and continuous delivery (CD) become mainstream, automating build and deployment is essential. This guide shows how to create a Jenkins pipeline for a Java application, covering preparation, CI stages (code checkout, dependency installation, unit testing, test reporting, SonarQube scanning, Docker image build and push), and CD stages (monitoring maintenance window, Helm chart creation, Helm deployment, health checks, rollback, and closing the maintenance window).

1. Preparation

1.1 Environment Setup

Install Jenkins : Download and install the latest Jenkins version, optionally using Docker to run a Jenkins container.

Ensure the Jenkins instance can access the Internet to download dependencies and plugins.

Install Required Plugins :

Configure JDK and Docker : Set the JDK and Docker installation paths in Jenkins.

Configure Credentials : Add credentials for Docker Hub or a private Docker registry.

1.2 Project Structure

Ensure the Java project follows a standard layout, typically including:

Source Code : Main application code.

Build Files : pom.xml for Maven or build.gradle for Gradle.

Dockerfile : Defines how to build the Docker image.

Test Files : Automated test scripts.

Example Dockerfile for a Java application:

# Use the official Java runtime as the base image
FROM openjdk:11-jre-slim

# Set the working directory
WORKDIR /app

# Copy the jar file into the container
COPY target/java-app.jar /app/java-app.jar

# Run the application
CMD ["java", "-jar", "java-app.jar"]

2. CI Pipeline

The CI pipeline automates building and testing. Its stages are:

Code Checkout : Retrieve the latest code from version control.

Dependency Installation : Install required dependencies using Maven or Gradle.

Unit Testing : Run unit tests to verify code correctness.

Generate Test Report : Produce test reports with coverage information.

SonarQube Code Scan : Perform static analysis with SonarQube Scanner.

Build Docker Image : Build the Docker image using the Dockerfile.

Push Docker Image : Push the image to Docker Hub or a private registry.

2.2 CI Pipeline Example

Simple Jenkins Pipeline script implementing the CI stages:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps { git 'https://github.com/your-repo/java-app.git' }
        }
        stage('Install Dependencies') {
            steps { sh 'mvn clean install' }
        }
        stage('Run Tests') {
            steps { sh 'mvn test' }
        }
        stage('Generate Test Report') {
            steps { junit '**/target/surefire-reports/*.xml' }
        }
        stage('SonarQube Code Scan') {
            steps { script { sh 'sonar-scanner' } }
        }
        stage('Build Docker Image') {
            steps { script {
                def appName = 'java-app'
                def version = 'latest'
                sh "docker build -t ${appName}:${version} ."
            } }
        }
        stage('Push Docker Image') {
            steps { script {
                def appName = 'java-app'
                def version = 'latest'
                sh "docker push ${appName}:${version}"
            } }
        }
    }
}

3. CD Pipeline

The CD pipeline deploys the tested code to production.

3.1 CD Pipeline Stages

Set Monitoring Alerts Maintenance Window : Configure Prometheus (or similar) to silence alerts during deployment.

Write Helm Chart : Create a Helm chart that defines Kubernetes resources.

Install Application with Helm : Deploy the chart to the cluster.

Application Health Check : Verify the service is healthy.

Application Rollback : Roll back using Helm if deployment fails.

Close Maintenance Window : Prompt to re‑enable alerts.

Monitoring and Validation : Observe the new version for anomalies.

3.2 Helm Chart Example

Directory layout:

java-app/
├── Chart.yaml
├── values.yaml
└── templates
    ├── deployment.yaml
    └── service.yaml

Chart.yaml (basic chart metadata):

apiVersion: v2
name: java-app
version: 0.1.0

values.yaml (configurable parameters):

replicaCount: 2

image:
  repository: your-docker-repo/java-app
  tag: latest

service:
  type: ClusterIP
  port: 8080

deployment.yaml (Kubernetes Deployment definition):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
        - name: java-app
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          ports:
            - containerPort: 8080

3.3 CD Pipeline Example

Jenkins Pipeline script for the CD process:

pipeline {
    agent any
    stages {
        stage('Set Monitoring Alerts Maintenance Window') {
            steps { script { echo 'Setting maintenance window for monitoring alerts...' } }
        }
        stage('Wait for Maintenance Window') {
            steps { script { echo 'Waiting for maintenance window to take effect...' } }
        }
        stage('Build Helm Chart') {
            steps { sh 'helm package java-app' }
        }
        stage('Install Application with Helm') {
            steps { sh 'helm install java-app ./java-app --namespace your-namespace' }
        }
        stage('Application Health Check') {
            steps { script { sh 'curl --fail http://your-service-url/health || exit 1' } }
        }
        stage('Application Rollback') {
            steps { script { sh 'helm rollback java-app' } }
        }
        stage('Close Maintenance Window') {
            steps { script {
                def response = input id: 'CloseMaintenanceWindow',
                                   message: 'Do you want to close the maintenance window?',
                                   parameters: [[ $class: 'BooleanParameterDefinition', name: 'Close', defaultValue: true ]]
                if (response.Close) {
                    echo 'Closing maintenance window...'
                } else {
                    echo 'Maintenance window remains open.'
                }
            } }
        }
        stage('Monitor and Validate') {
            steps { echo 'Monitoring application performance...' }
        }
    }
}

Conclusion

Implementing a Jenkins CI/CD pipeline for a Java application automates building, testing, and deployment, improving development efficiency and software quality. The guide covers preparation, CI stages (including test reporting, SonarQube scanning, and Docker image creation) and CD stages (maintenance‑window handling, Helm chart creation, Helm deployment, health checks, rollback, and monitoring). Continued refinement of these processes will further enhance DevOps practices.

JavaDockerCI/CDKubernetesJenkinsHelm
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.