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.
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.
In this blog post, we will discuss how to set up a CI/CD pipeline using Java, Maven, JUnit, Jenkins, GitHub, AWS 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:
Log in to your AWS Management Console.
Navigate to the EC2 Dashboard.
Click on the "Launch Instance" button.
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.
Choose the instance type as "t2.medium".
Click on the "Next: Configure Instance Details" button.
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.
Click on the "Next: Add Storage" button.
On the "Add Storage" page, you can leave the default settings or adjust them as needed. You can add additional EBS volumes if necessary.
Click on the "Next: Add Tags" button.
On the "Add Tags" page, you can add tags to your instance for easy identification and management.
Click on the "Next: Configure Security Group" button.
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.
Click on the "Review and Launch" button.
On the "Review Instance Launch" page, review your settings and make any necessary changes.
Click on the "Launch" button.
Choose an existing key pair or create a new one to use for SSH access.
Click on the "Launch Instances" button.
Your EC2 instance of t2.medium and Ubuntu 22.04 LTS is now being launched.
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:
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
We will use AWS S3 to store our artifact file. Create an S3 bucket named "abhirajcicdjava" in your AWS account.
Steps to create an S3 bucket with the name "abhirajcicdjava" and make it public:
Navigate to the S3 service.
Click on the "Create bucket" button.
In the "Bucket name" field, enter "abhirajcicdjava".
Leave the other settings at their default values and click on the "Create bucket" button.
Once your bucket is created, select it from the list of buckets.
Click on the "Permissions" tab.
Click on the "Bucket Policy" button.
In the "Bucket policy editor" window, paste the following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::abhirajcicdjava/*"
]
}
]
}
Click on the "Save" button to save the policy.
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
Step 3: Install Jenkins on the EC2 Instance
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:
- Update the package list:
sudo apt-get update
- Install Docker.io:
sudo apt-get install docker.io
- Start and enable the Docker service:
sudo systemctl start docker
sudo systemctl enable docker
- Verify that Docker is installed correctly by running the "hello-world" image:
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 :
Update the package list by running the following command:
sudo apt update
Install Maven by running the following command:
sudo apt install maven
After the installation is complete, verify the Maven version by running the following command:
mvn -version
This command should display the Maven version and other details.
That's it! You have now installed Maven on your system.
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:
pipeline {
agent any
stages {
stage('Clone') {
steps {
sh 'git clone https://github.com/Abhiraj2310/java-junit-haelloworld.git'
}
}
stage('Build') {
steps {
sh 'mvn package'
}
}
stage('Unit Test') {
steps {
sh 'mvn test'
}
}
stage('Integration Test') {
steps {
sh 'mvn verify'
}
}
stage('Deploy') {
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
Upload Artifact: This stage uploads the artifact file to the S3 bucket. It uses the AWS CLI 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.
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.
Edit the branch name as per the repository.
- In the script path write Jenkinsfile, it will fetch the Jenkinsfile that we have stored in the repository earlier and select Lightweight checkout.
- In the additional behavior section select the clean before checkout option this instructs Jenkins to delete all existing files and directories in the workspace before checking out the code
after finishing the configuration click
Apply
and thenSave
Our pipeline is now created.
Now click the build now option this will run the pipeline in Jenkinsfile.
Output :
Output 1: Artifact stored in a given bucket
Output 2: Hello world! is shown when accessing the localhost:8081
Conclusion:
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 :