Integrating Tomcat Server in CI/CD pipeline (By Git Automation)

  1. Prerequisites
  2. Download & Install Tomcat
  3. Add Role for Tomcat
  4. Update users information
  5. Install Deploy to container in Jenkins Server
  6. Create Deploy Tomcat Server Job
  7. Build Jenkins & Check Tomcat Server
  8. Deploy on VM through PollSCM

Prerequisites

  1. EC2 Instance
    • With Internet Access
    • Security Group with Port 8080 open for internet
  1. Java v1.8.x

Delete Java if Java version is not over 1.8 Install Java

sudo su - 
java -version
java version "1.7.0_211"
yum remove java -1.7.0*

Install Java

yum install java-1.8*

Confirm Java Version and set the java home

find /usr/lib/jvm/java-1.8* | head -n 3
/usr/lib/jvm/java-1.8.0
/usr/lib/jvm/java-1.8.0-openjdk
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.amzn2.0.1.x86_64

Open .bash_profile

vi ~/.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.amzn2.0.1.x86_64
PATH=$PATH:$JAVA_HOME

export PATH

Download & Install Tomcat

Download Tomcat in opt folder

wget http://mirror.navercorp.com/apache/tomcat/tomcat-8/v8.5.58/bin/apache-tomcat-8.5.58.tar.gz
  • Version 8.5.58

Install Tomcat

tar -xvzf apache-tomcat-8.5.58.tar.gz

Start Tomcat

./startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.58
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.58
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.58/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.58/bin/bootstrap.jar:/opt/apache-tomcat-8.5.58/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
  • /opt/apache-tomcat-8.5.58/bin

Security Group 8080 is opened. So, You can access.

find / -name context.xml
/opt/apache-tomcat-8.5.58/conf/context.xml
/opt/apache-tomcat-8.5.58/webapps/host-manager/META-INF/context.xml
/opt/apache-tomcat-8.5.58/webapps/manager/META-INF/context.xml
  • By default Tomcat Server allows to access local system
vi /opt/apache-tomcat-8.5.58/webapps/manager/META-INF/context.xml
vi /opt/apache-tomcat-8.5.58/webapps/host-manager/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      <http://www.apache.org/licenses/LICENSE-2.0>

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1" />
  <Manager sessionAttributeValueClassNameFilter="java\\.lang\\.(?:Boolean|Integer|Long|Number|String)|org\\.apache\\.catalina\\.filters\\.CsrfPreventionFilter\\$LruCache(?:\\$1)?|java\\.util\\.(?:Linked)?HashMap"/>
</Context>                                                                                                                
  • Modify Allow part
<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1" /> -->
  • Comment out

After re-setting the configuration, tomcat server restart /opt/apache-tomcat-8.5.58/bin

$ ./shutdown.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.58
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.58
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.58/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.58/bin/bootstrap.jar:/opt/apache-tomcat-8.5.58/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
$ ./startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.58
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.58
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.58/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.58/bin/bootstrap.jar:/opt/apache-tomcat-8.5.58/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

Update users information

<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"/>
  • Change username and password and then use it.

Add New User information for /opt/apache-tomcat-8.5.58/conf

<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
  <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>

After re-setting the configuration, tomcat server restart /opt/apache-tomcat-8.5.58/bin

$ ./shutdown.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.58
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.58
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.58/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.58/bin/bootstrap.jar:/opt/apache-tomcat-8.5.58/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
$ ./startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.58
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.58
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.58/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.58/bin/bootstrap.jar:/opt/apache-tomcat-8.5.58/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

Install Deploy to container in Jenkins Server

Manage Jenkins → Manage Plugins → Available

  • [ ] Deploy to container**Artifact Uploaders**This plugin allows you to deploy a war to a container after a successful build.Glassfish 3.x remote deployment1.151 yr 1 mo ago

새로운 Item(New Item) → Enter an item name(Maven Project) → Edit Description

Enter an item name: Deploy_on_Tomcat_Server

Edit Description: Deploy_on_Tomcat_Server

Source Code Management: git

Build

  • Goals and options: clean install package

Post-build Actions

  • Deploy war/ear to a container WAR/EAR files: /.war
  • Containers:
    • Tomcat 8.x Remote
      • Credentials: add Jenkins → ex) Username deployer Password deployer ID deployer_user Description user to deploy on tomcat VM: Check in tomcat server (tomcat-users.xml)
      • Tomcat URL: http://<Public_IP>:8080

Apply & Save

Build Jenkins Job, and Jenkins will copy the war file to the target server and delivers it. (Tomcat Server)

Tomcat Server usually default location is webapps

  • /opt/apache-tomcat-8.5.58/webapps

Build Jenkins & Check Tomcat Server

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.146 s
[INFO] Finished at: 2020-09-17T03:27:55Z
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/pom.xml to com.example.maven-project/webapp/1.0-SNAPSHOT/webapp-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war to com.example.maven-project/webapp/1.0-SNAPSHOT/webapp-1.0-SNAPSHOT.war
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/server/pom.xml to com.example.maven-project/server/1.0-SNAPSHOT/server-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/server/target/server.jar to com.example.maven-project/server/1.0-SNAPSHOT/server-1.0-SNAPSHOT.jar
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/pom.xml to com.example.maven-project/maven-project/1.0-SNAPSHOT/maven-project-1.0-SNAPSHOT.pom
channel stopped
[DeployPublisher][INFO] Attempting to deploy 1 war file(s)
[DeployPublisher][INFO] Deploying /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war to container Tomcat 8.x Remote with context null
  [/var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war] is not deployed. Doing a fresh deployment.
  Deploying [/var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war]
Finished: SUCCESS
  • Deploying [/var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war]

Check war file in Tomcat Server

$ ls
webapp.war

http://<Public_IP>:8080/webapp/ << Tomcat Server

Deploy on VM through PollSCM

Configuration→ Build Triggers → pollSCM

If the repository is changed, build the job

Schedule

* * * * *

min/hour/day/week/month

https://github.com/yankils/hello-world.git code fork and add new and commit

After 1minute, automatically build Jenkins job

[INFO] Reactor Summary for Maven Project 1.0-SNAPSHOT:
[INFO] 
[INFO] Maven Project ...................................... SUCCESS [  1.047 s]
[INFO] Server ............................................. SUCCESS [  4.811 s]
[INFO] Webapp ............................................. SUCCESS [  1.624 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.219 s
[INFO] Finished at: 2020-09-17T05:27:24Z
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/pom.xml to com.example.maven-project/webapp/1.0-SNAPSHOT/webapp-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war to com.example.maven-project/webapp/1.0-SNAPSHOT/webapp-1.0-SNAPSHOT.war
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/server/pom.xml to com.example.maven-project/server/1.0-SNAPSHOT/server-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/server/target/server.jar to com.example.maven-project/server/1.0-SNAPSHOT/server-1.0-SNAPSHOT.jar
[JENKINS] Archiving /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/pom.xml to com.example.maven-project/maven-project/1.0-SNAPSHOT/maven-project-1.0-SNAPSHOT.pom
channel stopped
[DeployPublisher][INFO] Attempting to deploy 1 war file(s)
[DeployPublisher][INFO] Deploying /var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war to container Tomcat 8.x Remote with context null
  Redeploying [/var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war]
  Undeploying [/var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war]
  Deploying [/var/lib/jenkins/workspace/Deploy_on_Tomcat_Server/webapp/target/webapp.war]
Finished: SUCCESS

Source: https://www.udemy.com/course/valaxy-devops/

Leave a Reply

Your email address will not be published.

ANOTE.DEV