Mastering CI/CD Automation for Java Applications with Jenkins, AWS, Docker, Kubernetes, SonarQube, and Nexus: A Step-by-Step Guide
Reza Chegini
Certified GCP & AWS DevOps Engineer| Seeking Entry-Level Cloud Developer, DevOps, SRE Roles, Software Engineer or Developer | Aspiring DevOps & SRE
Introduction
In this guide, we’ll explore how to set up a Jenkins pipeline to automate your CI/CD process for a Java application. This pipeline integrates key DevOps tools, including AWS, Docker, Kubernetes (K8s), SonarQube, and Nexus, to streamline the software development lifecycle.
We’ll cover:
Whether you’re a beginner or an experienced DevOps practitioner, this guide will help you set up a reliable, automated CI/CD pipeline.
In a future post, I’ll also explain how to automate the installation and configuration of Jenkins, SonarQube, Docker, Kubernetes, and Nexus using an Ansible Playbook, making it even easier to establish your DevOps infrastructure.
Let’s dive in!
1. The Jenkins Pipeline
Here’s a sample Jenkins pipeline script that automates the following steps:
Pipeline Code
pipeline {
agent any
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: '2')
}
tools {
maven 'Maven'
}
environment {
AWS_CREDENTIALS = credentials('aws-key')
}
stages {
stage('Checkout Code') {
steps {
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/example-org/example-repo']])
}
}
stage('SonarQube Analysis') {
steps {
script {
def mvn = tool 'Maven'
withSonarQubeEnv(installationName: 'sonarqube-server') {
sh "${mvn}/bin/mvn clean verify sonar:sonar -Dsonar.projectKey=example-project -Dsonar.projectName='Example Project'"
}
}
}
}
stage('Build and Package') {
steps {
sh 'mvn package'
}
}
stage('Publish to Nexus') {
steps {
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: 'https://nexus.example.com:8081',
groupId: 'com.example',
version: '1.0-SNAPSHOT',
repository: 'maven-snapshots',
credentialsId: 'nexus-credentials',
artifacts: [
[artifactId: 'ExampleApp',
classifier: '',
file: 'target/example-app-1.0.war',
type: 'war']
]
)
}
}
stage('Build Docker Image') {
steps {
sh 'docker build -t example-app .'
}
}
stage('Push Docker Image to ECR') {
steps {
sh """
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
docker tag example-app:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/example-app:latest
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/example-app:latest
"""
}
}
stage('Deploy to Kubernetes') {
steps {
sh """
aws eks update-kubeconfig --region us-east-1 --name example-cluster
kubectl apply -f deployment.yaml
"""
}
}
}
post {
always {
echo "Pipeline execution completed."
}
success {
echo "Pipeline executed successfully!"
}
failure {
echo "Pipeline execution failed."
}
}
}
2. Jenkins Pipeline Code: Line-by-Line Explanation
Pipeline Declaration
pipeline {
Agent Declaration
agent any
What It Does: Specifies that the pipeline can run on any available Jenkins agent. If there are multiple agents in your Jenkins setup, Jenkins will assign one dynamically.
Pipeline Options
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: '2')
}
Tools Configuration
tools {
maven 'Maven'
}
Environment Variables
environment {
AWS_CREDENTIALS = credentials('aws-key')
}
Stages Section
stages {
Stage: Checkout Code
stage('Checkout Code') {
steps {
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/example-org/example-repo']])
}
}
Stage: SonarQube Analysis
stage('SonarQube Analysis') {
steps {
script {
def mvn = tool 'Maven'
withSonarQubeEnv(installationName: 'sonarqube-server') {
sh "${mvn}/bin/mvn clean verify sonar:sonar -Dsonar.projectKey=example-project -Dsonar.projectName='Example Project'"
}
}
}
}
领英推荐
Stage: Build and Package
stage('Build and Package') {
steps {
sh 'mvn package'
}
}
Stage: Publish to Nexus
stage('Publish to Nexus') {
steps {
nexusArtifactUploader(
nexusVersion: 'nexus3',
protocol: 'http',
nexusUrl: 'https://nexus.example.com:8081',
groupId: 'com.example',
version: '1.0-SNAPSHOT',
repository: 'maven-snapshots',
credentialsId: 'nexus-credentials',
artifacts: [
[artifactId: 'ExampleApp',
classifier: '',
file: 'target/example-app-1.0.war',
type: 'war']
]
)
}
}
Stage: Build Docker Image
stage('Build Docker Image') {
steps {
sh 'docker build -t example-app .'
}
}
Stage: Push Docker Image to ECR
stage('Push Docker Image to ECR') {
steps {
sh """
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
docker tag example-app:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/example-app:latest
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/example-app:latest
"""
}
}
Stage: Deploy to Kubernetes
stage('Deploy to Kubernetes') {
steps {
sh """
aws eks update-kubeconfig --region us-east-1 --name example-cluster
kubectl apply -f deployment.yaml
"""
}
}
Post Actions
post {
always {
echo "Pipeline execution completed."
}
success {
echo "Pipeline executed successfully!"
}
failure {
echo "Pipeline execution failed."
}
}
}
3. Installing Plugins for AWS and Docker in Jenkins
Step 1: Installing the AWS Plugin
Step 2: Installing the Docker Pipeline Plugin
4. Creating Secrets for AWS
For AWS Credentials
Why Use Jenkins?
Jenkins is a powerful, flexible, and open-source CI/CD tool that serves as the backbone for modern DevOps pipelines. Here’s why Jenkins stands out:
By using Jenkins, teams can ensure faster deployments, improved code quality, and enhanced collaboration.
Conclusion
This guide demonstrates how to:
Stay tuned to learn how to simplify DevOps infrastructure setup with automation tools!
Instructive??
Student @ Università degli Studi di Genova | Computer Vision, Machine Learning
1 个月Love this ??