DEV Community

Cover image for Deploy Application Using Jenkins, Tomcat Server, and PollSCM
Dmitriy A. for appfleet

Posted on • Originally published at appfleet.com

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.

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.

$ 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
Enter fullscreen mode Exit fullscreen mode

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

$ docker ps

CONTAINER ID    IMAGE                COMMAND         CREATED       STATUS                PORTS                        NAMES
ce46107c9f83    jenkins/jenkins:lts  "/sbin/tini”    Up 1 minutes  Up 1 minutes             0.0.0.0:8080->8080/tcp       jenkins_container
Enter fullscreen mode Exit fullscreen mode

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

$ docker exec -it jenkins_container cat /var/jenkins_home/secrets/initialAdminPassword

gSzHTGfbtXSpcBXMyJ6gzmNfNH7BjhK23
Enter fullscreen mode Exit fullscreen mode

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:

Alt Text

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:

$ docker pull centos

Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete 
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
Enter fullscreen mode Exit fullscreen mode

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

$ docker run -dit --name tomcat_container -p 8888:8080 centos
Enter fullscreen mode Exit fullscreen mode

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:

$ docker exec -it eca291a36127 /bin/bash 
Enter fullscreen mode Exit fullscreen mode

Go to /opt directory, in your tomcat_container:

[root@eca291a36127 /]# cd opt/
Enter fullscreen mode Exit fullscreen mode

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.

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

Extract the tar file:

[root@eca291a36127 /]# tar -xvzf apache-tomcat-8.5.50.tar.gz
[root@eca291a36127 /]# cd apache-tomcat-8.5.50/bin/
Enter fullscreen mode Exit fullscreen mode

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:

[root@eca291a36127 bin]# chmod +x shutdown.sh
[root@eca291a36127 bin]# chmod +x startup.sh
Enter fullscreen mode Exit fullscreen mode

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:

[root@eca291a36127 /]# yum install java-1.7.0-openjdk-devel
Enter fullscreen mode Exit fullscreen mode

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

[root@eca291a36127 /]# ./startup.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.50
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.50
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.50/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.50/bin/bootstrap.jar:/opt/apache-tomcat-8.5.50/bin/tomcat-juli.jar
Tomcat started.
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

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:

$ docker exec -it tomcat_container /bin/bash
Enter fullscreen mode Exit fullscreen mode

Now, find the context.xml file:

$ find / -name context.xml

/opt/apache-tomcat-8.5.50/conf/context.xml
/opt/apache-tomcat-8.5.50/webapps/host-manager/META-INF/context.xml
/opt/apache-tomcat-8.5.50/webapps/manager/META-INF/context.xml
Enter fullscreen mode Exit fullscreen mode

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:

...
<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
... 
Enter fullscreen mode Exit fullscreen mode

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:

$ /opt/apache-tomcat-8.5.50/conf && nano tomcat-users.xml
Enter fullscreen mode Exit fullscreen mode

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>:

... 
<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"/>
</tomcat-users>
...
Enter fullscreen mode Exit fullscreen mode

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

$ cd /opt/apache-tomcat-8.5.50/bin/
$ ./shutdown.sh
$ ./startup.sh 

Using CATALINA_BASE:   /opt/apache-tomcat-8.5.50
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.50
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.50/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.50/bin/bootstrap.jar:/opt/apache-tomcat-8.5.50/bin/tomcat-juli.jar
Tomcat started.
Enter fullscreen mode Exit fullscreen mode

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:

Alt Text

For Publish over SSH:

Alt Text

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:

Alt Text

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:

Alt Text

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

Alt Text

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).

Alt Text

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

Alt Text

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:

docker cp ~/tomcat/hello-world.war tomcat_container:/opt/apache-tomcat-8.5.50/webapps

docker exec -i tomcat_container bash ./opt/apache-tomcat-8.5.50/bin/shutdown.sh

docker exec -i tomcat_container bash ./opt/apache-tomcat-8.5.50/bin/startup.sh
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

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:

Alt Text

Voila!

You have successfully deployed a sample application over Tomcat Server running on Docker with the help of Jenkins Jobs.

Top comments (0)