Through the combination of jenkins and shell scripts, the war package is automatically deployed on multiple servers.

Source: Internet
Author: User

Through the combination of jenkins and shell scripts, the war package is automatically deployed on multiple servers.

Combined with shell scripts, jenkins can automatically deploy war packages on multiple servers.

Environment:

192.168.2.120 jenkins Server

192.168.2.117 tomcat server

Set the jenkins server to log on to the tomcat server without a key

Server 117: Execute the command ssh-keygen-t rsa

120 server: cd. ssh/; scp-p id_rsa.pub root@192.168.2.117:/root/. ssh/authorized_keys

Or

Scp-p ~ /. Ssh/id_rsa.pub root@192.168.2.117:/root/. ssh/authorized_keys

Click helloworld and then click Configure to go to the configuration page.

1) Select execute shell

Click Save.

Script:

cp /root/.jenkins/jobs/helloworld/workspace/target/edu.war /var/www/html/download/chown apache:apache /var/www/html/download/edu.warssh  root@192.168.2.117  'bash -x -s' < /data/sh/auto_install.sh

Because the war package is downloaded directly from the web page, you need to enable httpd

Service httpd start (yum install httpd apr-util)

Add Script Name on server 120

mkdir -p /data/shcd /data/sh

Add script content:

#!/bin/bashTOMCAT_PID=`ps -ef| grep tomcat | grep -v grep |awk '{print $2}'`TOMCAT_DIR="/usr/local/tomcat"FILES="edu.war"DES_DIR="/usr/local/tomcat/webapps/ROOT"DES_URL="http://192.168.2.120/download"BAK_DIR="/data/backup/`date +%Y%m%d-%H%M`"[ -n "$TOMCAT_PID" ] && kill -9 $TOMCAT_PIDcd $DES_DIRrm -rf $FILESmkdir -p $BAK_DIR\cp -a $DES_DIR/* $BAK_DIR/rm -rf $DES_DIR/*wget -c $DES_URL/$FILES/usr/java/jdk1.8.0_151/bin/jar  -xvf  $FILEScd $TOMCAT_DIRrm -rf work. /etc/profileset -m;/bin/sh $TOMCAT_DIR/bin/startup.sh

On the jenkins web interface, enter the job and click build now.

1)

2) Go to the build page and click console output to view the relevant output information.

After the build is complete, you can view the downloaded file edu. war on the 120 web page.

Start tomcat on 117, enter the URL in the browser, and you can see the following deployed web Page

Web deployment is now complete

If you want to deploy multiple ip addresses, you can set multiple ip addresses in ip.txt and deploy them through the for loop.

Add 119 servers, configure tomcat, and set 120 server key-free login to 119 Server

There are two tomcat servers: 117 server and 119 server.

On the project configuration page, modify the script

cp /root/.jenkins/jobs/helloworld/workspace/target/edu.war /var/www/html/download/chown apache:apache /var/www/html/download/edu.warfor  I  in  `cat /data/sh/ip.txt`;do ssh  root@${I}  'bash -x -s' < /data/sh/auto_install.sh ;done

Manually create ip.txt in the/data/shdirectory, and enter the IP address to automatically deploy the web

[root@localhost sh]# cat /data/sh/ip.txt 192.168.2.117192.168.2.119

Click the build Now button to view the automatically deployed web interface on server 117 and server 119.

1) 117 servers

2) 119 servers

If the number of tomcat servers is large, you can use the ansible tool and jenkins to implement batch deployment.

Note:
1) install jdk on the tomcat server

2) manually download on tomcat server, prompting for permission issues

Apache installed in yum, the default execution user is apache, and the owner and group of the cp file edu. war in the script are both root, so no permission is prompted

3) The jdk path of the tomcat server should be uniform.

4) The default file path is/root/. jenkins/jobs/helloworld/workspace/target.

5) httpd is used to obtain the download path. In fact, you can set the file storage path on the helloworld configuration page of the project.

A. Click the Add build button to set the path

B. Click build now. The war package is stored in this path. The url is http: // 192.168.2.120: 8080/job/helloworld/lastSuccessfulBuild/artifact/target/edu. war.

C. You can directly obtain the war package through url,

For example

wget http://192.168.2.120:8080/job/helloworld/lastSuccessfulBuild/artifact/target/edu.war

The corresponding server path is/root/. jenkins/jobs/helloworld/lastSuccessful/archive/target.

Auto_install.sh

#!/bin/bashTOMCAT_PID=`ps -ef| grep tomcat | grep -v grep |awk '{print $2}'`FILENAME="edu.war"TOMCAT_DIR="/usr/local/tomcat"DES_DIR="/usr/local/tomcat/webapps/ROOT"DES_URL="http://192.168.2.120:8080/job/helloworld/lastSuccessfulBuild/artifact/target"BAK_DIR="/data/backup/`date +%Y%m%d-%H%M`"[ -n "$TOMCAT_PID" ] && kill -9 $TOMCAT_PIDcd $DES_DIRrm -rf $FILENAMEmkdir -p $BAK_DIR\cp -a $DES_DIR/* $BAK_DIR/rm -rf $DES_DIR/*wget -c $DES_URL/$FILENAME/usr/java/jdk1.8.0_151/bin/jar  -xvf  $FILENAMEcd $TOMCAT_DIRrm -rf work. /etc/profileset -m;/bin/sh $TOMCAT_DIR/bin/startup.sh

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.