preface

As many of you may know, in the early stage of Internet enterprises, most of them use manual packaging, uploading and publishing for code release. The common way is to use packaging tools to manually package, upload to the WEB server, back up the original code file, release new code, restart the service and check whether the release is successful.

As time went by, the number of WEB clusters increased, and this manual operation would greatly reduce the productivity, so a kind of “continuous integration” was introduced, as for what is “continuous integration” please feel free to baidu or Google.

Today, I’m going to talk about a small part of continuous integration: the ability to automatically package and distribute code, which is also common in production environments.

Install the JDK and Tomcat environment

[root@centos6 ~]# tar zxf jdk-8u111-linux-x64.tar.gz -C /usr/local/

[root@centos6 ~]# tar zxf apache-tomcat-8.5.9.tar.gz -C /usr/local/

[root @ centos6 local] # export JAVA_HOME = / usr/local/jdk1.8.0 _111

[root@centos6 local]# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

[root@centos6 local]# export PATH=$JAVA_HOME/bin:$PATH

[root @ centos6 local] # export the CATALINA_HOME = / usr/local/apache tomcat — 8.5.9

[root@centos6 local]# source /etc/profile

[root@centos6 local]# java -version

Java version “1.8.0 comes with _111”

Java(TM) SE Runtime Environment (build 1.8.0_111-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

Download the installation package from the official website

[root@centos6 ~]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war

/ root @ centos6 ~ # cp Jenkins. War/usr/local/apache tomcat — 8.5.9 / webapps /

[root@centos6 webapps]#.. /bin/startup.sh &

Log in to the WEB UI and perform related configurations

Prompt for password

The initial password can be found in the file below

[root@centos6 webapps]# cat /root/.jenkins/secrets/initialAdminPassword

0d647a32992149b3b43f77e4bda93809

Enter the password to go to the next screen

Then select install plug-in and the next step is complete

The installation is complete

Create a user and password

After completion, you can enter the home page for configuration and management

Next, install the plug-in required later

System Management – Plug-in management – Install maven plug-ins

Installing the SVN Plug-in

Install the Deploy to Container Plugin

Download software from the official website

Wget HTTP: / / http://apache.fayea.com/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz

Tar ZXF apache-3.3.9-bin.tar. gz -c /usr/local/

Mv/usr/local/apache maven – 3.3.9 / usr/local/maven – 3.3.9

Configuring environment Variables

Echo ‘export MAVEN_HOME=/usr/local/maven-3.3.9’ >> /etc/profile

echo ‘export PATH=$PATH:$MAVEN_HOME/bin’ >> /etc/profile

source /etc/profile

Check whether the installation is complete

[root@centos6 ~]# mvn -version

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)

Maven home: / usr/local/Maven – 3.3.9

Java version: 1.8.0_111, vendor: Oracle Corporation

Java home: / usr/local/jdk1.8.0 _111 / jre

Default locale: en_US, platform encoding: UTF-8

OS name: “Linux “, version: “2.6.32-642.el6.x86_64″, ARCH:” AMD64 “, family: “Unix”

Configure Maven on Jenkins

The SVN configuration is the same as the preceding

create
Job

Let’s create a JOB to automate packaging and code distribution

Start creating a task or project

Enter the project name and select build a Maven project

Set the SVN address of the code base. Note that the URL must have access permission

If there is no optional user, you need to create one

Bulid The pom.xml file here is provided by the developer and cannot be packaged automatically without it

Here we are going to use an automated publishing script

Click Save after the configuration is complete

You can see the newly created JOB on the home page, click Build now, and see the output on the console

You can also see that the code has been packaged, and the directory is as follows:

/root/.jenkins/workapace/JAVA/target/spring-mvc.war

Now that we’ve done this, let’s test to see if we’re done

The browser enter http://192.168.4.254:80801/spring-mvc

Next we modify the contents of the home page file, rebuild it again, and then access the detection again

Found that automatic packaging and code publishing are implemented

[root@centos scripts]# cat auto_push.sh

#! /bin/sh

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

## create by mingongge at 2017-07-09

## this scripts is for auto push code to WEB server

## wechat official account: Youkanyouxiao

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

#stop server

/usr/local/tomcat/bin/shutdown.sh

TPID=`ps -ef|grep tomcat|grep -v grep|awk ‘{print $2}’`

TIME=`date +%F`

INSTALLDIR=/usr/local/tomcat/webapps/

 

if [ $TPID ==”” ]; then

   echo “tomcat stop sucessfully”

else

   kill -9 $TPID

fi

#push code to server and start server

cd $INSTALLDIR

\cp -ar /root/.jenkins/workspace/JAVA/target/*.war $INSTALLDIR

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

RESULT=`netstat -lntup|grep 8081|wc -l`

if [ $RESULT -eq 1 ]; then

   echo “tomcat start sucessfull”

else

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

Fi

There are no perfect scripts, only scripts that temporarily implement functionality !!!!

Write article is not easy, if have help, sweep code dozen reward worker elder brother!!