SpringBoot自动部署脚本,可用于jenkins

#!/bin/bash
# description:springboot 自动部署 1.0版本
# version 1.0
show_usage="args: [-t]\[--deploy-type=]"
#版本部署路径
mainpath=/data/service/youAppDir

# 应用名称
appName=yourAppName

#主函数
function deploy()
{
cd ${mainpath}
echo "[info]start deploy...[$(date +'%F %H:%M:%S')]"
stop
backup
upload
start
}

#启动app
function stop(){


echo `ps -ef | grep ${appName} | grep -v 'grep' | grep -v 'deploy'|awk '{print $2}' `;

PIDCOUNT=`ps -ef | grep ${appName} | grep -v 'grep' | grep -v 'deploy' | awk '{print $2}' | wc -l`;


echo "stopping pid count:$PIDCOUNT"
if [ ${PIDCOUNT} -gt 0 ];then
{
# 获取进程ID
appID=$(ps -ef | grep ${appName} | grep -v 'grep' | grep -v 'deploy' | awk '{print $2}')
echo "[info]当前进程ID为:$appID"
kill -9 $appID
} || {
echo "[info]进程ID为:$appID停止异常"
}
fi
echo "stoped pid:$appID"

}

#启动app
function start(){

echo "starting $appName.jar"
nohup java -jar -Dspring.profiles.active=test -Xms128m -Xmx512m $mainpath/$appName.jar > /dev/null 2>&1 &

echo "success started $appName.jar"
}
#上传文件
function upload(){
echo "uploading $mainpath/${appName}.jar"
cp $mainpath/respo/${appName}.jar $mainpath/${appName}.jar
}

#版本回滚
deploy_Rollback()
{
#进入备份文件夹
cd ${mainpath}/backup/
#获取最新备份文件
file_name_new=''
for i in `ls -tr`;
do
echo $i;
file_name_new=$i;
done;
#将备份文件复制到webapps
cp ${mainpath}/backup/${file_name_new} ${mainpath}/${appName}.jar
if [ $? -eq 0 ]
then
echo 复制${file_name_new}成功
else
echo 复制失败,退出!
exit 1
fi
#进入webapps/目录
cd ${mainpath}
#重启App
stop
start
}

#备份原来的项目
function backup()
{
echo "开始备份to${mainpath}...."
fileDate=$(date "+%Y%m%d%H%M%S")
fileName=${appName}${fileDate}
mv $mainpath/${appName}.jar $mainpath/backup/${fileName}.jar
}

echo --------欢迎使用shell自动部署脚本--------
#获取用户操作
case $wm2 in
rollback)
deploy_Rollback
;;
*)
deploy
;;
esac