DevOps Project — CI/CD -1

jabir ahammed
7 min readAug 8, 2023

--

During this DevOps project, we are going to deploy our application on three different kind of target environment:-

1- Tomcat Server

2- Docker Container

3- Kubernetes

Initially, we are going to build and deploy our application on Tomcat server.

For this, we are going to set up CI/CD pipeline with the help of GitHub Jenkins Maven, and Tomcat.

Github : https://github.com/jabir000/hello-world.git

Setup CI/CD with Github, Jenkins, Maven and Tomcat

1- Setup Jenkins

2- Setup and configure Maven and Git

3- Setup Tomcat Server

4- Integrate Github, Maven, Tomcat Server with Jenkins

5- Create CI/CD job

6- Test the deployment

To initiate the project, the first step involves setting up a Jenkins server.

connect the instance to the terminal

sudo su -
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
amazon-linux-extras install epel
amazon-linux-extras install java-openjdk11
yum install jenkins
systemctl status jenkins
systemctl enable --now jenkins

Login to jenkins server using public ip of jenkins server through port 8080

need to get the Administrator Password from /var/lib/jenkins/secrets/initialAdminPassword

cat /var/lib/jenkins/secrets/initialAdminPassword

Successfully set up Jenkins server and logged in.

Next, proceed with installing Git on the Jenkins Server.

yum install git
git -v

Next step is to integrate git with jenkins.

Jenkins -> Dashboard -> Manage Jenkins -> Plugins -> Available ->

Search for GitHub plugin and install it.

Next step is to Configure Jenkins.

Jenkins -> Manage Jenkins -> Tools -> Git ->

Give a name and type “git” in the Path to executable.

Apply -> Save.

Successfully integrated Github with Jenkins.

Next step is to install Maven on the Jenkins server.

cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
tar -xvzf apache-maven-3.9.4-bin.tar.gz
mv apache-maven-3.9.4 maven
cd maven/bin
./mvn -v
cd ~
vim .bash_profile
M2_HOME=/opt/maven
M2=/opt/maven/bin
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.amzn2.0.1.x86_64
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2
(save it)
source .bash_profile
echo $PATH

Now we can execute maven execute from anywhere.

mvn -v

Need to install maven plugins in jenkins.

Jenkins -> Manage Jenkins -> Plugins -> Available ->

search for Maven Integration -> install

Lets configure.

Manage Jenkins -> Tools -> JDK -> Add JDK -> give name and enter Java home path -> /usr/lib/jvm/java-11-openjdk-11.0.19.0.7–1.amzn2.0.1.x86_64

Maven -> Add Maven -> give name -> enter Maven home path -> /opt/maven

Apply -> Save.

Successfully configured and integrated Maven with Jenkins.

Now, proceed to set up the target environment. For this project, the chosen target environment is the Tomcat server.

1- Setup Tomcat Server.

Connect to the terminal.

sudo su -
amazon-linux-extras install java-openjdk11
java --version
cd /opt
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.78/bin/apache-tomcat-9.0.78.tar.gz
tar -xvzf apache-tomcat-9.0.78.tar.gz
mv apache-tomcat-9.0.78 tomcat
cd tomcat/bin
./startup.sh

Lets access the tomcat server using public ip through port 8080.

cd /opt/tomcat
find / -name context.xml
vim /opt/tomcat/webapps/host-manager/META-INF/context.xml
vim /opt/tomcat/webapps/manager/META-INF/context.xml

From both of the files comment these lines

 cd /opt/tomcat/bin
./shutdown.sh
./startup.sh

Again access the tomcat server using public ip through port 8080 and go to manager app.

Then it will be asking for credentials.

cd /opt/tomcat/conf
vim tomcat-users.xml

Now update file from this

to this

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
<user username="deployer" password="deployer" roles="manager-script"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>

Now lets goto command line and create link files for tomcat startup.sh and shutdown.sh

ln -s /opt/tomcat/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/tomcat/bin/shutdown.sh /usr/local/bin/tomcatdown
echo $PATH
tomcatdown
tomcatup

Now we can access the tomcat server manager app using the credentails that we have updated in the tomcat-users.xml file

username : tomcat

password: s3cret

Lets Integrate Tomcat with Jenkins.

Jenkins ->Manage Jenkins -> Plugins -> Available -> search for Deploy to container and install it

Lets Configure.

Jenkins -> Manage Jenkins -> Credentials -> System -> Global Credentials -> Add Credentials -> Lets add the credentials that we have updated.

kind: Username with password

username : deployer

password: deployer

id: tomcat_deployer

descripton: tomcat_deployer

Click Create.

Lets create the job for deploying our code to Tomcat server.

Jenkins -> Dashboard -> New Item -> give a name -> select Maven Project -> Click OK -> Give a description -> Source Code Management -> Select Git -> Enter the Github url ( https://github.com/jabir000/hello-world.git )

Goto Build section -> Goals and options : clean install

Goto Post-build Actions section -> Add post build action → select Deploy war/ear to a container -> WAR/EAR Files : **/*.war -> Add Container -> Tomcat 8 x Remote -> select the Credentials -> Tomcat URL : http://<public-ip>:8080 -> Apply -> Save

Now lets build a job manually

Now lets enable automatic build triggering by configuring the system to initiate builds whenever changes occur in the source code.

Jenkins -> select the job -> Configure -> Source code management -> Build Triggers -> Select Poll SCM -> enter this ( * * * * * ) -> Apply -> Save.

Now lets clone Github Repository and make some changes in source code.

Open GitBash

git clone https://github.com/jabir000/hello-world.git
cd hello-world/webapp/src/main/webapp/
vim index.jsp

Lets make some changes.

changed this

into this

save it.

git status
git add .
git status
git commit -m "colour changed"
git push origin master

Goto the Jenkins server.

Now we can see the job is triggered automatically.

Next, verify that modifications in the source code are appropriately reflected in the Tomcat Server.

In this section, we have successfully deployed the application to the Tomcat server and enabled CI/CD. If any changes occur in the source code, they will be automatically built and deployed to the server.

In the next session, we will be deploying this application to a Docker container.

Linkedin : www.linkedin.com/in/jabir-ahammed

THANK YOU !

--

--