03wms84-test.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. #set -x
  3. set -e
  4. codePath=/home/www/deploy/wms84_test
  5. gitUrlArr=(
  6. "http://git.yvanui.com/jztd/wms84.git"
  7. "www@git.yvanui.com:lizhiwei/yvan-framework.git"
  8. # "http://git.yvanui.com/lizhiwei/yvan-framework.git"
  9. "www@git.yvanui.com:jztd/yvan-vue.git"
  10. # "http://git.yvanui.com/jztd/yvan-vue.git"
  11. "www@git.yvanui.com:jztd/yvan-vue-src.git"
  12. # "http://git.yvanui.com/jztd/yvan-vue-src.git"
  13. )
  14. branchArr=(
  15. "master"
  16. "8.4"
  17. "master"
  18. "master"
  19. )
  20. positionArr=(
  21. "."
  22. "yvan-framework"
  23. "yvan-vue"
  24. "yvan-vue-src"
  25. )
  26. # 是否需要构建,-1:自动;0:不需要;1:需要
  27. needBuild=(
  28. "1"
  29. "1"
  30. "1"
  31. "1"
  32. )
  33. echoPrefix="\033[36m+"
  34. echoSuffix="\033[0m"
  35. # 强制覆盖本地的代码 git fetch --all && git reset --hard origin/master
  36. isChange="1" # 仓库是否发生变化,0:未变化;非0:变化
  37. pullCode() {
  38. # Usage: pullCode "codePath" "gitUrl" "branch" "position"
  39. path=$1 # 代码保存路径
  40. gitUrl=$2 # git仓库地址
  41. branch=${3:-"master"} # git分支
  42. position=${4:-""} # 使用“.”clone到当前文件夹
  43. isChange="1"
  44. # 创建文件夹
  45. if [ ! -d "$path" ]; then
  46. echo -e "$echoPrefix 请检查服务器是否正确,再执行: mkdir -p $path $echoSuffix"
  47. exit
  48. fi
  49. # git clone
  50. echo -e "$echoPrefix cd $path $echoSuffix"
  51. cd $path
  52. if [ "`ls -A $path`" == "" ] || [ ! -d "$path/$position" ] || [ "`ls -A $path/$position`" = "" ]; then
  53. echo -e "$echoPrefix git clone $gitUrl $position $echoSuffix"
  54. git clone $gitUrl $position
  55. isChange="2"
  56. fi
  57. # 进入文件夹,切换分支,git pull
  58. if [ "$position" != "" ]; then
  59. echo -e "$echoPrefix cd $position $echoSuffix"
  60. cd $position
  61. fi
  62. echo -e "$echoPrefix git checkout $branch $echoSuffix"
  63. git checkout $branch
  64. echo -e "$echoPrefix git checkout . $echoSuffix"
  65. git checkout .
  66. echo -e "$echoPrefix git pull $echoSuffix"
  67. if [ "`git pull`" == "Already up-to-date." ] && [ $isChange == "1" ]; then
  68. isChange="0"
  69. fi
  70. echo ""
  71. }
  72. buildCode() {
  73. # 构建 yvan-vue-src
  74. if [ "${needBuild[0]}" != "0" ]; then
  75. echo -e "$echoPrefix cd $codePath/yvan-vue-src $echoSuffix"
  76. cd "$codePath/yvan-vue-src"
  77. echo -e "$echoPrefix yarn $echoSuffix"
  78. yarn
  79. echo -e "$echoPrefix yarn run dev2 $echoSuffix"
  80. yarn run dev2
  81. echo ""
  82. else
  83. echo "yvan-vue-src 文件未变化"
  84. fi
  85. # 构建 yvan-vue
  86. if [ "${needBuild[0]}" != "0" ]; then
  87. echo -e "$echoPrefix cd $codePath/yvan-vue $echoSuffix"
  88. cd "$codePath/yvan-vue"
  89. echo -e "$echoPrefix yarn $echoSuffix"
  90. yarn
  91. echo -e "$echoPrefix yarn link $echoSuffix"
  92. yarn link
  93. echo ""
  94. else
  95. echo "yvan-vue 文件未变化"
  96. fi
  97. # pc-client
  98. if [ "${needBuild[0]}" != "0" ]; then
  99. echo -e "$echoPrefix cd $codePath/pc-client $echoSuffix"
  100. cd "$codePath/pc-client"
  101. echo -e "$echoPrefix yarn $echoSuffix"
  102. yarn
  103. echo -e "$echoPrefix yarn link yvan-vue $echoSuffix"
  104. yarn link yvan-vue
  105. echo ""
  106. else
  107. echo "pc-client 文件未变化"
  108. fi
  109. # 构建 java
  110. echo -e "$echoPrefix cd $codePath $echoSuffix"
  111. cd $codePath
  112. echo -e "$echoPrefix gradle build -x test $echoSuffix"
  113. gradle build -x test
  114. echo ""
  115. }
  116. logPath=/data/logs/wms84_test/server.log
  117. dirPath=/home/www/deploy/wms84_test/wms/wms-application/build/libs/
  118. serverName=wms84_test
  119. cmd=$1
  120. profiles=${2:-"test"}
  121. #-------------------------------------------------------------------
  122. JAVA_MEM_OPTS=" -DappName=${serverName} -server -Xmx2g -Xms2g"
  123. DATABASE_OPTS=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  124. JAVA_OPTS_EXT=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  125. #-------------------------------------------------------------------
  126. startServer() {
  127. pid=$1
  128. if [ -z $pid ];then
  129. echo -e "$echoPrefix cd $dirPath../runtime/ $echoSuffix"
  130. cd $dirPath../runtime/
  131. echo -e "$echoPrefix java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./wms-application-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8004 >>/dev/null 2>&1 & $echoSuffix"
  132. java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./wms-application-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8004 >>/dev/null 2>&1 &
  133. echo "$serverName 启动成功!"
  134. else
  135. echo "$serverName 正在运行..."
  136. fi
  137. echo "查看日志: tail -F $logPath -n 100"
  138. }
  139. stopServer() {
  140. pid=$1
  141. if [ -z $pid ];then
  142. echo "$serverName 未运行"
  143. else
  144. echo -e "$echoPrefix ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill $echoSuffix"
  145. ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}' | xargs kill
  146. echo "$serverName 已停止!"
  147. fi
  148. }
  149. deployPull() {
  150. for ((idx=0; idx<${#gitUrlArr[@]}; idx++)); do
  151. pullCode $codePath ${gitUrlArr[idx]} ${branchArr[idx]} ${positionArr[idx]}
  152. if [ "${needBuild[idx]}" == "-1" ]; then
  153. needBuild[idx]=$isChange
  154. fi
  155. done
  156. echo "###--代码更新完成--------------------------------------------------------------------------###"
  157. echo ""
  158. }
  159. deployServer() {
  160. pid=$1
  161. deployPull
  162. buildCode
  163. echo "###--代码构建完成--------------------------------------------------------------------------###"
  164. # 重启服务
  165. if [ ! -z $pid ];then
  166. stopServer $pid
  167. sleep 8s
  168. fi
  169. # 移动编译后的文件
  170. echo -e "$echoPrefix cd $dirPath $echoSuffix"
  171. cd $dirPath
  172. echo -e "$echoPrefix rm -rf ../runtime $echoSuffix"
  173. rm -rf ../runtime
  174. echo -e "$echoPrefix cp -r ./ ../runtime $echoSuffix"
  175. cp -r ./ ../runtime
  176. echo ""
  177. startServer
  178. }
  179. restartServer() {
  180. pid=$1
  181. if [ ! -z $pid ];then
  182. stopServer $pid
  183. sleep 3s
  184. fi
  185. startServer
  186. }
  187. logs() {
  188. tail -F $logPath -n 100
  189. }
  190. pid=`ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}'`
  191. # 操作参数: pull deploy restart start stop kill log logs
  192. if [ "$cmd" == "pull" ];then
  193. deployPull
  194. if [ -z $pid ];then
  195. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  196. fi
  197. elif [ "$cmd" == "deploy" ];then
  198. deployServer $pid
  199. elif [ "$cmd" == "restart" ];then
  200. restartServer $pid
  201. elif [ "$cmd" == "start" ];then
  202. startServer $pid
  203. elif [ "$cmd" == "stop" ] || [ "$cmd" == "kill" ];then
  204. stopServer $pid
  205. elif [ "$cmd" == "log" ] || [ "$cmd" == "logs" ];then
  206. logs
  207. else
  208. if [ -z $pid ];then
  209. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  210. else
  211. echo "输入操作参数: pull deploy restart start stop kill log logs"
  212. echo "pid=$pid | $serverName 正在运行... | 查看日志: tail -F $logPath -n 100"
  213. fi
  214. fi
  215. # wms85测试
  216. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/hua_wei_yun/03wms84-test.sh) [cmd profiles]