Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement jenkins+shell Automation deployment

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to achieve jenkins+shell automation deployment, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to achieve jenkins+shell automation deployment. Let's take a look at it.

Through the combination of jenkins and shell scripts, war packages are automatically deployed to multiple servers.

Environment:

192.168.2.120 jenkins server

192.168.2.117 tomcat Server

Set jenkins server keyless login tomcat server

117Service: execute the command ssh-keygen-t rsa

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 the configuration button to enter the configuration interface

1) Select execute shell

Click the Save button

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 此处由于直接通过网页上下载war包,所以需要开启httpd service httpd start(yum install httpd apr apr-util) 在120服上添加脚本名称 mkdir -p /data/shcd /data/sh 添加脚本内容: #!/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 在jenkins的web界面,进入job后,点击立即构建按钮 1) 2)进入构建页面,点击console output按钮,可以看到相关输出信息 构建完成后,可以在120的web界面看到下载的文件edu.war 在117上启动tomcat,浏览器输入网址,可以看到如下部署好的web页面 至此自动部署web完成 如果要部署多个,可以在ip.txt设置多个ip,然后通过for循环取部署即可 添加个119服务器,并配置好tomcat,然后设置120服免密钥登陆119服 此时有两个tomcat服务器,分别为117服和119服 在工程的配置界面,修改脚本为 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 在/data/sh目录下手动建立文本ip.txt,里面填写上要自动部署web的ip地址 [root@localhost sh]# cat /data/sh/ip.txt 192.168.2.117192.168.2.119 然后点击立即构建按钮,此时就可以在117服和119服上看到自动部署的web界面 1)117服 2)119服

If there are a large number of tomcat servers, you can use the combination of ansible tools and jenkins to achieve batch deployment.

Note:

1) jdk should be installed on tomcat server

2) download manually on the tomcat server to prompt for permission issues

For the apache installed by yum, the default execution user is apache, while the owner and group of the file edu.war of cp in the script are root, so the prompt has no permission.

3) the jdk path of tomcat server should be unified.

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

5) httpd is combined to obtain the download path. In fact, the document storage path can be set in the configuration interface of the project helloworld.

A. Click the add Post-build step button to set the path

B. after you click build immediately, the war package will be stored in this path. Url is http://192.168.2.120:8080/job/helloworld/lastSuccessfulBuild/artifact/target/edu.war.

C. You can get the war package directly through url

such as

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% dcombe% 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. This is the end of the article on "how to automate the deployment of jenkins+shell" in / etc/profileset-m Binhampash $TOMCAT_DIR/bin/startup.sh. Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to achieve jenkins+shell automation deployment". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report