deploy.sh 6.9 KB

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