Automated Build and Deployment of Maven Web-app


Project Architecture

Screenshot 2024-07-23 153635

Project Preview.

My.Video.mp4
My.Video-1.mp4

~ By default the Tomcat will run on Port No. 8080

~ By default the Jenkins will run on Port No. 8080

~ Here we have to change the Port No. of Tomcat to 9090.

Tasks:


======== Part 1

  1. Create an EC2 Instance - Connect to the instance using MobaXTerm Tool.

image

======== Part 2

JENKINS INSTALLATION ON LINUX EC2 INSTANCE ~ Add Jenkins repo. to our YUM repo: sudo wget -O /etc/yum.repos.d/jenkins.repo
https://pkg.jenkins.io/redhat-stable/jenkins.repo

~ Import a key-file from Jenkins-CI to enable the installation from package: sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

    sudo yum upgrade

~ Installing Java sudo amazon-linux-extras install java-openjdk11 -y

    java -version

image

~ Install Jenkins: sudo yum install jenkins -y image

~ Lets start, enable, and check the status of Jenkins sudo systemctl enable jenkins
sudo systemctl start jenkins sudo systemctl status jenkins You should see Active & Running in Green Colour, which means the Jenkins Tool is successfully installed

image

~ Check the jenkins accessibility. By default the Jenkins will run on Port No. 8080. So, open Port No. 8080 for EC2 instance to access Jenkins "Check" the instance ----> Security ----> Click on the link under SGs ----> Add Rules ----> Custom TCP, 8080, Anywhere ----> Save Rules.

Open new tab: :8080 ----> You will see the Jenkins Home Page ----> Now we have to Unlock the Jenkins ----> You will see a code in the red colour ----> Copy the code in red colour ----> Go to MobaXTerm ----> sudo cat ----> You will see a password. Copy that password and paste in jenkins home page ----> Click on suggested plugins ----> You will see "Create First Admin User" ----> Enter Username, Password, Name, Email Id ----> Save and Continue ----> Jenkins URL:http://52.14.0.208:8080/ ----> Save and Finish ----> You will see "Jenkins is ready!" ----> Click on "Start using jenkins" ----> Now you can see the jenkins home page and here you can create Jenkins Jobs.

image

Welcome to jenkins Dashboard!!!

image

======== Part 3

TOMCAT WEBSERVER INSTALLATION ON LINUX EC2 INSTANCE

https://tomcat.apache.org/download-90.cgi ----> Downloads ----> Tomcat 9 ----> Copy the link of tar.gz file ----> https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.73/bin/apache-tomcat-9.0.73.tar.gz ----> sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.73/bin/apache-tomcat-9.0.73.tar.gz ----> ls -l ----> You will the tomcat file in red colour. This is a tar.gz file and we have to extract the tar.gz file ----> tar -xvf ----> All the files will get extracted ----> ls -l ----> You will see the list of extracted files

Lets start the tomcat server:

Inorder to start the tomcat server, we need a file called "starup.sh" This "startup.sh" file will be there in "bin" folder ----> cd bin ----> ls -l ----> you will see startup.sh file ----> ./startup.sh ----> You will see "Tomcat Started"

Lets access the tomcat server: Since, the tomcat also runs on Port No. 8080, we cannot access tomcat as on the same port no. jenkins is also running. Now, we have to change the Port No. of Tomcat. (Tomcat Port No. 9090)

How to change the port no. of Tomcat?

In order to change the port no. of Tomcat, we will configure a file known as "server.xml" This server.xml file will be available in "conf" folder.

cd .. ----> pwd ----> /home/ec2-user/apache-tomcat-9.0.73 ----> ls -l ----> cd conf ----> ls -l ----> you will see server.xml file ----> vi server.xml ----> scroll down till you see "connector port=8080" ----> press "i" ----> Change the port no to 9090 instead of 8080 ----> escape ----> :wq

Check the Tomcat accessibility. By default the Tomcat will also run on Port No. 8080. But on the same port no. jenkins is also running. So we have to add the port no. to 9090 in ec2 instance to access Tomcat: "Check" the instance ----> Security ----> Click on the link under SGs ----> Add Rules ----> Custom TCP, 9090, Anwhere ----> Save Rules. Shutdown the tomcat server ----> cd bin ----> ./shutdown.sh ----> ./startup.sh

Open new tab: :9090 ----> You will see the Tomcat Home Page Go to browser and check for tomcat accessibility. You are now able to access Tomcat and Jenkins in the same instance.

Welcome to Tomcat server!!!

image

Here we can access only the Home page of Tomcat. Inorder to access complete tomcat server, we need to do the configuration in "context.xml" file. apache-tomcat-9.0.73/webapps/manager/META-INF/context.xml

cd apache-tomcat-9.0.73/webapps/manager/META-INF/context.xml ----> vi context.xml ----> You will see a "valve" tag. You have to edit "Allow" tag ----> ".*" />

Configuring the Users in Tomcat

we need to edit the tomcat-users.xml to configure the users. The "tomcat-users.xml" file is available in "conf" folder

cd bin ./shutdown.sh ./startup.sh

Access Tomcat on Browser ----> Click on Server Status ----> Enter the username (admin) and password (admin)

Now we can access manager and all others page of tomcat serever.!!!

image

Install GIT in EC2 Instance where Jenkins is running sudo yum install git -y

Jenkins Port No.: 8080 Tomcat Port No.: 9090

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Steps to create the Jenkins job with GITHUB Repo. + Maven + Tomcat Server: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Connect to the Tomcat Server Connect to the Jenkins Software

Inorder to do the automation, Jenkins is going to execute some script to deploy the war file into the Tomcat Webserver.


Step 1: Adding the manager-script role in tomcat-users.xml file


In order to do the automation, Tomcat user should have a "manager-script" permission. There should be one user in the Tomcat with the "manager-script" permission. Then with that user credentials we can automate the deployment process.

Goto MobaXTerm and Connect to the Instance ----> ls -l ----> cd apache-tomcat-9.0.73 ----> ls -l ----> cd conf ----> ls -l ----> You can see tomcat-users.xml ----> vi tomcat-users.xml


Step 2: Installing "deploy to container" plugin in Jenkins software


Jenkins Dashboard ----> Manage Jenkins ----> Mange Plugins ----> Available Plugins ----> In the search bar, type deploy to container and check the same ----> Click on "Install without restart" ----> Click on "Go back to the top page.


Step 3: Creating a Jenkins job and for that Jenkins job we will give permission to deploy

#Configure the Jenkins and it's all settings.

image

Successfully Build

image

#Successfully Deployed Maven project

image

After Changes!!!

image


Add JDK, GIT, MAVEN in Jenkins Browser Page by going into Global Tools Configuration