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.
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:
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.
2. Click on the “Create bucket” button.
3. In the “Bucket name” field, enter “nivascicd”.
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:::nivascicd/*"
]
}
]
}
9.Click on the “Save” button to save the policy.
Step 3: Install Jenkins on the EC2 Instance
Step 4: Install Docker on the EC2 Instance
Step 5: Install Maven on the EC2 Instance
Step 6: Jenkinsfile
Here’s the Jenkinsfile that we will use:
pipeline {
agent any
stages {
stage('Clone') {
steps {
git branch: 'main', url: 'https://github.com/nivas-22/java-junit.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 8082:8080 helloworld'
}
}
stage('Upload Artifact') {
steps {
// Using AWS credentials securely from Jenkins credentials store
withCredentials([aws(credentialsId: 'aws-credentials')]) {
sh 'aws s3 cp target/helloworld-servlet-1.0-SNAPSHOT.jar s3://nivascicd/'
}
}
}
}
}
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 “nivascicd”.
Step 7: 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.
- 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/nivas-22/java-junit.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 thenSave
- Our pipeline is now created.
- Now click the build option; this will run the pipeline in Jenkinsfile.
Output :
Artifact stored in a given bucket
ERROR :
I made this document unclear because if you face errors, then you will learn more ..!
1. Install Docker and Jenkins in the Ec2 machine.
2. Give Public access to the S3 bucket and attach the policies.
3. Install AWS CLI in the EC2 server & Add the AWS plugin in the Jenkins for Credentials.
Hope it helps..!
Thank you for reading so far! Before you go:
- 👏 Clap for the story if it helped :)
- 📰 View more content from me https://medium.com/@clouddevsecops