publish.sh
#!/bin/bash
#description: 一键部署脚本
# 注意事项:以下所有路径都不是以/结尾
# 标题
title="即将部署制券客户端"
# 源码根路径
git_source_root_path="/home/git_workspace/fd-cloud"
# 源码项目路径(相对路径) 备注:如果没有配置 项目路径 即认为是单模块项目,根路径即项目路径
git_source_project_path="fd-client-parent/client-vms/vms-jhyt-yrbjk"
# 项目备份路径
project_backup_path="/home/run/fd-cloud/client/vms/backup1"
# 项目运行目录
project_run_path="/home/run/fd-cloud/client/vms"
# 项目名称
project_name="vms-jhyt-yrbjk-0.0.1-SNAPSHOT.jar"
# 日志路径
log_path="/home/logs/fd-cloud/vms-jhyt-yrbjk/vms-jhyt-yrbjk.log"
# 运行环境
active="test"
# 堆转储文件生成的路径
heap_dump_path="/home/logs/jvm/vms-jhyt-yrbjk-gc.hprof"
# JVM初始分配的堆内存
xms="-Xms256m"
# JVM最大允许分配的堆内存,按需分配
xmx="-Xmx256m"
# 一键部署
go(){
echo $title
sleep 3
echo "切换到源码目录"
cd $git_source_root_path
echo "当前目录:"
pwd
echo "当前分支:"
git branch
read -p'切换分支? Y/[N]:' checkBranch
if [ "$checkBranch" == "Y" ] || [ "$checkBranch" == "y" ]; then
echo "切换分支前拉取代码开始"
git pull
echo "切换分支前拉取代码完成"
sleep 3
read -p "请输入分支名称:" branchName
read -p "确认切换到分支<$branchName>? Y/[N]:" confirm
if [ "$confirm" == "Y" ] || [ "$confirm" == "y" ]; then
echo "切换分支开始"
git checkout $branchName
echo "切换分支结束"
echo "切换分支后当前分支:"
git branch
sleep 3
fi
fi
echo "拉取代码开始"
git pull
echo "代码拉取完成"
sleep 1
echo "开始打包"
if [ -z $git_source_project_path ]; then
echo "打包命令:mvn clean install"
mvn clean install
else
echo "打包命令:mvn clean package -pl $git_source_project_path -am install -Dmaven.skip.test=true"
mvn clean package -pl $git_source_project_path -am install -Dmaven.skip.test=true
fi
echo "打包完成"
sleep 1
echo "jar包备份开始"
# 如果文件夹不存在,创建文件夹
if [ ! -d $project_backup_path ]; then
echo "备份文件目录不存在: $project_backup_path,开始创建备份文件目录"
mkdir $project_backup_path
fi
date=$(date +%Y%m%d%H%M)
cp $project_run_path/$project_name $project_backup_path/"$project_name"_"$date"
echo "jar包备份完成"
sleep 1
echo "拷贝Jar包到运行目录"
cp $git_source_root_path/$git_source_project_path/target/$project_name $project_run_path/
echo "拷贝完成"
echo "开始运行"
restart
echo "运行成功"
sleep 2
echo "查看日志"
log
}
# 启动项目
start(){
nohup java $xms $xmx -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$heap_dump_path -jar $project_run_path/$project_name --spring.profiles.active=$active > /dev/null 2>&1 &
echo "$project_name 已启动"
}
# 停止项目
stop(){
PID=`ps -ef|grep $project_name|grep -v grep|awk '{printf $2}'`
if [ ! -n "$PID" ];
then
echo "$project_name程序未启动"
else
kill -9 $PID
echo "已杀掉$project_name进程:$PID"
fi
}
# 查看项目运行状态
status(){
PID=`ps -ef|grep $project_name|grep -v grep|awk '{printf $2}'`
if [ ! -n "$PID" ];
then
echo "$project_name 未启动"
else
echo "$project_name 运行中,PID为:$PID"
fi
}
# 重启项目
restart(){
stop
echo "等待3秒,启动"
sleep 3
echo "启动中"
start
}
# 查看日志
log(){
tail -f $log_path
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
go)
go
;;
log)
log
;;
restart)
restart
;;
*)
echo "Usage: {go|start|stop|status|restart|log}"
;;
esac
exit 0