Shell常用脚本:SpringBoot、Vue项目部署 - 文件夹配置根据自己需求进行修改

x33g5p2x  于2022-02-18 转载在 Shell  
字(7.6k)|赞(0)|评价(0)|浏览(485)
脚本1 - 以前使用

deployment.sh

#!/bin/bash
#
#   注意:
#      1. 需要你安装rsync拷贝工具:yum install rsync
#      2. 需要将Tomcat作为服务,如何设置自行百度,因为我是用的宝塔面板装的Tomcat,它自行帮我作为服务运行了  
#      3. deploymentDir、jenkinProjectBaseDir目录是以我的环境默认设置的,如果你也想使用默认,自修修改下面的目录值即可,如果运行脚本没有传参进来,也没按你环境修改下面的默认值,后果自负
#
# 【说明】运行该脚本时可填入的参数
#   $1 => projectName[must]: 项目名 = Jenkin任务名称, 部署的jar包名字, 项目名三者跟此参数一致,否则后果自负
#   $2 => projectPort[must]: 项目端口号 
#   $3 => projectPackged[option]: 从仓库拉取的代码是否已经打包好  0未打包[默认值]   1已打包
#   $4 => projetType[option]: 项目类型: SpringBoot[默认值]、vue
#   $5 => sourceBasePath[option]: 绝对路径, 存放多个项目的目录 == 类似IDEA的workspace目录
#   $6 => deploymentDirPath[option]: 绝对路径, 部署多个项目的目录 == 类似Tomcat的webapps目录



echo "当前目录: $(pwd)"

tomcatWebappsDir='/www/server/tomcat8/webapps'

# jenkin所有源码项目目录
jenkinProjectBaseDir='/var/lib/jenkins/workspace'
if [ "$5" ]; then
  jenkinProjectBaseDir=$5
fi

# 部署目录
deploymentDir='/www/project'
if [ "$6" ]; then
  deploymentDir=$6
elif [ "$4" = 'vue'  ]; then
   deploymentDir=$tomcatWebappsDir
fi

# 输入参数校验, 必须填入打包后的项目名以及项目部署的端口号
if [ ! "$1" ] || [ ! "$2" ]; then
  echo '缺失参数, 正确格式为: sh 脚本绝对路径 项目名 项目端口号'
  exit 1
elif [ ! -d "$jenkinProjectBaseDir/$1" ]; then
  echo "源码目录不存在: $jenkinProjectBaseDir/$1 , 请检查"
  exit 1
else
  echo "源码目录存在: $jenkinProjectBaseDir/$1"
fi



# 用于新项目,如果是旧项目更新部署一般都不会执行这块
if [ ! -d "$deploymentDir/$1/backup" ]; then
  mkdir -p "$deploymentDir/$1/backup"
fi

if [ ! -f "$deploymentDir/$1/nohup.out" ]; then
  touch "$deploymentDir/$1/nohup.out"
fi


# 0. 打包最新的源码项目
# == cd执行不成功直接退出
echo "====打包中 $1:$2===="
cd "$jenkinProjectBaseDir/$1" || exit 1
if [ "$3" != '1' ]; then
  if [ "$4" = 'vue' ]; then
    npm install
    npm run build
  else
    mvn clean package -Dmaven.test.skip=true -P pro
  fi
#else
#  rm -rf "${jenkinProjectBaseDir:?'parameter deploymentDir is null, You want to delete the library and run away?'}"/"$1"*
fi

echo "====打包完成 $1:$2===="

sleep 10s

# 1. 定义当前shell文件下面所执行的命令以这个目录为基准(用于相对路径) == cd执行不成功直接退出
cd "$deploymentDir/$1" || exit 1
echo "当前目录: $(pwd)"

# 2. 杀端口为第二个参数的进程
echo "====正在关闭进程 $1:$2===="
if [ "$4" != 'vue' ]; then
  projectPID=$(netstat -nlp | grep :"$2" | awk '{print $7}' | awk -F"/" '{ print $1 }')
  if [ "$projectPID" ]; then
    kill -9 "$projectPID"  
  fi
else 
  service tomcat8 stop
fi
echo "====进程关闭完成 $1:$2===="

sleep 1s

# 3. 备份上一个版本的jar包,并从maven拉取最新打包的jar包
echo '====正在备份旧版本===='
# SpringBoot项目
if [ "$4" != 'vue' ]; then
  rm -rf ./backup/"$1"*
  mv "./$1.jar" "./backup/$1.jar.$(date '+%Y%m%d%H%M%S')"
  mv ./nohup.out "./backup/nohup.out.$(date '+%Y%m%d%H%M%S')"
  cp "$jenkinProjectBaseDir/$1/target/$1.jar" "$deploymentDir/$1/$1.jar" 
  touch nohup.out
# Vue项目
else
  currentProjectBackupDirName="${1}Backup$(date '+%Y%m%d%H%M%S')"
  currentProjectBackupDir="$deploymentDir/$currentProjectBackupDirName"
  mkdir "$currentProjectBackupDir"
  # 需要安装这个工具:yum install rsync
  rsync -r --exclude "${1}Backup*/"  ./* "$currentProjectBackupDir"
  rm -rf  ${deploymentDir:?'parameter deploymentDir is null, You want to delete the library and run away?'}/"$1"/*
  if [ "$3" = '1' ]; then
    cp -r "$jenkinProjectBaseDir"/"$1"/* "$deploymentDir"/"$1"/
  else
    cp -r "$jenkinProjectBaseDir"/"$1"/dist/* "$deploymentDir"/"$1"/
  fi
  mv "$currentProjectBackupDir" "$deploymentDir"/"$1"/
fi
echo '====旧版本备份完成===='

sleep 2s

# 4. 启动项目
echo "====$1:$2 项目部署中===="
if [ "$4" != 'vue' ]; then
  nohup java -jar "$1.jar" > nohup.out 2>&1 &
else
  service tomcat8 start
fi
echo "====$1:$2 项目部署完成==="
脚本2 - 当前正在使用

脚本1

#!/bin/bash
#
#   注意:
#      1. 需要你安装rsync拷贝工具:yum install rsync
#      2. 需要将Tomcat作为服务,如何设置自行百度,因为我是用的宝塔面板装的Tomcat,它自行帮我作为服务运行了  
#      3. deploymentDir、jenkinProjectBaseDir目录是以我的环境默认设置的,如果你也想使用默认,自修修改下面的目录值即可,如果运行脚本没有传参进来,也没按你环境修改下面的默认值,后果自负
#
# 【说明】运行该脚本时可填入的参数
#   $1 => projectName[must]: 项目名 = Jenkin任务名称, 部署的jar包名字, 项目名三者跟此参数一致,否则后果自负
#   $2 => projectPort[must]: 项目端口号 
#   $3 => projectPackged[option]: 从仓库拉取的代码是否已经打包好  0未打包[默认值]   1已打包
#   $4 => projetType[option]: 项目类型: SpringBoot[默认值]、vue
#   $5 => tomcatFlag[option]:是否需要部署到Tomcat中 0不需要(默认) 1需要
#   $6 => sourceBasePath[option]: 绝对路径, 存放多个项目的目录 == 类似IDEA的workspace目录
#   $7 => deploymentDirPath[option]: 绝对路径, 部署多个项目的目录 == 类似Tomcat的webapps目录



echo "当前目录: $(pwd)"

tomcatWebappsDir='/www/server/tomcat8/webapps'

# jenkin所有源码项目目录
jenkinProjectBaseDir='/var/lib/jenkins/workspace'
if [ "$6" ]; then
  jenkinProjectBaseDir=$6
fi

# 部署目录
deploymentDir='/www/project'
if [ "$7" ]; then
  deploymentDir=$7
elif [ "$4" = 'vue' ] && [ && "$5" = '1' ]; then
   deploymentDir=$tomcatWebappsDir
fi

# 输入参数校验, 必须填入打包后的项目名以及项目部署的端口号
if [ ! "$1" ] || [ ! "$2" ]; then
  echo '缺失参数, 正确格式为: sh 脚本绝对路径 项目名 项目端口号'
  exit 1
elif [ ! -d "$jenkinProjectBaseDir/$1" ]; then
  echo "源码目录不存在: $jenkinProjectBaseDir/$1 , 请检查"
  exit 1
else
  echo "源码目录存在: $jenkinProjectBaseDir/$1"
fi



# 用于新项目,如果是旧项目更新部署一般都不会执行这块
if [ ! -d "$deploymentDir/$1/backup" ]; then
  mkdir -p "$deploymentDir/$1/backup"
fi

if [ ! -f "$deploymentDir/$1/nohup.out" ] && [ "$4" != 'vue' ]; then
  touch "$deploymentDir/$1/nohup.out"
fi


# 0. 打包最新的源码项目
# == cd执行不成功直接退出
echo "====打包中 $1:$2===="
cd "$jenkinProjectBaseDir/$1" || exit 1
if [ "$3" != '1' ]; then
  if [ "$4" = 'vue' ]; then
    npm install
    npm run build
  else
    mvn clean package -Dmaven.test.skip=true -P pro
  fi
fi

echo "====打包完成 $1:$2===="

sleep 10s

# 1. 定义当前shell文件下面所执行的命令以这个目录为基准(用于相对路径) == cd执行不成功直接退出
cd "$deploymentDir/$1" || exit 1
echo "当前目录: $(pwd)"

# 2. 杀端口为第二个参数的进程
echo "====正在关闭进程 $1:$2===="
if [ "$4" != 'vue' ]; then
  projectPID=$(netstat -nlp | grep :"$2" | awk '{print $7}' | awk -F"/" '{ print $1 }')
  if [ "$projectPID" ]; then
    kill -9 "$projectPID"  
  fi
elif [ "$5" = '1' ]; then
  service tomcat8 stop
fi
echo "====进程关闭完成 $1:$2===="

sleep 1s

# 3. 备份上一个版本的jar包,并从maven拉取最新打包的jar包
echo '====正在备份旧版本===='

currentProjectBackupDir="/www/project/backup_project"
if [ ! -d "$currentProjectBackupDir" ]; then
  mkdir -p $currentProjectBackupDir
fi

dateTime="$(date '+%Y%m%d%H%M%S')"

# SpringBoot项目
if [ "$4" != 'vue' ]; then
  # rm -rf ./backup/"$1"*
  mv "./$1.jar" "$currentProjectBackupDir/$1.jar.$dateTime"
  mv ./nohup.out "$currentProjectBackupDir/$1.nohup.out.$dateTime"
  cp "$jenkinProjectBaseDir/$1/target/$1.jar" "$deploymentDir/$1/$1.jar" 
  touch nohup.out
# Vue项目
else
  currentProjectBackupFileName="${1}.$dateTime"
  # cd "$deploymentDir"
  tar -zcPf "$currentProjectBackupDir/$currentProjectBackupFileName".tar.gz "$deploymentDir/${1}"
  # mv "$currentProjectBackupFileName=".tar.gz "$currentProjectBackupDir"
  # 需要安装这个工具:yum install rsync
  # rsync -r --exclude "${1}Backup*/"  ./* "$currentProjectBackupDir"
  # rm -rf  ${deploymentDir:?'parameter deploymentDir is null, You want to delete the library and run away?'}/"$1"/*
  if [ "$3" = '1' ]; then
    cp -r "$jenkinProjectBaseDir"/"$1"/* "$deploymentDir"/"$1"/
  else
    cp -r "$jenkinProjectBaseDir"/"$1"/dist/* "$deploymentDir"/"$1"/
  fi
  # mv "$currentProjectBackupDir" "$deploymentDir"/"$1"/
  # cd "$deploymentDir"/"$1"/
  # tar -zcf "$currentProjectBackupDirName".tar.gz "$currentProjectBackupDirName"
  # rm -rf "$currentProjectBackupDirName"
fi
echo '====旧版本备份完成===='

sleep 2s

# 4. 启动项目
echo "====$1:$2 项目部署中===="
if [ "$4" != 'vue' ]; then
  nohup java -jar "$1.jar" > nohup.out 2>&1 &
elif [ "$5" = '1' ]; then
  service tomcat8 start
fi
echo "====$1:$2 项目部署完成==="

相关文章