一、打包项目
1、从git克隆代码到本地
2、项目源码存储在/data/path/project_name目录下,eg:/data/back-end/peach3、通过maven命令将jar/war安装到本地仓库4、脚本文件取名为:install.sh#! /bin/bashif [ $# -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/$2echo "#### $2项目源码存储路径:"$project_pathrm -rf $project_pathecho "#### $2项目源码删除完毕"git_path="git@gitlab.xm6f.com:"$1/$2".git"echo '#### git克隆代码 from '$git_path' to '$project_pathgit clone $git_path $project_pathecho '#### git克隆代码完毕.....'branchName=$2_$3if [ "$3" == "master" ]then branchName="master"ficd $project_pathgit checkout $branchNamegit pullmvn clean installecho "#### $2项目打包完毕....."
二、部署项目
#! /bin/bashif [ $# -ne 4 ]then echo "#### 参数有误,\$1:项目名,\$2:tomcat路径,\$3:ip地址, \$4:war path"exit 1fiscp $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 & exiteeooff
三、自动化部署web项目
1、脚本文件取名为:startup.sh
2、执行命名:./startup.sh pear master
#! /bin/bashif [ $# -ne 2 ] then echo "#### 参数有误,\$1:项目名,\$2:分支版本" echo "#### 输入参数格式如:peach master | pear v2.0 | pear v2.0_dzy" exit 1 fiwar_path="/data/back-end/$1/target/$1.war"remote_tomcat=/home/xm6f/dev/sh install.sh back-end monkey $2if [ "$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_pathelif [ "$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_pathelif [ "$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_pathelif [ "$1" == "order" ] then sh install.sh back-end tiger $2 sh install.sh back-end $1 $2else echo "####没有找到匹配的项目名"fi