Project - Setting Up A CI - CD Pipeline Using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
Project - Setting Up A CI - CD Pipeline Using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
Abhiraj Kharbade
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 1/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
Introduction
Continuous Integration and Continuous Deployment (CI/CD) is a widely adopted
approach that allows developers to automate their software delivery pipeline,
enabling faster and more efficient development cycles, and reducing errors. CI/CD
is an essential practice for any software development team looking to streamline
their processes and deliver high-quality software to end-users.
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 2/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
In this blog post, we will discuss how to set up a CI/CD pipeline using Java, Maven,
JUnit,TABLE OFGitHub,
Jenkins, CONTENTSAWS EC2, Docker, and AWS S3. We will walk through the
entire process, from setting up the environment to deploying the application to a
Docker container and storing the artifact file in an S3 bucket.
Project Steps:
Step 1: Create an AWS EC2 Instance
We will create an AWS EC2 instance that will host Jenkins and Docker. We will use
t2.medium Ubuntu instance with a 10GB root volume.
you can follow the following steps to launch a new instance:
1. Log in to your AWS Management Console.
TABLE OF CONTENTS
4. Choose the Amazon Machine Image (AMI) for Ubuntu 22.04 LTS. You can find
it by typing "Ubuntu" in the search box and selecting the version from the list.
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 4/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 5/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 6/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
Once it's up and running, you can connect to it using SSH and start using it as
needed.
Note: Also Modify the IAM Role for the instance so that it can access S3 Bucket.
Here is a link to the official AWS documentation on how to modify an IAM role to
grant access to an S3 bucket:
https://docs.aws.amazon.com/en_us/AWSEC2/latest/UserGuide/iam-roles-for-
amazon-ec2.html#modify-iam-role-s3-bucket-permissions
You can also follow the AWS documentation to create an EC2 instance:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 7/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
We will use AWS S3 to store our artifact file. Create an S3 bucket named
TABLE OF CONTENTS
"abhirajcicdjava" in your AWS account.
Steps to create an S3 bucket with the name "abhirajcicdjava" and make it public:
1. Navigate to the S3 service.
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 8/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
4. Leave the other settings at their default values and click on the "Create
bucket" button.
5. Once your bucket is created, select it from the list of buckets.
6. Click on the "Permissions" tab.
7. Click on the "Bucket Policy" button.
8. In the "Bucket policy editor" window, paste the following policy:
COPY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 9/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
],
TABLE OF"Resource":
CONTENTS[
"arn:aws:s3:::abhirajcicdjava/*"
]
}
]
}
1. Install Docker.io:
COPY
COPY
TABLE OF CONTENTS
sudo docker run hello-world
3. After the installation is complete, verify the Maven version by running the
following command:
COPY
mvn -version
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 12/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
4. This command should display the Maven version and other details.
5. TABLEit!OFYouCONTENTS
That's have now installed Maven on your system.
6. We will now write a Jenkinsfile that will checkout the code from GitHub, build
the code using Maven, test it using JUnit, and deploy it in a Docker container.
Step 5: Jenkinsfile
Here's the Jenkinsfile that we will use:
COPY
pipeline {
agent any
stages {
stage('Clone') {
steps {
sh 'git clone https://github.com/Abhiraj2310/java-junit-haellowor
}
}
stage('Build') {
steps {
sh 'mvn package'
}
}
stage('Unit Test') {
steps {
sh 'mvn test'
}
}
stage('Integration Test') {
steps {
sh 'mvn verify'
}
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 13/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
}
TABLEstage('Deploy')
OF CONTENTS{
steps {
sh 'docker build -t helloworld .'
sh 'docker run -d -p 8081:8080 helloworld'
}
}
stage('Upload Artifact') {
steps {
sh 'aws s3 cp target/*.jar s3://abhirajcicdjava/'
}
}
}
}
Upload Artifact: This stage uploads the artifact file to the S3 bucket. It uses
TABLE
the AWSOFCLICONTENTS
to upload the .jar file to the S3 bucket named "abhirajcicdjava".
Now select the Pipeline option from the menu and give a suitable name for the
pipeline. In our case lets name it java-junit-helloworld.
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 15/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
Scroll down in the configuration menu, Under the "Pipeline" section, select
"Pipeline script from SCM" as the definition.
Select "Git" as the SCM and enter the GitHub repository URL.
https://github.com/Abhiraj2310/java-junit-haelloworld.git
Edit the branch name as per the repository.
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 16/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
In the script path write Jenkinsfile, it will fetch the Jenkinsfile that we have
stored in the repository earlier and select Lightweight checkout.
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 17/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
InTABLE
the additional
OF behavior section select the clean before checkout option this
CONTENTS
instructs Jenkins to delete all existing files and directories in the workspace
before checking out the code
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 18/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
Output :
Output 1: Artifact stored in a given bucket
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 19/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 20/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
Conclusion:
TABLE OF CONTENTS
We have successfully set up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins,
GitHub, AWS EC2, Docker, and AWS S3.
The above information is up to my understanding and learning experience.
Suggestions are always welcome.
~Abhiraj kharbade
#DevOps #Jenkins #java #maven #junit #docker
Connect with me :
LinkedIn
12 2
Subscribe to my newsletter
Read articles from Abhiraj Kharbade's Blog directly inside your inbox. Subscribe to
the newsletter, and don't miss out.
Enter your email address SUBSCRIBE
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 21/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
MORE ARTICLES
TABLE OF CONTENTS
Abhiraj Kharbade
Abhiraj Kharbade
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 22/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
TABLE OF CONTENTS
Abhiraj Kharbade
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 23/24
5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 24/24