DEV Community

bhaktraj
bhaktraj

Posted on

Deploying Java Applications on AWS ECS and ECR with a Jenkins CI/CD Pipeline

Project Arch

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:

  1. Automate builds and testing for the Java application.
  2. Perform static code analysis and quality checks.
  3. Dockerize the application for consistent deployments.
  4. Push container images to AWS ECR for versioning and storage.
  5. Deploy the application on AWS ECS with zero downtime.
  6. 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'
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Build Stage

The application is built using Maven, ensuring that all dependencies are resolved.

stage('Build') {
    steps {
        sh 'mvn clean install -DskipTests'
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Testing Stage

Automated tests are executed to validate the application.

stage('Test') {
    steps {
        sh 'mvn test'
    }
}
Enter fullscreen mode Exit fullscreen mode

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'''
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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/")
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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")
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.")
    }
}
Enter fullscreen mode Exit fullscreen mode

ecs

service

ecr


Key Benefits

  1. Automation Saves Time: Eliminates manual intervention, speeding up the delivery process.
  2. Enhanced Code Quality: SonarQube ensures adherence to coding standards.
  3. Scalable Deployments: AWS ECS allows for easy scaling as application traffic grows.
  4. Consistent Environments: Docker ensures the same runtime environment across all stages.
  5. 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!

DevOps #JenkinsPipeline #AWS #Java #Docker #CICD #CloudComputing #SonarQube #Automation #SoftwareEngineering

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay