Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Project - Setting Up A CI - CD Pipeline Using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Project - Setting Up A CI - CD Pipeline Using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker, and AWS S3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

5/7/23, 9:05 PM Project: Setting up a CI/CD Pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS EC2, Docker,

, AWS EC2, Docker, and AWS S3

TABLE OF CONTENTS

Abhiraj Kharbade's Blog


Follow

Project: Setting up a CI/CD Pipeline using Java,


Maven, JUnit, Jenkins, GitHub, AWS EC2,
Docker, and AWS S3
A project for practicing ci/cd.

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

May 7, 2023 · 7 min read


TABLE OF CONTENTS

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.

2. Navigate to the EC2 Dashboard.


https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 3/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

3. Click on the "Launch Instance" button.

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

5. Choose the instance type as "t2.medium".


6. TABLEonOF
Click theCONTENTS
"Next: Configure Instance Details" button.
7. On the "Configure Instance Details" page, you can leave the default settings or
adjust them as needed. You can choose the VPC, subnet, IAM role, and other
settings.
8. Click on the "Next: Add Storage" button.
9. On the "Add Storage" page, you can leave the default settings or adjust them
as needed. You can add additional EBS volumes if necessary.
10. Click on the "Next: Add Tags" button.
11. On the "Add Tags" page, you can add tags to your instance for easy
identification and management.
12. Click on the "Next: Configure Security Group" button.
13. On the "Configure Security Group" page, you can create a new security group
or use an existing one. Make sure to open Port 22 for SSH access, Port 8080
for Jenkins, Port 8081 for Docker container.

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

14. Click on the "Review and Launch" button.


15. On the "Review Instance Launch" page, review your settings and make any
necessary changes.
16. Click on the "Launch" button.
17. Choose an existing key pair or create a new one to use for SSH access.
18. Click on the "Launch Instances" button.
Your EC2 instance of t2.medium and Ubuntu 22.04 LTS is now being launched.

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

Step 2: Create a s3 bucket

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.

2. Click on the "Create bucket" button.


3. In the "Bucket name" field, enter "abhirajcicdjava".

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. Click on the "Save" button to save the policy.


2. Your S3 bucket is now public, and anyone can access its objects by using the
URL https://s3.amazonaws.com/abhirajcicdjava/ .
Note: Making your S3 bucket public can have security implications. Make sure you
understand the risks before doing this.

You can follow the AWS documentation to create an S3 bucket:


https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 10/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

Step 3: Install Jenkins on the EC2 Instance


TABLE OF CONTENTS
We will install Jenkins on our EC2 instance. You can follow the official
documentation to install Jenkins: https://www.jenkins.io/doc/book/installing/
Note: Install the "AWS SDK" plugin in Jenkins

Step 4: Install Docker on the EC2 Instance


To install the Docker.io , you can follow these steps:
1. Update the package list:
COPY

sudo apt-get update

1. Install Docker.io:
COPY

sudo apt-get install docker.io

1. Start and enable the Docker service:


COPY

sudo systemctl start docker


sudo systemctl enable docker

1. Verify that Docker is installed correctly by running the "hello-world" image:


https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 11/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

COPY

TABLE OF CONTENTS
sudo docker run hello-world

This should download the "hello-world" image and run it in a container. If


everything is working correctly, you should see a message indicating that Docker is
running and configured correctly.

Step 4: Install Maven on the EC2 Instance


The steps to install Maven :
1. Update the package list by running the following command:
COPY

sudo apt update

2. Install Maven by running the following command:


COPY

sudo apt install maven

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/'
}
}
}
}

Let's go through each stage in detail.


Clone: This stage clones the code from the GitHub repository. It uses the
GitSCM plugin to retrieve the code from the main branch of the repository.
Build: This stage builds the code using Maven. It runs the "mvn package"
command to compile the code, run tests, and package the application into a
.jar file.
Test: This stage runs the JUnit tests using Maven. It runs the "mvn test"
command to execute the JUnit tests and verify that the code is working
correctly.
Deploy: This stage deploys the application in a Docker container. It uses the
Docker build command to build the Docker image and the Docker run
command to start the container. The application will be accessible at
http://<EC2 Public IP Address>:8081
https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 14/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

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".

Step 6: Running the Pipeline


Creating a Jenkins Pipeline Now that we have installed Jenkins, we can create
a pipeline that will automate the CI/CD process.
Go to your Jenkins dashboard and click + New Item

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

after finishing the configuration click Apply and then Save


Our pipeline is now created.
Now click the build now option this will run the pipeline in Jenkinsfile.

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

Output 2: Hello world! is shown when accessing the


localhost:8081

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

Jenkins Devops maven Java projects

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

Step-by-Step Guide to Deploying a Flask and MongoDB


Microservices Project on Kubernetes with Kubeadm
Kubeadm Installation: Setting up a Kubernetes Cluster Kubernetes is an open-source
container orchest…

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

A Cheat Sheet of Essential Commands for Managing and


Debugging Your Kubernetes Cluster's Networking
Kubernetes is a popular container orchestration platform that allows you to manage
containerized app…

Keeping Your Kubernetes Cluster Healthy: Best Practices


for Upgrading, Backing Up, and Scaling
Kubernetes has become the go-to container orchestration tool for managing and
scaling containerized …

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

TABLE OF CONTENTS©2023 Abhiraj Kharbade's Blog


Archive · Privacy policy · Terms

Publish with Hashnode

Powered by Hashnode - Home for tech writers and readers

https://abhirajdevops.hashnode.dev/project-setting-up-a-cicd-pipeline-using-java-maven-junit-jenkins-github-aws-ec2-docker-and-aws-s3 24/24

You might also like