Shell脚本-自动化部署WEB
一、打包项目
1、从git克隆代码到本地
2、项目源码存储在/data/path/project_name目录下,eg:/data/back-end/peach
3、通过maven命令将jar/war安装到本地仓库
4、脚本文件取名为:install.sh
#! /bin/bash if [ $# -ne 3 ] then echo "#### 参数有误,\$1:相对路径,\$2:项目名,\$3:分支版本" echo "#### 输入参数格式如:" echo "#### back-end peach master" echo "#### back-end pear v2.0" exit 1 fi echo "#### 输入参数,\$1:$1,\$2:$2,\$3:$3" project_path=/data/$1/$2 echo "#### $2项目源码存储路径:"$project_path rm -rf $project_path echo "#### $2项目源码删除完毕" git_path="git@gitlab.xm6f.com:"$1/$2".git" echo '#### git克隆代码 from '$git_path' to '$project_path git clone $git_path $project_path echo '#### git克隆代码完毕.....' branchName=$2_$3 if [ "$3" == "master" ] then branchName="master" fi cd $project_path git checkout $branchName git pull mvn clean install echo "#### $2项目打包完毕....."
二、部署项目
#! /bin/bash if [ $# -ne 4 ] then echo "#### 参数有误,\$1:项目名,\$2:tomcat路径,\$3:ip地址, \$4:war path" exit 1 fi scp $4 root@$3:$2"/temp/" ssh root@$3 << eeooff cd $2 #####查找tomcat进程并杀死 ps -ef | grep '$2' | grep -v grep | awk '{print \$2}' | xargs kill -9 mv "webapps/"$1".war" "temp/"$1"_"$(date +%Y%m%d%H%M%S)".war" mv "temp/"$1".war" webapps/ rm -rf webapps/$1 rm -rf webapps/ROOT* $1 rm -rf work/ rm -rf temp/$1".war" bin/startup.sh & exit eeooff
三、自动化部署web项目
1、脚本文件取名为:startup.sh
2、执行命名:./startup.sh pear master
#! /bin/bash if [ $# -ne 2 ] then echo "#### 参数有误,\$1:项目名,\$2:分支版本" echo "#### 输入参数格式如:peach master | pear v2.0 | pear v2.0_dzy" exit 1 fi war_path="/data/back-end/$1/target/$1.war" remote_tomcat=/home/xm6f/dev/ sh install.sh back-end monkey $2 if [ "$1" == "peach" ] then sh install.sh back-end banana $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat8088" remote_ip=192.168.1.160 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "mango" ] then sh install.sh back-end banana $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat-8081" remote_ip=192.168.1.35 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "pear" ] then sh install.sh back-end banana $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat-7.0.79/tomcat-8080" remote_ip=192.168.1.105 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "apple" ] then sh install.sh back-end banana $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat-7.0.79/tomcat-8082" remote_ip=192.168.1.105 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "tomato" ] then sh install.sh back-end banana $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat-7.0.79/tomcat-8081" remote_ip=192.168.1.105 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "pig" ] then sh install.sh back-end tiger $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat8081" remote_ip=192.168.1.160 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "deer" ] then sh install.sh back-end tiger $2 sh install.sh back-end $1 $2 remote_tomcat=$remote_tomcat"tomcat-8083" remote_ip=192.168.1.160 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "lotmall" ] then sh install.sh back-end $1 $2 remote_tomcat="/www/server/tomcat" remote_ip=192.168.1.35 sh deploy.sh $1 $remote_tomcat $remote_ip $war_path elif [ "$1" == "order" ] then sh install.sh back-end tiger $2 sh install.sh back-end $1 $2 else echo "####没有找到匹配的项目名" fi