Deploy a WAR file on Tomcat Server using Jenkins

In this document, I will demonstrate deploying a war file (application code)  that is present in the Github repository to the Tomcat server using Jenkins. To complete this article you need to understand the following prerequisites.

Prerequisite

  1. A sample Java code repository on Github.

If you don’t have a sample Java code you can use my code which is present in the Github repository. This code is built with Maven.

https://github.com/techiescorner/Hello-World-Code.git

To clone this code use the “git clone” command.

2.  Jenkins server installed with Github and Maven plugins.

To install Jenkins server on EC2 VM click here

To install Github plugin for Jenkins click here

To configure Maven plugin for Jenkins server click here

3. A VM with Tomcat server installed

To install and configure  Tomcat server on AWS cloud click here

 

Workflow diagram 

 

 

The code is present in the Github repository and we will deploy this code an EC2 VM, running the Tomcat server. For this, we need to install a plugin (Deploy to container) on the Jenkins server.

Deploy to container Plugin installation

Log in to the Jenkins server and click on Manage Jenkins present on the left side menu. On the next page select Manage plugin and click on the available tab. Search for deploy to container and select it. Click Install without restart button to install it.

The plugin installation is completed successfully. Next, we will create a new job on Jenkins to deploy the war file.

Create a Jenkins job

Click on create a new item to start from the left side menu.

Select the Maven project because the Java code is built with Maven. On the next page give a description for your project.

Select Git as source code management and paste your repository URL. If you don’t have one you can use the below repository.

https://github.com/techiescorner/Hello-World-Code.git

Add “clean install package” as Goal and options as given in the above screenshot. Also, we need to add a “post-build Action“. Here we are deploying it to a VM, so choose “Deploy war/ear to a container”.

The above option is only available if we install the “Deploy to container” plugin. We need to provide a few details here.

If we don’t know the output war file name we can give it as **/*.war then it will automatically pick up the value. Next is the container, click add container button to add a container. I have installed the Tomcat-8 version on EC2 VM, so I chose it. To communicate with the Tomcat server we need a hostname, user, and password. Click the “Add” button near to the credentials to add user details. I have used a Tomcat user with the role “manager-script”  [Refer the  below article to understand how to install and configure Tomcat server]

Tomcat installation and configuration

Give a password, ID, and description for the user. Click the “Add” button to add details. Next, hostname we have to add on the Tomcat URL box. Click Apply and Save button to complete Jenkins job. Refer to the below screenshot for more details.

We are now ready to build the code, for this click on “Build now” from the left side panel.

If the build is successful you will get a message like below.

[INFO] Maven Project ...................................... SUCCESS [  1.008 s]
[INFO] Server ............................................. SUCCESS [  6.905 s]
[INFO] Webapp ............................................. SUCCESS [  1.514 s]
[INFO] --------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------
[INFO] Total time:  11.244 s
[INFO] Finished at: 2020-03-27T11:53:33Z
[INFO] --------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[DeployPublisher][INFO] 
Attempting to deploy 1 war file(s)
[DeployPublisher][INFO] Deploying /var/lib/jenkins/workspace/war-deploy-tomcat/webapp/target/webapp.war to container Tomcat 8.x Remote with context null
  [/var/lib/jenkins/workspace/war-deploy-tomcat/webapp/target/webapp.war] is not deployed. Doing a fresh deployment.
  Deploying [/var/lib/jenkins/workspace/war-deploy-tomcat/webapp/target/webapp.war]
Finished: SUCCESS

 

When we built the jobs, the war file will be placed on the Tomcat server under the tomcat installation ‘webapps’ directory.

 

Here “/opt/tomcat/webapps/ is my Tomcat installation directory (Default contents load from here).

From the above screenshot, we can confirm that the war file is successfully deployed under the webapp folder.

We can access the application with the help of the Tomcat server IP address. Add “webapps” after port number as shown in the below image.

We could successfully build our code with Jenkins and deployed it on a Tomcat server.

On the next page, we can see how can a built happen automatically when there is a change in the code.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *