How to Create Jenkins CI/CD Pipeline to Automatic Deployment of JAVA Application on Tomcat Server

Surya Raj Ghimire
9 min readJun 23, 2023

Jenkins is an open source automation tool written in Java for creating CI/CD Pipeline. Other tools like Jenkins are Hudson, Travis, TeamCity, etc.

Jenkins CI/CD Pipeline

Configuring Jenkins Server

[root@original ~]# hostnamectl set-hostname jenmaster.srg.com
[root@original ~]# exec bash
[root@jenmaster ~]# vim /etc/hosts
---------------------------------
192.168.13.140 jenmaster.srg.com
---------------------------------
[root@jenmaster ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
---------------------------------
BOOTPROTO=static
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.13.140
NETMASK=255.255.255.0
GATEWAY=192.168.13.2
DNS1=192.168.13.2
---------------------------------
[root@jenmaster ~]# systemctl restart network
[root@jenmaster ~]# systemctl restart network
[root@jenmaster ~]# hostname
jenmaster.srg.com
[root@jenmaster ~]# hostname -I
192.168.13.140 192.168.122.1
[root@jenmaster ~]# ping google.com
  1. Install java (pre-requisite)
[root@jenmaster ~]# yum -y install java-11-openjdk
[root@jenmaster ~]# java -version

2. Set JAVA_HOME path

[root@jenmaster ~]# ls /usr/lib/jvm
java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64 jre-11 jre-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64
jre jre-11-openjdk jre-openjdk
[root@jenmaster ~]# ls /usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64
bin conf legal lib release
[root@jenmaster ~]# cd
[root@jenmaster ~]# pwd
/root
[root@jenmaster ~]# ls -a
[root@jenmaster ~]# vim .bashrc
---------------------------------
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/
export PATH=$PATH:$JAVA_HOME
---------------------------------
[root@jenmaster ~]# source .bashrc
[root@jenmaster ~]# echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/
[root@jenmaster ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/root/bin:/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/

3. Install Jenkins: download jenkins repo file from the internet and import public key

[root@jenmaster ~]# yum -y install jenkins
Last metadata expiration check: 0:44:42 ago on Fri 23 Jun 2023 04:51:12 PM +0545.
No match for argument: jenkins
Error: Unable to find a match: jenkins
[root@jenmaster ~]# ls /etc/yum.repos.d/
CentOS-Stream-AppStream.repo CentOS-Stream-Extras.repo CentOS-Stream-PowerTools.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-HighAvailability.repo CentOS-Stream-RealTime.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-Media.repo CentOS-Stream-ResilientStorage.repo
CentOS-Stream-Extras-common.repo CentOS-Stream-NFV.repo CentOS-Stream-Sources.repo
[root@jenmaster ~]# cd /etc/yum.repos.d/
[root@jenmaster yum.repos.d]# pwd
/etc/yum.repos.d
[root@jenmaster yum.repos.d]# wget https://pkg.jenkins.io/redhat/jenkins.repo
[root@jenmaster yum.repos.d]# ls
[root@jenmaster yum.repos.d]# rpm --import https://pkg.jenkins.io/redhat/jenkins.io-2023.key
[root@jenmaster yum.repos.d]# yum -y install jenkins
[root@jenmaster yum.repos.d]# rpm -q jenkins
jenkins-2.411-1.1.noarch
[root@jenmaster yum.repos.d]# yum -y update

4. Start and enable Jenkins service

[root@jenmaster ~]# systemctl start jenkins
[root@jenmaster ~]# systemctl enable Jenkins
[root@jenmaster ~]# systemctl status jenkins
[root@jenmaster ~]# netstat -tnl | grep 8080
tcp6 0 0 :::8080 :::* LISTEN

5. Start configuring Jenkins by navigating to http://your-server-ip-address:8080. Unlock the admin privileges by entering the default password created during the installation

[root@jenmaster ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
196e049c23144ff1bfc0ba3cb648de56
Getting started : Jenkins

6. Select Install suggested plugins and wait till all the plugins are successfully downloaded and installed on your machine

Getting started : customize Jenkins

7. Create a new account by entering a username, password, and email address

Getting started : create new admin account

8. Set Jenkins url

Getting started : set Jenkins url

9. Jenkins has been successfully installed and configured on your machine

Getting started : Jenkins is ready!
Dashboard [Jenkins]

10. Configure JAVA_HOME path in the Jenkins server: goto Manage JenkinsTools JDK installationsAdd JDK and then enter name and JAVA_HOME path

[root@jenmaster ~]# echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/
JDK installations

11. Install required plugins (if not already installed)
-Credentials plugin
-Github integration
-Maven invoker
-Publish over SSH
-Deploy to container

Goto Manage JenkinsPluginsAvailable plugins and then type plugin name to search and install

Jenkins install plugins

12. Generate SSH key for Jenkins user

[root@jenmaster ~]# grep jenkins /etc/passwd
jenkins:x:975:974:Jenkins Automation Server:/var/lib/jenkins:/bin/false
[root@jenmaster ~]# sudo -su jenkins
bash-4.4$ whoami
Jenkins
bash-4.4$ cd /var/lib/jenkins/
bash-4.4$ pwd
/var/lib/Jenkins
bash-4.4$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa):
Created directory '/var/lib/jenkins/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8mIxFI2tiKxfN5jzCUiShj3wxznLN/Vgb0xf2Po6ZVY jenkins@jenmaster.srg.com
The key's randomart image is:
+---[RSA 3072]----+
| .+ |
|. ..o |
|.* o o.. o |
|+.B *.. + . . o E|
|.+ = =+oS* . o . |
|. . O == = o + |
| . . *o+.. = |
| . .o. . . |
| .o |
+----[SHA256]-----+

13. Add new config file on /var/lib/jenkins/.ssh

bash-4.4$ cd /var/lib/jenkins/.ssh/
bash-4.4$ pwd
/var/lib/jenkins/.ssh
bash-4.4$ vi config
---------------------------------
Host github.com
ServerAliveInterval 60
ServerAliveCountMax 5
TCPKeepAlive yes
IPQos=throughput
---------------------------------
bash-4.4$ ls
config id_rsa id_rsa.pub

14. Add private key to SSH agent

bash-4.4$ eval `ssh-agent -s`
Agent pid 14601
bash-4.4$ pwd
/var/lib/jenkins/.ssh
bash-4.4$ ssh-add ~/.ssh/id_rsa
Identity added: /var/lib/jenkins/.ssh/id_rsa (jenkins@jenmaster.srg.com)

15. Adding a new SSH key to your GitHub account, copy the SSH public key (contents of the id_rsa.pub file) to your clipboard

bash-4.4$ ls
config id_rsa id_rsa.pub
bash-4.4$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAA… = jenkins@jenmaster.srg.com

Goto github accountSettingsSSH and GPG keysNew SSH key and then input title and paste public key from your clipboard

SSH keys

16. Check authentication from Jenkins user to the github

bash-4.4$ ssh -T git@github.com
Hi srghimire061! You've successfully authenticated, but GitHub does not provide shell access.

17. Add credentials for Jenkins on Jenkins dashboard: copy private key from id_rsa file and goto Manage JenkinsCredentialsglobalAdd Credentials → choose SSH Username with private key option on Kind dropdown → enter username jenkins → choose Enter directly option on Private Key → click on Add button and paste your private key → click on Create button

[root@jenmaster ~]# cd /var/lib/jenkins/.ssh
[root@jenmaster .ssh]# ls
config id_rsa id_rsa.pub known_hosts
[root@jenmaster .ssh]# cat id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn

-----END OPENSSH PRIVATE KEY-----
Add new credentials
Jenkins global credentials

18. Installing git on Jenkins server

[root@jenmaster .ssh]# yum -y install git
[root@jenmaster .ssh]# rpm -q git
git-2.39.3-1.el8.x86_64
[root@jenmaster .ssh]# git --version
git version 2.39.3

19. Configure Jenkins for git: goto Jenkins DashboardManage JenkinsToolsGit installations and then check the details

Git installations

20. Download and install maven on Jenkins server

[root@jenmaster ~]# ls /opt
[root@jenmaster ~]# mkdir -p /opt/maven
[root@jenmaster ~]# ls /opt
maven
[root@jenmaster ~]# cd /opt/maven/
[root@jenmaster maven]# pwd
/opt/maven
[root@jenmaster maven]# wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
[root@jenmaster maven]# ls
apache-maven-3.9.2-bin.tar.gz
[root@jenmaster maven]# tar -zxvf apache-maven-3.9.2-bin.tar.gz
[root@jenmaster maven]# ls
apache-maven-3.9.2 apache-maven-3.9.2-bin.tar.gz
[root@jenmaster maven]# cd apache-maven-3.9.2/
[root@jenmaster apache-maven-3.9.2]# ls
bin boot conf lib LICENSE NOTICE README.txt
[root@jenmaster apache-maven-3.9.2]# pwd
/opt/maven/apache-maven-3.9.2

21. Set maven home path to variable M2_HOME

[root@jenmaster apache-maven-3.9.2]# cd
[root@jenmaster ~]# pwd
/root
[root@jenmaster ~]# ls -a
[root@jenmaster ~]# vim .bashrc
---------------------------------
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export M2_HOME=/opt/maven/apache-maven-3.9.2
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/
export PATH=$PATH:$JAVA_HOME:$M2_HOME/bin
---------------------------------
[root@jenmaster ~]# . .bashrc
[root@jenmaster ~]# echo $M2_HOME
/opt/maven/apache-maven-3.9.2
[root@jenmaster ~]# echo $PATH
[root@jenmaster ~]# which mvn
/opt/maven/apache-maven-3.9.2/bin/mvn

22. Specify maven path in the Jenkins server

[root@jenmaster ~]# echo $M2_HOME
/opt/maven/apache-maven-3.9.2

goto Jenkins DashboardManage JenkinsToolsMaven installationsAdd Maven and then enter Maven on name field, uncheck Install automatically checkbox and paste $M2_HOME variable path on MAVEN_HOME field → click Save button

Maven installations

23. Creating testing job to verify whether Jenkins server is working properly or not

goto Jenkins Dashboard
→ New Item
→ Enter an item name: testjob
→ Choose Freestyle project → OK
→ Description (optional): Test Job
→ Build Steps → choose Execute shell
→ Enter command: echo “Test job to verify Jenkins setup” → Save
→ Click Build Now
→ Build History → Detail → Console Output

Jenkins test job

24. Default location of jobs

[root@jenmaster ~]# cd /var/lib/jenkins/workspace/
[root@jenmaster workspace]# pwd
/var/lib/jenkins/workspace
[root@jenmaster workspace]# ls
testjob

25. Main configuration file in Jenkins

[root@jenmaster workspace]# vim /etc/sysconfig/jenkins

26. Default location of Jenkins plugins

[root@jenmaster ~]# cd /var/lib/jenkins/plugins/
[root@jenmaster plugins]# ls
[root@jenmaster plugins]# pwd
/var/lib/jenkins/plugins

Creating a CI/CD Pipeline on the Jenkins Server to Deploy Java Web App on the Tomcat Server

  1. Configure tomcat server
[root@tomcatsrv ~]# hostname -I
192.168.13.135 192.168.122.1
[root@tomcatsrv ~]# netstat -tnl | grep 8090
tcp6 0 0 :::8090 :::* LISTEN
[root@tomcatsrv ~]# cd /opt/apache-tomcat-10.1.10/webapps/
[root@tomcatsrv webapps]# ls
docs examples host-manager manager ROOT

2. Login to the Jenkins

Sign in to Jenkins

3. Click on new item

4. Enter and item name and click on OK button

5. Configure general tab

6. Source code management
→ Choose git
→ Repository URL: git@github.com:srghimire061/testjava.git (github repo SSH url)
→ Credentials: Choose Jenkins (jenkins user)

Github repository
Source Code Management

7. Build Triggers
→ Choose Poll SCM checkbox
→ Schedule: * * * * * (cron expression)

Build triggers

8. Build Steps
→ Add build step
→ Choose Invoke top-level Maven targets option
→ Maven Version: select Maven
→ Goals: package

Build Steps

9. Post-built Actions
→ Add post-built action
→ Choose Deploy war/ear to a container option
→ WAR/EAR files: target/*.war
→ Containers à Add Container → Choose Tomcat 9.x Remote
→ Credentials: Click Add button → Click Jenkins
→ Jenkins Credentials Provider: Add Credentials
→ Kind: Username with password
→ Username: surya
→ Password: S3ry@!
→ ID: surya
→ Description: Tomcatuser
→ Credentials: Choose surya/****** (Tomcatuser)
→ Tomcat URL: http://192.168.13.135:8090

Add Jenkins Credentials
Post-build Actions
Post-build Actions

10. To view build details: click on build history section’s job id and console output


Finished: SUCCESS

11. Accessing to the application from end user: http://192.168.13.135:8090/test/

12. Modify source code from developer machine and push to the github repo

[surya@developer ~]$ cd /home/surya/javaproj/test/
[surya@developer test]$ pwd
/home/surya/javaproj/test
[surya@developer test]$ ls
file1 pom.xml README.md src target
[surya@developer test]$ vim src/main/webapp/index.jsp
[surya@developer test]$ cat src/main/webapp/index.jsp
<html>
<body>
<h2>Deploying Second Version of Java Web Application on Tomcat Server Using CI/CD Pipeline...</h2>
</body>
</html>
[surya@developer test]$ git add -A
[surya@developer test]$ git commit -m "second version"
[master e508937] second version
1 file changed, 1 insertion(+), 1 deletion(-)
[surya@developer test]$ git push origin master
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), 1.17 KiB | 599.00 KiB/s, done.
Total 12 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To github.com:srghimire061/testjava.git
5238071..e508937 master -> master
[surya@developer test]$ date
Sat Jun 24 01:00:21 +0545 2023

--

--