Automatically package and deploy Tomcat environment based on Jenkins

Table of contents

1. Configure git host

2. Configure the jenkins host

3. Configure web host

4. Create a new Maven project

5. Verify Jenkins automatic packaging and deployment results


The working principle of Jenkins is to first copy the source code from the SVN/Git version control system to the local, and then call Maven to build according to the set script. The key to the entire system is the build script, which tells Jenkins the tasks that need to be performed in an integration.

 

lab environment

operating system

IP address

CPU name

Role

CentOS7.5

192.168.200.111

git

git server

CentOS7.5

192.168.200.112

Jenkins git client

jenkins server

CentOS7.5

192.168.200.113

tomcat

web server

Turn off firewall and selinux on all hosts

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# iptables -F

[root@localhost ~]# setenforce 0

1. Configure git host


# rpm installation installation dependencies: both 1 and 2 are installed

[root@gitclient ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel     [Some dependencies may be missing, such as c++, which can be installed as needed]

 

# Compile and install—server and client, visit https://mirrors.edge.kernel.org/pub/software/scm/git/ to download the required version

First rz import the git2.22 package

[root@gitclient ~]# tar xf git-2.22.0.tar.gz -C /usr/src/

[root@gitclient ~]# cd /usr/src/git-2.22.0/

[root@git git-2.22.0]# make configure [This step may prompt the following yum -y install to install it]

[root@git git-2.22.0]# yum -y install gcc-c++ [Install c++ dependencies]

[root@git git-2.22.0]# ./configure --prefix=/usr/local/git && make && make install

[root@git git-2.22.0]# ln -sf /usr/local/git/bin/git /usr/bin/

[root@git git-2.22.0]# git --version

 

#Configure users on the git server [first]

[root@git ~]# useradd git

[root@git ~]# echo "123456" | passwd --stdin git

[root@git ~]# echo "123456" | passwd --stdin git

Create a local warehouse probe

[root@git ~]# su - git

Last login: Thursday June 20 09:46:10 CST 2019pts/0

[git@git ~]$ mkdir probe.git

[git@git ~]$ cd probe.git

[git@git probe.git]$ git --bare init

Initialize an empty Git repository at /home/git/probe.git/

[git@git probe.git]$ exit

[root@git ~]# rz #Upload psi-probe.tar.gz

 

[root@git ~]# tar xf psi-probe.tar.gz

[root@git ~]# git clone [email protected]:/home/git/probe.git

[root@git ~]# cp -rf psi-probe/* probe/

[root@git ~]# cd probe/

[root@git probe]# git add .

[root@git probe]# git config --global user.email "[email protected]"

[root@git probe]# git config --global user.name "jialiang"

[root@git probe]# git commit -m "all probe"

[root@git probe]# git push origin master

2. Configure the jenkins host


Add verification credentials: You can add the credentials here or not. This test does not involve credentials.

 

 "Add Credentials".

 

 

 After filling in the above data, click "OK" to view the newly added remote web host account.

 

 

Add Publish Over SSH remote host

Click "Manage Jenkins" -> "Configure System" -> "Publish over SSH" -> "SSH Servers" -> "Add" option button on the Jenkins homepage to add an SSH remote host. As shown in Figure 3.13, after entering necessary information such as Name, Hostname, Username, etc., click the "Advanced" option button -> Check the "Use Password authentication, or use a different key" option -> Enter the "Remote host login password" -> "Test Configuration" tests the remote host configuration. After successfully testing the remote host configuration, click the "Save" button.

 

 

 

 

Configure Maven, JDK, and Git environments

On the Jenkins home page, click "Manage Jenkins" -> "Global Tool Configuration" -> "JDK" -> Add "JDK" and set the JDK alias to "JDK1 7 " . Remove the "Install automatically" option and set "JAVA_HOME" to the actual installation path of the JDK in this case.

/usr/local/java

 

 

 

 

 

Install maven in Jenkins

 [root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz

[root@jenkins ~]# mv apache-maven-3.5.0 /usr/local/maven-3.5.0

Change Alibaba Cloud mirror station for maven

[root@jenkins ~]# vim /usr/local/maven-3.5.0/conf/settings.xml

#Delete everything in the two <mirrors>, about 12 lines, and add the following Alibaba Cloud mirror station to the two <mirrors>

    <mirror>

        <id>nexus-aliyun</id>

        <mirrorOf>central</mirrorOf>

        <name>Nexus aliyun</name>

        <url>http://maven.aliyun.com/nexus/content/groups/public</url>

</mirror>

final effect 

 

 

Find the Maven configuration option in the "Global Tool Configuration" configuration interface, then click "Add Maven" and set the alias to "Maven3.5".

 

Git configuration

[root@jenkins ~]# which git

/usr/bin/git

 

After the above global basic configuration is completed, click Save to complete.

3. Configure web host


[root@tomcat ~]# tar xf apache-tomcat-8.5.40.tar.gz

[root@tomcat ~]# tar xf jdk-8u191-linux-x64.tar.gz

[ root @ tomcat ~ ] # mv jdk1 .8.0_191 / / usr / local / java

[root@tomcat ~]# mv apache-tomcat-8.5.40 /usr/local/tomcat

[root@tomcat ~]# vim /etc/profile

export JAVA_HOME=/usr/local/java/

export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

export PATH=$JAVA_HOME/bin:$PATH

 

[root@tomcat ~]# source /etc/profile

[root@tomcat ~]# java -version

java version "1.8.0_191"

Java(TM) SE Runtime Environment (build 1.8.0_191-b12)

Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

Publish the public key to the jenkins host

[root@tomcat ~]# ssh-keygen

[root@tomcat ~]# ssh-copy-id 192.168.200.112

 

[Back to jenkins]

The host Jenkins uses the jenkins user to connect to git by default, so use the jenkins user to generate a key pair and send it to git.

[root@jenkins ~]# id jenkins

uid=988(jenkins) gid=982(jenkins) group=982(jenkins)

 

[root@jenkins ~]# su -s /bin/bash jenkins

bash-4.2$ ssh-keygen

bash-4.2$ ssh-copy-id [email protected]

bash-4.2$ ssh [email protected]             #Login test

bash-4.2$ exit

[root@jenkins ~]# ssh-keygen

[root@jenkins ~]# ssh-copy-id [email protected]

[root@jenkins ~]# ssh [email protected]              #Login test

 

4. Create a new Maven project


After the above configuration is completed, return to the Jenkins homepage, select "New Task", then enter a task name "probe", select "Maven project" and click the "OK" button at the bottom of the current page.

 

 

After clicking the "OK" button, select "Source Code Management", select "Git", and configure "RepositoriesURL" as

[email protected].200.111:/home/git/probe.git

 

 

Select "Build"

clean package -Dmaven.test.skip=true

 

Select "send build artifacts over SSH" in "Post-build operations". The meaning of executing the command in "Exec command" is: kill the Tomcat process before automatic deployment, then delete the war package, and use the scp remote copy command to automatically package Jenkins. Copy the good project war package to the current Tomcat application directory. Then restart Tomcat.

 

scp 192.168.200.112:/root/.jenkins/workspace/probe/psi-probe-web/target/probe.war /usr/local/tomcat/webapps/

/usr/local/tomcat/bin/startup.sh

 

After all the above configurations are completed, click Save. Then click the "probe" -> "Build Now" you just created until the project is built. The build process can be viewed in the "Console Output".

 

 

 

5. Verify Jenkins automatic packaging and deployment results


Check on the web host whether the probe directory is copied to the /usr/local/tomcat/webapps directory

[root@tomcat ~]# ls /usr/local/tomcat/webapps/

[root@tomcat ~]# ls /usr/local/tomcat/webapps/probe -l

 Judging from the above results, Jenkins has copied the prepared probe war package.

It can be seen from the execution command after the build that Tomcat has been restarted and the test probe monitoring system is accessed through the browser. http://192.168.200.113:8080/probe .

 

[root@tomcat ~]# vim /usr/local/tomcat/conf/tomcat-users.xml

  <role rolename="manager-gui"/>

  <role rolename="admin-gui"/>

  <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>

</tomcat-users> # Add the above three lines before this line

 

[root@tomcat ~]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml

<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"

         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Comment out the above

 

[root@tomcat ~]# /usr/local/tomcat/bin/shutdown.sh

[root@tomcat ~]# /usr/local/tomcat/bin/startup.sh

 

 At this point, Jenkins’ automatic packaging and deployment is completed

Guess you like

Origin blog.csdn.net/2302_77750172/article/details/132426244