Modern software development demands speed, reliability, and scalability. To meet these challenges, I recently built a CI/CD pipeline that seamlessly deploys a Java application on AWS ECS (Elastic Container Service) and ECR (Elastic Container Registry) using Jenkins. Here’s a detailed walkthrough of the project and the technologies that made it possible.
Project Overview
The goal was to automate the entire software delivery process—from code integration to deployment—ensuring fast, reliable, and scalable application delivery.
Key Objectives:
- Automate builds and testing for the Java application.
- Perform static code analysis and quality checks.
- Dockerize the application for consistent deployments.
- Push container images to AWS ECR for versioning and storage.
- Deploy the application on AWS ECS with zero downtime.
- Notify the team about build and deployment statuses via Slack.
Technologies Used
- Jenkins: CI/CD pipeline automation.
- Maven: Build and dependency management.
- SonarQube: Code quality analysis.
- Docker: Containerization of the Java application.
- AWS ECS/ECR: Container orchestration and storage.
- Slack: Real-time team communication.
Pipeline Workflow
The Jenkins pipeline was structured into several stages:
1. Fetch the Code
The pipeline fetches the latest code from the Git repository.
stage("Fetch the code") {
steps {
git url: 'https://github.com/bhaktraj/deploy_javaapp_on_aws_ecs.git', branch: 'main'
}
}
2. Build Stage
The application is built using Maven, ensuring that all dependencies are resolved.
stage('Build') {
steps {
sh 'mvn clean install -DskipTests'
}
}
3. Testing Stage
Automated tests are executed to validate the application.
stage('Test') {
steps {
sh 'mvn test'
}
}
4. Code Quality Analysis
SonarQube is integrated to perform static code analysis and ensure adherence to quality standards.
stage('Code analysis with checkstyle') {
environment {
scannerHome = tool 'sonarserver'
}
steps {
withSonarQubeEnv('sonarserver') {
sh '''${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=vprofile \
-Dsonar.projectName=vprofile-repo \
-Dsonar.projectVersion=1.0 \
-Dsonar.sources=src/ \
-Dsonar.java.binaries=target/test-classes/com/visualpathit/account/controllerTest/ \
-Dsonar.junit.reportsPath=target/surefire-reports/ \
-Dsonar.jacoco.reportsPath=target/jacoco.exec \
-Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml'''
}
}
}
5. Docker Image Build
The Java application is packaged into a Docker image using a multi-stage Dockerfile.
stage('image_build') {
steps {
script {
dockerimage = docker.build(imagename + ":$BUILD_NUMBER", "./Docker-files/app/multistage/")
}
}
}
6. Push to AWS ECR
The Docker image is pushed to AWS ECR, enabling version control and easy access for deployment.
stage('upload image to ecr') {
steps {
script {
docker.withRegistry(registeryurl, awscred) {
dockerimage.push("$BUILD_NUMBER")
dockerimage.push("latest")
}
}
}
}
7. Deployment to AWS ECS
The application is deployed on AWS ECS using the update-service
command, ensuring zero downtime.
stage('deploy to ecs') {
steps {
withAWS(credentials: 'awscred', region: 'us-east-1') {
sh 'aws ecs update-service --cluster ${cluster} --service ${service} --force-new-deployment'
}
}
}
Post-Build Notifications
To keep the team informed, Slack notifications are sent at each build stage—success, failure, or completion.
post {
success {
slackSend(channel: '#all-javacicdproject', color: 'good', message: "Build #${env.BUILD_NUMBER} succeeded!")
}
failure {
slackSend(channel: '#all-javacicdproject', color: 'danger', message: "Build #${env.BUILD_NUMBER} failed.")
}
always {
slackSend(channel: '#all-javacicdproject', color: 'warning', message: "Build #${env.BUILD_NUMBER} completed.")
}
}
Key Benefits
- Automation Saves Time: Eliminates manual intervention, speeding up the delivery process.
- Enhanced Code Quality: SonarQube ensures adherence to coding standards.
- Scalable Deployments: AWS ECS allows for easy scaling as application traffic grows.
- Consistent Environments: Docker ensures the same runtime environment across all stages.
- Real-Time Feedback: Slack notifications provide instant updates to the team.
Conclusion
This project highlights the power of combining Jenkins, Docker, and AWS to create a seamless CI/CD pipeline for modern Java applications. The result is a reliable, scalable, and automated deployment process that aligns with DevOps best practices.
Have you worked on a similar project or have questions about setting up a CI/CD pipeline? Let’s discuss in the comments!
Top comments (0)