#!/bin/bash
# 运行命令 # nohup java -jar ruoyi-admin.jar > ./nohup.out &
# 打包命令 # mvn package -P druid jia包会在 target/ruoyi-admin.jar
# jump_guest.pem 私钥
WORKSPACE="$1"
DATE=`date "+%Y-%m-%d %H:%M:%S"`
Jar='ruoyi-admin.jar'
Dir=/opt/scripts/ssyy99_scripts
echo "当前执行时间为:$DATE"
case $status in
Deploy)
echo "========= status:$status =========="
echo "========= 自定义输入version:$version =========="
echo $PATH
echo "========= 进入jenkins工作目录前端源码所在文件夹 =========="
source /etc/profile
cd $WORKSPACE
########## 用scp命令发动到远程服务器的部署目录下 ##########
echo "========== 开始上传jar包到测试服务器 =========="
sudo scp -P 59157 -r -i $Dir/jump_guest.pem $WORKSPACE/ruoyi-admin/target/$Jar jump_guest@192.168.10.1:/home/jump_guest/
echo "========= 将jar包备份到backup_jar文件夹 =========="
########## 备份在jenkins的工作目录下的backup_app文件夹下,方便之后回滚 ##########
########## $BUILD_NUMBER 为构建id,初始为1 每一次构建递增1 可用于回滚 ##########
########## $WORKSPACE 为项目工作目录 实际为jenkins安装目录+项目名字 ##########
########## $JENKINS_HOME 为jenkins安装目录 在此为 /var/lib/jenkins ##########
\cp $WORKSPACE/ruoyi-admin/target/$Jar $JENKINS_HOME/workspace/backup_jar/"$Jar".$BUILD_NUMBER
sudo find $JENKINS_HOME/workspace/backup_jar/ -type f -mtime +30 -exec rm -f {} \;
echo "deploy done!"
;;
Rollback)
backup_path="${JENKINS_HOME}/workspace/backup_jar/"
echo "======status:$status========"
echo "======build_id:$version======="
cd $backup_path
ls
echo "==============正在回滚 $Jar.$version 包 发送到远程测试服务器==================="
sudo scp -P 59157 -r -i $Dir/jump_guest.pem "$Jar".$version jump_guest@192.168.10.1:/home/jump_guest/$Jar
echo "roll_back done!"
;;
*)
esac