一、前言
因为使用Jenkins自动部署有很多未知问题,自己写脚本来部署也是可以的。
二、实现过程
创建脚本o2o-erp-order-dsc.sh和o2o-erp-order_all.sh
#!/bin/bashsource /etc/profileprofileName="local_dev"projectTag="60049-o2o-erp-order-dsc"svn update /data/code/trunk/java/microservice/o2o-erp-order/shell/o2o-erp-order_all.shsh /data/code/trunk/java/microservice/o2o-erp-order/shell/o2o-erp-order_all.sh "${profileName}" "${projectTag}"
#!/bin/bashsource /etc/profileexport M2_HOME=/data/apache-maven-3.0.3export M2=$M2_HOME/binexport MAVEN_OPTS="-Xms256m -Xmx512m"export PATH=$M2:$PATH:echo -e "\n\n profile name:$1 \n"if [ $# == 2 ] ; then startProject="$2" echo -e "start project :$startProject \n\n"ficd /data/code/trunk/java/common/o2o-common && svn update . && mvn clean install -Dmaven.test.skip=trueif [ $? -eq 0 ];then echo "mvn install o2o-common success!";else echo "mvn install o2o-common fail!";exit $?;ficd /data/code/trunk/java/microservice/o2o-basis-data && svn update . && mvn clean install -Dmaven.test.skip=trueif [ $? -eq 0 ];then echo "mvn install o2o-basis-data success!";else echo "mvn install o2o-basis-data fail!";exit $?;ficd /data/code/trunk/java/microservice/o2o-erp-order && svn update . && mvn clean install -Dmaven.test.skip=trueif [ $? -eq 0 ];then echo "mvn install o2o-erp-order success!";else echo "mvn install o2o-erp-order fail!";exit $?;fi# 如果传递了第二个参数,那么只启动指定的项目======================if [ $# == 2 ] ; then kill -9 `netstat -nlp | grep "${startProject%%-*}" | awk '{print $7}'|awk -F/ '{print $1}'` # start o2o-erp-order- 某个project cd /data/code/trunk/java/microservice/o2o-erp-order/"${startProject#*-}" && mvn clean package -Dmaven.test.skip=true -P"$1" if [ $? -eq 0 ];then echo "mvn package ${startProject#*-} success!";else echo "mvn package ${startProject#*-} fail!";exit $?;fi rm -rf /data/webservers/"${startProject}"/webapps/* cp -r /data/code/trunk/java/microservice/o2o-erp-order/"${startProject#*-}"/target/"${startProject#*-}".war /data/webservers/"${startProject}"/webapps/ROOT.war sh /data/webservers/"${startProject}"/bin/catalina.sh start tail -f /data/webservers/"${startProject}"/logs/catalina.out break;fi# 如果传递了第二个参数,那么只启动指定的项目======================# 启动全部项目===========================================# start o2o-erp-order-dsccd /data/code/trunk/java/microservice/o2o-erp-order/o2o-erp-order-dsc && mvn clean package -Dmaven.test.skip=true -P"$1"if [ $? -eq 0 ];then echo "mvn package o2o-erp-order-dsc success!";else echo "mvn package o2o-erp-order-dsc fail!";exit $?;fikill -9 `netstat -nlp | grep 60049 | awk '{print $7}'|awk -F/ '{print $1}'`rm -rf /data/webservers/60049-o2o-erp-order-dsc/webapps/*cp -r /data/code/trunk/java/microservice/o2o-erp-order/o2o-erp-order-dsc/target/o2o-erp-order-dsc.war /data/webservers/60049-o2o-erp-order-dsc/webapps/ROOT.warsh /data/webservers/60049-o2o-erp-order-dsc/bin/catalina.sh starttail -f /data/webservers/60049-o2o-erp-order-dsc/logs/catalina.out# start o2o-erp-order-webcd /data/code/trunk/java/microservice/o2o-erp-order/o2o-erp-order-web && mvn clean package -Dmaven.test.skip=true -P"$1"if [ $? -eq 0 ];then echo "mvn package o2o-erp-order-web success!";else echo "mvn package o2o-erp-order-web fail!";exit $?;fikill -9 `netstat -nlp | grep 60149 | awk '{print $7}'|awk -F/ '{print $1}'`rm -rf /data/webservers/60149-o2o-erp-order-web/webapps/*cp -r /data/code/trunk/java/microservice/o2o-erp-order/o2o-erp-order-web/target/o2o-erp-order-web.war /data/webservers/60149-o2o-erp-order-web/webapps/ROOT.warsh /data/webservers/60149-o2o-erp-order-web/bin/catalina.sh starttail -f /data/webservers/60149-o2o-erp-order-web/logs/catalina.out# start o2o-erp-order-jobcd /data/code/trunk/java/microservice/o2o-erp-order/o2o-erp-order-job && mvn clean package -Dmaven.test.skip=true -P"$1"if [ $? -eq 0 ];then echo "mvn package o2o-erp-order-job success!";else echo "mvn package o2o-erp-order-job fail!";exit $?;fikill -9 `netstat -nlp | grep 60249 | awk '{print $7}'|awk -F/ '{print $1}'`rm -rf /data/webservers/60249-o2o-erp-order-job/webapps/*cp -r /data/code/trunk/java/microservice/o2o-erp-order/o2o-erp-order-job/target/o2o-erp-order-job.war /data/webservers/60249-o2o-erp-order-job/webapps/ROOT.warsh /data/webservers/60249-o2o-erp-order-job/bin/catalina.sh starttail -f /data/webservers/60249-o2o-erp-order-job/logs/catalina.outecho -e "\n\nall package ok, test Tomcat...\n\n"# judge is start ok======================================================# curl -X GET --header 'Accept: text/plain' 'http://localhost:60149/health.do'# /usr/bin/wget -S --spider --tries=3 --timeout=3 http://localhost:60149/health.docluster="localhost:60049|o2o-erp-order-dsc localhost:60149|o2o-erp-order-web localhost:60249|o2o-erp-order-job"serviceCount=0 # 服务数量successCount=0 # 启动成功的服务数量for server in $clusterdo let serviceCount++ echo "$server"|while IFS="|" read f1 f2 do tryCount=1 # 重试次数 host="$f1" serviceName="$f2" while [ $tryCount -le 10 ]; do result=`/usr/bin/wget -S --spider --tries=3 --timeout=3 http://$host/health.do 2>&1 | grep HTTP/1.1 | awk '{print $2}'` if [ "$result" = "200" ] then echo -e "\nTomcat $host [$serviceName] Running OK..." let successCount++ break else echo -e ".\c" let tryCount++ sleep 5 fi if [ $tryCount -gt 10 ] then echo "Tomcat $host [$serviceName] Running Test more than 20 times. Tomcat maybe start fail !" break; fi done donedoneecho -e "all process finish. Success: $successCount, Total Process: $serviceCount\n"exit