{{announcement.body}}
{{announcement.title}}

Deploy Application Using Jenkins, Tomcat Server, and PollSCM

DZone 's Guide to

Deploy Application Using Jenkins, Tomcat Server, and PollSCM

In this guide, we are going to use Docker to configure Jenkins and Tomcat to achieve Continuous Integration/Continuous Deployment.

· Cloud Zone ·
Free Resource

In this guide, we are going to use Docker to configure Jenkins and Tomcat to achieve Continuous Integration/Continuous Deployment.

We will install and set up Tomcat and Jenkins using Docker, and thereafter we are going to create a Jenkins Job to deploy your Java application over the Tomcat server.

This guide is assuming that you already have basic knowledge of Docker and Docker is up and running on your host machine. So let’s begin with the guide.

Jenkins Set up

You can spin up a Docker container using the Jenkins image.

Shell
 




x


 
1
$ docker container run -dit -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /data/jenkins/jenkins_home:/var/jenkins_home --name jenkins_container jenkins/jenkins


We are running Jenkins on port 8080. You can see the running container here: 

Shell
 




xxxxxxxxxx
1


 
1
$ docker ps
2
 
3
CONTAINER ID    IMAGE                COMMAND         CREATED       STATUS                PORTS                        NAMES
4
ce46107c9f83    jenkins/jenkins:lts  "/sbin/tini”    Up 1 minutes  Up 1 minutes             0.0.0.0:8080->8080/tcp       jenkins_container


To extract the Jenkins password from Jenkins’s container, you can run below-mentioned command:

Shell
 




xxxxxxxxxx
1


 
1
$ docker exec -it jenkins_container cat /var/jenkins_home/secrets/initialAdminPassword
2
 
3
gSzHTGfbtXSpcBXMyJ6gzmNfNH7BjhK23


Copy the admin password from your terminal and open http://your-ip-addr:8080 in your preferred browser. Next, run the initial setup wizard and select Recommended Plugins. Jenkins will install all the necessary plugins.

If everything goes well, you will land on the Jenkins’ dashboard area, which looks something like this:

Tomcat Set up

Now, it’s time to spin the Tomcat container. For this, we are going to use CentOS, which will enable us to set up a customized Tomcat server. You can also use tomcat image as per your choice, however for this guide we are going to stick to the plan. So let's start by pulling the latest Centos image from Docker Hub:

Shell
 




xxxxxxxxxx
1


1
$ docker pull centos
2
 
3
Using default tag: latest
4
latest: Pulling from library/centos
5
8a29a15cefae: Pull complete 
6
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
7
Status: Downloaded newer image for centos:latest
8
docker.io/library/centos:latest


To run the container from the pulled image in detached mode, use the command below: 

Shell
 




xxxxxxxxxx
1


 
1
$ docker run -dit --name tomcat_container -p 8888:8080 centos


As Jenkins has already occupied port 8080 on the machine, here we have opened port 8888 for Tomcat in spite the default port being 8080.

To install Tomcat in your Centos container, follow the steps:

Shell
 




xxxxxxxxxx
1


 
1
$ docker exec -it eca291a36127 /bin/bash 


Go to /opt directory, in your tomcat_container

Shell
 




xxxxxxxxxx
1


1
[root@eca291a36127 /]# cd opt/


Open https://tomcat.apache.org/download-80.cgi on your browser. Find “Core” and download the Tomcat binary. At the time of writing this article, the current version is 8.5.50

Shell
 




xxxxxxxxxx
1


 
1
[root@eca291a36127 /]# yum install wget
2
[root@eca291a36127 /]# wget http://apachemirror.wuchna.com/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz


Extract the tar file: 

Shell
 




xxxxxxxxxx
1


 
1
[root@eca291a36127 /]# tar -xvzf apache-tomcat-8.5.50.tar.gz
2
[root@eca291a36127 /]# cd apache-tomcat-8.5.50/bin/


Here you are going to find two shell script files named startup.sh and shutdown.sh, assign “execute” permission to both. Both these files allow us to start and stop the Tomcat server, within our container: 

Shell
 




xxxxxxxxxx
1


1
[root@eca291a36127 bin]# chmod +x shutdown.sh
2
[root@eca291a36127 bin]# chmod +x startup.sh


To run the Tomcat server, Java is required. In order to do so, run following command to satisfy the requirement. Here we are going to install OpenJDK 7 using Yum:

Shell
 




xxxxxxxxxx
1


1
[root@eca291a36127 /]# yum install java-1.7.0-openjdk-devel


It’s time to run our Tomcat server; use startup.sh file to do so: 

Shell
 




xxxxxxxxxx
1


 
1
[root@eca291a36127 /]# ./startup.sh 
2
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.50
3
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.50
4
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.50/temp
5
Using JRE_HOME:        /usr
6
Using CLASSPATH:       /opt/apache-tomcat-8.5.50/bin/bootstrap.jar:/opt/apache-tomcat-8.5.50/bin/tomcat-juli.jar
7
Tomcat started.


In your terminal, you can see the message Tomcat started. Now, go to your browser; Tomcat should be accessible at http://your-ip-addr:8888.

In case you want to access the Manage App, you will have to do some additional settings, as by default, the Manager App section can only be accessed from the local system (i.e., 127.0.0.1).
To overcome this, you will need to make some changes to context.xml file. To do so follow the below-mentioned steps:

Shell
 




xxxxxxxxxx
1


 
1
$ docker exec -it tomcat_container /bin/bash


Now, find the context.xml file: 

Shell
 




xxxxxxxxxx
1


 
1
$ find / -name context.xml
2
 
3
/opt/apache-tomcat-8.5.50/conf/context.xml
4
/opt/apache-tomcat-8.5.50/webapps/host-manager/META-INF/context.xml
5
/opt/apache-tomcat-8.5.50/webapps/manager/META-INF/context.xml


Edit the file that resides in webapps directory i.e. /opt/apache-tomcat-8.5.50/webapps/host-manager/META-INF/context.xml, find the Value class name org.apache.catalina.valves.RemoteAddrValve and comment it: 

Shell
 




xxxxxxxxxx
1


 
1
...
2
<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
3
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
4
... 


Let's do the same thing on the other file as well, i.e., /opt/apache-tomcat-8.5.50/webapps/manager/META-INF/context.xml.

To access the default username and password through the Manager App section, go to conf/tomcat-users.xml and make the following changes:

Shell
 




xxxxxxxxxx
1


 
1
$ /opt/apache-tomcat-8.5.50/conf && nano tomcat-users.xml


Now, as part of your pipeline, you will have to add a user and define roles on your Tomcat server. To copy the file from the remote machine (in our case, the Jenkins server), you will need to define the role Manager Script, copy the lines and add it to the end of the file just before the closing tag </tomcat-users>

Shell
 




xxxxxxxxxx
1
10


1
... 
2
<role rolename="manager-gui"/>
3
    <role rolename="manager-script"/>
4
    <role rolename="manager-jmx"/>
5
    <role rolename="manager-status"/>
6
    <user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
7
    <user username="deployer" password="deployer" roles="manager-script"/>
8
    <user username="tomcat" password="s3cret" roles="manager-gui"/>
9
</tomcat-users>
10
...


Since you have made configuration changes in the Tomcat server, you will need to restart the server for the changes to take effect. 

Shell
 




xxxxxxxxxx
1
10


 
1
$ cd /opt/apache-tomcat-8.5.50/bin/
2
$ ./shutdown.sh
3
$ ./startup.sh 
4
 
5
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.50
6
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.50
7
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.50/temp
8
Using JRE_HOME:        /usr
9
Using CLASSPATH:       /opt/apache-tomcat-8.5.50/bin/bootstrap.jar:/opt/apache-tomcat-8.5.50/bin/tomcat-juli.jar
10
Tomcat started.


You can now access the Tomcat Web Application Manager dashboard; username and password are tomcat and s3cret, respectively.

Let us move to the next part of the tutorial.

Configure Jenkins

It’s time to set up Maven and an additional plugin so that Jenkins can connect to Tomcat container over SSH. For this, we are going to install Maven Integration and Publish over SSH plugin in Jenkins.

Login to your Jenkins environment and go to Dashboard > Manage Jenkins > Manage Plugins > Available, as shown below:

For Maven Integration:

For Publish over SSH:

Post-installation, you need to configure SSH server details so that Jenkins can connect the Tomcat container. In Jenkins, go to Dashboard > Manage Jenkins > Configure System and find SSH Servers. Enter details as shown below:

Test your connection before saving the details. For the sake of this article, we have used the password authentication method, however, it is not recommended in a production environment. We recommend you use a SSH key instead.

Deploying WAR file on Tomcat Server using Jenkins

As you are through with the configuration part, now you can proceed to create a Jenkins Job.

For this simple job, you would need to use Freestyle Project. Create a new item, as shown below:

SCM
Go to the Source Code Management (also known as SCM), and select Git. Add your Github or GitLab URL in Repository URL field:

Poll SCM
In the Build Triggers section, select Poll SCM. It periodically polls the VCS to check if any changes were made. During the poll, if any changes were detected in repo since the last build, a new build is initiated.

Let's say you want to poll SCM every 30 minutes, then in the text field, add H/30 * * * * (i.e., every 30 minutes, every hour, every day, every month, and every day of the week).

Post-Build Step
Go to Post-build Actions section and from the drop-down menu, select Send build artifacts over SSH, as shown below:

In the Source files field, mention the file name that is going to be uploaded to the server. In case you are not sure about the file name, you can use the pattern as mentioned above **/*.war.

Other than this, make sure that you mention the Remote Directory; in our case, it is /root/tomcat. Whereas, for Exec command, as you are using Docker for your Tomcat server, use the below-mentioned commands:

Shell
 




xxxxxxxxxx
1


 
1
docker cp ~/tomcat/hello-world.war tomcat_container:/opt/apache-tomcat-8.5.50/webapps
2
 
          
3
docker exec -i tomcat_container bash ./opt/apache-tomcat-8.5.50/bin/shutdown.sh
4
 
          
5
docker exec -i tomcat_container bash ./opt/apache-tomcat-8.5.50/bin/startup.sh


The above will copy hello-world.war file to your dockerized tomcat container in /opt/apache-tomcat-8.5.50/webapps. Plus, you are making sure that you restart Tomcat to see the changes.

Build Now
Initiate the build process from the Project’s Jenkins Page, and go to the console log output to see if the build was a success or if there is any error/exception.

Your build finished with a SUCCESS message. Now go to your web browser and open http://your-ip-addr:8888/hello-world. You will see your application index page content. In our case, output is:

Voila!

You have successfully deployed a sample application over Tomcat Server running on Docker with the help of Jenkins Jobs. See you again on a new topic!


This article was originally posted on https://appfleet.com/ .

Topics:
cloud native ,devops ,docker ,jenkins ,kubernetes ,tomcat

Published at DZone with permission of Sudip Sengupta . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}