jenkins利用pipeline进行回滚
一 利用jar包名字查找pid
1 设置项目和要回滚的主机
2 选择jar包名字
3 执行回滚脚本
4 脚本内容
回滚脚本
#!/bin/bash
function sendToRemote(){
currentTime=`date "+%Y%m%d_%H%M%S"`
remote_shell_path=/home/spring_boot
remote_path=/home/spring_boot/${project}
#echo $remote_path
remote_backup_path=${remote_path}/backup
file_name=`ssh ${remote_user}@${remote_ip} -p${remote_port} "ls -t ${remote_backup_path} |head -1"` #最新的备份文件
echo "开始关闭服务"
ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/server.sh stop ${project} ${currentTime}${jarname} ${jarname}"
#ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/server.sh stop ${jarname}.jar" && sleep 2
#stop=`ssh ${remote_user}@${remote_ip} -p${remote_port} "ps -ef |grep ${project}|grep -v grep |wc -l"`
#if [ ${stop} -eq 0 ];then
# echo "关闭完成"
#删除有问题的软件包
ssh ${remote_user}@${remote_ip} -p${remote_port} "rm -rf ${remote_path}/20*"
#else
# echo "关闭失败"
#exit
#fi
echo "关闭完成"
echo "正在拷贝回滚文件"
ssh ${remote_user}@${remote_ip} -p${remote_port} "cp -r ${remote_backup_path}/${file_name} ${remote_path}"
echo "恢复完成"
sleep 5
echo "开始启动服务"
ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/server.sh start ${project} ${file_name}" && sleep 2
#job=`ssh ${remote_user}@${remote_ip} -p${remote_port} "ps -ef |grep ${project}|grep -v grep |wc -l"`
#if [ ${job} -eq 1 ];then
# echo "启动完成"
#else
# echo "启动失败"
#exit
# fi
}
function remote(){
project=$1
jarname=$2
parentDirectory=$4
remote_port=22
remote_user=root
remote_ip=$3
echo "remote_ip:${remote_ip}"
local_dir=/home/yw/git_project
sendToRemote
}
case "$@" in
*)
packageName="$1"
parentDirectory="$3"
node="$2"
remote ${packageName%.*} ${packageName} ${node} ${parentDirectory}
;;
esac
echo "$@脚本执行完毕"
服务重启脚本
#!/bin/bash
project="$2" # 项目名字 testwechat
appName="$3" # jar包名字 20200616_144104WeChat.jar
jarname="$4"
echo $jarname
APP_NAME=${appName}
#packageName=20200616_144104WeChat.jar #打包的名字
APP_NAME=${appName%.*} #把jar去掉
APP_NAME=${APP_NAME:15} #只保留WeChat这个名字
PROJECT=${project}
APP_DIR=/home/spring_boot/${PROJECT} # 项目路径
JAR_NAME=${appName} # jar包路径
java="/usr/bin/java"
# 自定义
logPath=/data/log/$project
#echo "11111111111"+ $logPath
if [ ! -x "$logPath" ]; then
mkdir -p "${logPath}"
fi
envName=test
case "$1" in
start)
#nohup java -jar $APP_DIR/$JAR_NAME --logging.path=$logPath >$logPath/nohup.out 2>&1 &
#echo "开始启动服务"
nohup $java -jar $APP_DIR/$appName --spring.profiles.active=$envName > $logPath/nohup.log &
#su ops -c " nohup java -jar $APP_DIR/$appName > $APP_DIR/nohup.log &"
sleep 17
echo "查看启动日志......."
grep -r JVM $logPath/nohup.log
#tail -n20 $APP_DIR/nohup.log
echo "查看启动进程"
sleep 5
#/usr/bin/ps -ef |grep $jarname|grep -v grep
ps -ef |grep $appName|grep -v grep|grep java
#echo "start $APP_NAME success!"
;;
stop)
APP_PID=`ps -ef|grep $jarname|grep $APP_DIR|grep -v 'grep'|awk '{print $2}'`
# APP_PID=`ps -ef|grep $jarname|grep -v 'grep'|awk '{print $2}'`
echo ${APP_PID}
#kill -9 $APP_PID
#exit
if [ "$APP_PID" == "" ];
then
echo "已经停止"
#exit
else
# echo "$APP_NAME process pid is:$APP_PID"
# sleep 5
# echo "begin kill $APP_NAME process, pid is:$APP_PID"
echo "开始杀进程"
kill -9 $APP_PID
echo "stop $APP_NAME success!"
fi
;;
restart)
echo "restarting $APP_NAME"
/usr/local/tiantian_dubbo/server.sh stop $2
echo "stoped"
sleep 2
echo "starting"
/usr/local/tiantian_dubbo/server.sh start $2
;;
*)
echo "start|restart|stop"
;;
esac
exit 0
最后保存,执行即可
二 利用端口查找pid
1 设置项目和要回滚的主机
2 设置jar包名称和端口
3 回滚脚本
node() {
stage("rollback"){
sh '''
/jenkins_shell/rollback_port.sh $appname $host $jarname $project_port
'''
}
}
4 主脚本内容:
#!/bin/bash
function sendToRemote(){
currentTime=`date "+%Y%m%d_%H%M%S"`
remote_shell_path=/home/spring_boot
remote_path=/home/spring_boot/${project}
#echo $remote_path
remote_backup_path=${remote_path}/backup
file_name=`ssh ${remote_user}@${remote_ip} -p${remote_port} "ls -t ${remote_backup_path} |head -1"` #最新的备份文件
echo "开始关闭服务"
ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/aliay_server.sh stop ${project} ${currentTime}${jarname} ${project_port}"
#ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/server.sh stop ${jarname}.jar" && sleep 2
#stop=`ssh ${remote_user}@${remote_ip} -p${remote_port} "ps -ef |grep ${project}|grep -v grep |wc -l"`
#if [ ${stop} -eq 0 ];then
# echo "关闭完成"
#删除有问题的软件包
ssh ${remote_user}@${remote_ip} -p${remote_port} "rm -rf ${remote_path}/20*"
#else
# echo "关闭失败"
#exit
#fi
echo "关闭完成"
echo "正在拷贝回滚文件"
ssh ${remote_user}@${remote_ip} -p${remote_port} "cp -r ${remote_backup_path}/${file_name} ${remote_path}"
echo "恢复完成"
sleep 5
echo "开始启动服务"
ssh ${remote_user}@${remote_ip} -p${remote_port} "/bin/sh ${remote_shell_path}/aliay_server.sh start ${project} ${file_name}" && sleep 2
#job=`ssh ${remote_user}@${remote_ip} -p${remote_port} "ps -ef |grep ${project}|grep -v grep |wc -l"`
#if [ ${job} -eq 1 ];then
# echo "启动完成"
#else
# echo "启动失败"
#exit
# fi
}
function remote(){
project=$1
jarname=$2
parentDirectory=$4
remote_port=22
remote_user=root
remote_ip=$3
echo "remote_ip:${remote_ip}"
local_dir=/home/yw/git_project
sendToRemote
}
case "$@" in
*)
packageName="$1"
parentDirectory="$3"
node="$2"
project_port="$4"
remote ${packageName%.*} ${packageName} ${node} ${parentDirectory}
;;
esac
echo "$@脚本执行完毕"
5 启动和关闭服务的脚本如下;
#!/bin/bash
project="$2" # 项目名字 testwechat
appName="$3" # jar包名字 20200616_144104WeChat.jar
port="$4"
echo $jarname
APP_NAME=${appName}
#packageName=20200616_144104WeChat.jar #打包的名字
APP_NAME=${appName%.*} #把jar去掉
APP_NAME=${APP_NAME:15} #只保留WeChat这个名字
PROJECT=${project}
APP_DIR=/home/spring_boot/${PROJECT} # 项目路径
JAR_NAME=${appName} # jar包路径
java="/usr/bin/java"
# 自定义
logPath=/data/log/$project
#echo "11111111111"+ $logPath
if [ ! -x "$logPath" ]; then
mkdir -p "${logPath}"
fi
envName=test
case "$1" in
start)
#nohup java -jar $APP_DIR/$JAR_NAME --logging.path=$logPath >$logPath/nohup.out 2>&1 &
#echo "开始启动服务"
nohup $java -jar $APP_DIR/$appName --spring.profiles.active=$envName > $logPath/nohup.log &
#su ops -c " nohup java -jar $APP_DIR/$appName > $APP_DIR/nohup.log &"
sleep 17
echo "查看启动日志......."
grep -r JVM $logPath/nohup.log
#tail -n20 $APP_DIR/nohup.log
echo "查看启动进程"
sleep 5
#/usr/bin/ps -ef |grep $jarname|grep -v grep
ps -ef |grep $appName|grep -v grep|grep java
#echo "start $APP_NAME success!"
;;
stop)
pid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F"/" '{ print $1 }'); #判断进程是否存在
echo $pid
if [ -n "$pid" ]; #变量的值非空返回0
then kill -9 $pid;
echo "关闭服务成功"
else
echo "服务并没有启动"
fi
;;
restart)
echo "restarting $APP_NAME"
/usr/local/tiantian_dubbo/server.sh stop $2
echo "stoped"
sleep 2
echo "starting"
/usr/local/tiantian_dubbo/server.sh start $2
;;
*)
echo "start|restart|stop"
;;
esac
exit 0
最后执行即可