【原创】jenkins+maven自动部署脚本
jenkins+maven自动部署脚本
2019-02-01 09:34:38
前提,所有主机都做了免密登录操作,相信下面的版本大家应该可以看得明白,我是两台服务器,但只放开了一个服务器的配置,原理很简单:
1.配置一些经常变化的变量
2.进入svn下载下来的源代码空间目录,然后编译
3.远程备份文件(很重要)
4.进入编译后的文件夹target,将文件拷贝至远程生产主机
5.远程重启服务,搞定!
#!/bin/bash source /etc/profile #基础环境变量 SERVER_232="43.254.1.232" SERVER_194="49.4.14.194" USER="root" PORT_22232="22232" PORT_22194="22194" HOME="/crccmall2.0" LOG_PATH="/crccmall-log" BACK_232="/log/jenkins-back/" BACK_194="/vdb1/jenkins-back/" start_mian="java -jar -Xms2048m -Xmx2048m -XX:+UseParallelGC -XX:+UseAdaptiveSizePolicy" TIME=`date +%F` APP=microservice-authorization-server-1.0.1-SNAPSHOT.jar log_dir_name=`echo ${APP} | awk -F ".jar" '{print $1}'` #进入编译文件目录 cd microservice-authorization-server #编译文件clean mvn package -Dmaven.test.skip=true #进入编译后jar包所在文件夹 cd target echo "" echo "============================================【正在备份${APP}文件到${BACK_232}】============================================" echo "" ssh -p ${PORT_22232} ${USER}@${SERVER_232} "cp ${HOME}/${APP} ${BACK_232}${APP}'_'${TIME}" if [ $? -eq 0 ];then echo "信息:${APP}文件 【备份成功】" else echo "警告:${APP}文件 【备份失败】" fi echo "" echo "============================================【正在拷贝${APP}文件到${SERVER_232}服务器中】============================================" echo "" scp -P ${PORT_22232} ${APP} ${USER}@${SERVER_232}:${HOME}/${APP} if [ $? -eq 0 ];then echo "信息:${APP}文件 【拷贝成功】" else echo "警告:${APP}文件 【拷贝失败】" fi echo "" echo "==============================================【正在重启${APP}项目,请稍后】===============================================" echo "" /usr/bin/ssh -p ${PORT_22232} ${USER}@${SERVER_232} "ps -ef | grep -w ${APP} | grep -v grep | awk '{print "kill -9 " $2}'|sh" /usr/bin/ssh -p ${PORT_22232} ${USER}@${SERVER_232} "su ectocyst -c "nohup ${start_mian} ${HOME}${APP} >> ${LOG_PATH}/${log_dir_name}.log &"" if [ $? -eq 0 ];then echo "信息:${APP}文件 【重启成功】" else echo "警告:${APP}文件 【重启失败】" fi #echo "" #echo "============================================【正在备份${APP}文件到${BACK_194}】============================================" #echo "" #ssh -p ${PORT_22194} ${USER}@${SERVER_194} "cp ${HOME}/${APP} ${BACK_194}${APP}'_'${TIME}" #if [ $? -eq 0 ];then # echo "信息:${APP}文件 【备份成功】" #else # echo "警告:${APP}文件 【备份失败】" #fi #echo "" #echo "============================================【正在拷贝${APP}文件到${SERVER_194}服务器中】============================================" #echo "" #scp -P ${PORT_22194} ${APP} ${USER}@${SERVER_194}:${HOME}/${APP} #if [ $? -eq 0 ];then # echo "信息:${APP}文件 【拷贝成功】" #else # echo "警告:${APP}文件 【拷贝失败】" #fi #echo "" #echo "============================================【${APP}项目重启中】============================================" #echo "" #/usr/bin/ssh -p ${PORT_22194} ${USER}@${SERVER_194} "ps -ef | grep -w ${APP} | grep -v grep | awk '{print "kill -9 " $2}'|sh" #/usr/bin/ssh -p ${PORT_22194} ${USER}@${SERVER_194} "su ectocyst -c "nohup ${start_mian} ${HOME}${APP} >> ${LOG_PATH}/${log_dir_name}.log &"" #if [ $? -eq 0 ];then # echo "信息:${APP}文件 【重启成功】" #else # echo "警告:${APP}文件 【重启失败】" #fi