Automated CI_CD Pipeline for Java Project
Automated CI_CD Pipeline for Java Project
Following
Palak Bhawsar
+39
In this article, we will be creating an automated CI/CD pipeline for your Java
project using Jenkins, Docker, and AWS. With this pipeline, your project will be
automatically built, tested, and deployed to your AWS EC2 instance every time
you push code to your GitHub repository.
Prerequisite:
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 1/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
COPY
Set JAVA_HOME and MAVEN_HOME using the below commands. To set the
variable permanently, you should add it to the .bashrc file in your home
directory.
COPY
Install plugins to integrate Jenkins with GitHub, Maven, and EC2. Go to Manage
Jenkins, and select Manage plugins. Under available plugins search for the
below plugins and Install without restart
1. Git
2. Pipeline maven integration
3. Pipeline stage view
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 2/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
4. SSH Agent
Go to Manage Jenkins, select Global tool configuration, and scroll down to add
JDK and Maven path that we exported in the above steps as shown below.
Uncheck the Install automatically checkbox, Give Name and Path and click
Save.
Webhook in Jenkins triggers the pipeline automatically when any changes are
done in the GitHub repository like commit and push. Go to Jenkins dashboard
and copy the URL in the browser. Now go to GitHub repository settings. In the
left pane select Webhooks.
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 3/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
Click Add webhook and paste the Jenkins URL in the Payload URL by
appending the URL with /github-webhook/ as shown below. Select the events
when you want to trigger the pipeline, I have selected Just the push event and
click Add webhook.
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 4/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
Click Security in the left panel and click New Access Token. Give a description
and click Generate. Copy the token and close. Keep this token as we will be
adding this to the Jenkins credential in the next steps.
Go to the Jenkins dashboard -> Manage Jenkins -> Manage credentials. Click
System -> Global credentials and then click Add credentials. Select the kind as
Username and Password and enter your DockerHub username and enter the
token in Password that we have generated in dockerHub. Give the ID and
description of your choice but remember this ID as we will be using it in
Jenkinsfile and click Create.
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 5/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
I have launched the Amazon Linux instance and setup Docker using the below
commands:
COPY
# Update packages
sudo yum update
# Install Docker
sudo yum install docker
# Add ec2-user to Docer group
sudo usermod -a -G docker ec2-user
# Enable docker service at AMI boot time
sudo systemctl enable docker.service
# Start the Docker service
sudo systemctl start docker.service
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 6/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
we will be using it in Jenkinsfile. Enter the username of the EC2 instance that
you have launched. For the Amazon Linux instance, the username is ec2-user.
Select Enter directly and click Add and copy-paste the private key that you
have created while launching an instance and click Create.
Write Jenkinsfile with all the below steps to fetch, build, test and deploy Java
application. Remember to add the below Jenkinsfile in the root directory of your
project in GitHub.
GitHub Project
COPY
pipeline {
agent any
environment {
DOCKERHUB_CREDENTIALS = credentials('docker-hub-cred')
REMOTE_SERVER = 'your-remote-server-ip'
REMOTE_USER = 'your-remote-server-user'
}
stages {
stage('checkout') {
steps {
git branch: 'main', url: 'https://github.com/palakbhawsar98/JavaWebAp
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 7/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
stage('Maven Build') {
steps {
sh 'mvn clean install'
}
post {
success {
archiveArtifacts artifacts: '**/target/*.jar'
}
}
}
stage('Maven Test') {
steps {
sh 'mvn test'
}
}
steps {
sh 'docker build -t javawebapp:latest .'
sh 'docker tag javawebapp palakbhawsar/javawebapp:latest'
}
}
stage('Login to DockerHub') {
steps {
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_C
}
}
}
post {
always {
sh 'docker logout'
}
}
Also, write Dockerfile with the instructions to build the Java project and keep
this file in the root directory of the project.
COPY
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 9/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
Go to Jenkins Dashboard click New Item -> Give a name to the pipeline ->
Select Pipeline -> Click Ok
Add Description of your pipeline -> Build Triggers -> GitHub hook trigger for
GITScm polling. Scroll to the last in the Pipeline section and from the dropdown
select Pipeline script from SCM. Under SCM, select Git and enter your GitHub
project repository URL. If your GitHub repository is private then add credentials.
Also, enter the branch name in Branches to build and the Jenkinsfile name in
Script Path and click Save. Finally, Click Build Now to run the pipeline.
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 10/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
The Jenkins pipeline run successfully and the docker image got deployed to the
EC2 instance.
Access your Java Application in the browser using the public IP of the AWS
instance at port 8081
publicIP:8081/hello
Thank You
Palak Bhawsar ✨
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 11/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
MORE ARTICLES
Palak Bhawsar
Palak Bhawsar
An intelligent journaling app that analyzes your entries, uplifts you during
tough times, and celebr…
Palak Bhawsar
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 13/14
4/8/25, 11:39 AM Automated CI/CD pipeline for Java Project
https://palak-bhawsar.hashnode.dev/automated-cicd-pipeline-for-java-project 14/14