jenkins 远程部署脚本
2020-02-23 本文已影响0人
秋元_92a3
脚本功能
1、将当前运行的jar包移动到备份目录下
2、将jar从jenkins远程投递的目录移动到自定义的服务目录
3、查看服务进程号PID,如果其存在,则kill调
4、启动自定义服务目录中的jar包,在启动参数中配置选择的环境变量。
SERVER_NAME="ruoyi-admin"
BASE_PATH="/data/${SERVER_NAME}"
if [ ! -d "$BASE_PATH/backup" ]
then
mkdir -p "$BASE_PATH/backup"
mkdir -p "/data/logs/${SERVER_NAME}"
fi
HAS_OLD_JAR=`ls $BASE_PATH/jar | grep -c '${SERVER_NAME}.jar'`
echo $HAS_OLD_JAR
if [ $HAS_OLD_JAR -gt 0 ]
then
echo "backup old jar $HAS_OLD_JAR"
mv -f ${BASE_PATH}/jar/${SERVER_NAME}.jar ${BASE_PATH}/backup_data/
fi
mv -f /data/jar/${SERVER_NAME}.jar $BASE_PATH/jar
JAR_FILE="${BASE_PATH}/jar/${SERVER_NAME}.jar"
if [ -f "$JAR_FILE" ]
then
echo "run : ps -ef | grep $SERVER_NAME | grep -v grep|awk {print$2}"
OLD_PROCESS=`ps -ef |grep $SERVER_NAME |grep -c -v grep`
PID=`ps -ef | grep $SERVER_NAME | grep -v grep | awk '{print$2}'`
echo "PID is $PID"
if [ $PID -gt 0 ]
then
kill -9 $PID
echo "kill -9 $PID"
else
echo "needn't kill ~!"
fi
echo 9
echo deploying $JAR_FILE
chmod 777 $JAR_FILE
echo 10
echo "start server $JAR_FILE"
nohup java -jar -Xms256m -Xmx256m $JAR_FILE --spring.profiles.active=dev > /data/logs/${SERVER_NAME}/${SERVER_NAME}.log 2>&1&
echo "$JAR_FILE published"
else
echo "error : $JAR_FILE not exists"
exit 1
fi