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

A project for practicing ci/cd.

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:

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 Dashboard.

  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.

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

  6. Click on the "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.

  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.

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

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:

  1. Navigate to the S3 service.

  2. Click on the "Create bucket" button.

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

  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:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "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

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:

  1. Update the package list:
sudo apt-get update
  1. Install Docker.io:
sudo apt-get install docker.io
  1. Start and enable the Docker service:
sudo systemctl start docker
sudo systemctl enable docker
  1. 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 :

  1. Update the package list by running the following command:

     sudo apt update
    
  2. Install Maven by running the following command:

     sudo apt install maven
    
  3. After the installation is complete, verify the Maven version by running the following command:

     mvn -version
    
  4. This command should display the Maven version and other details.

  5. That's it! You 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:

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.

    https://github.com/Abhiraj2310/java-junit-haelloworld.git

  • 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 then Save

  • 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 :

LinkedIn