shunde-test_build.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/bash
  2. #set -x
  3. set -e
  4. codePath=/data/wms85std_shunde_test
  5. gitUrlArr=(
  6. "http://git.yvanui.com/jztd/wms85std.git"
  7. "http://git.yvanui.com/lizhiwei/yvan-framework.git"
  8. "http://git.yvanui.com/luoyifan/yvan-ui.git"
  9. )
  10. branchArr=(
  11. "master"
  12. "8.6"
  13. "master"
  14. )
  15. positionArr=(
  16. "."
  17. "yvan-framework"
  18. "yvan-ui"
  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-ui
  67. if [ "${needBuild[0]}" != "0" ]; then
  68. echo -e "$echoPrefix cd $codePath/yvan-ui $echoSuffix"
  69. cd "$codePath/yvan-ui"
  70. echo -e "$echoPrefix yarn run init $echoSuffix"
  71. yarn run init
  72. echo ""
  73. else
  74. echo "yvan-ui 文件未变化"
  75. fi
  76. # 构建 wms-ui
  77. if [ "${needBuild[0]}" != "0" ]; then
  78. echo -e "$echoPrefix cd $codePath/wms-ui $echoSuffix"
  79. cd "$codePath/wms-ui"
  80. #echo -e "$echoPrefix yarn $echoSuffix"
  81. #yarn
  82. echo -e "$echoPrefix yarn link yvan-ui $echoSuffix"
  83. yarn link yvan-ui
  84. echo ""
  85. else
  86. echo "yvan-ui 文件未变化"
  87. fi
  88. # 构建 java
  89. echo -e "$echoPrefix cd $codePath $echoSuffix"
  90. cd $codePath
  91. echo -e "$echoPrefix gradle build -x test $echoSuffix"
  92. gradle build -x test
  93. echo ""
  94. }
  95. logPath=/data/logs/wms85std_shunde_test/server.log
  96. dirPath=/data/wms85std_shunde_test/wms-modules/wms-system/build/libs/
  97. serverName=wms85std_shunde_test
  98. cmd=$1
  99. profiles=${2:-"shunde-test"}
  100. #-------------------------------------------------------------------
  101. JAVA_MEM_OPTS=" -DappName=${serverName} -server -Xmx4g -Xms4g"
  102. DATABASE_OPTS=" -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
  103. JAVA_OPTS_EXT=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai"
  104. #-------------------------------------------------------------------
  105. startServer() {
  106. pid=$1
  107. if [ -z $pid ];then
  108. echo -e "$echoPrefix cd $dirPath../runtime/ $echoSuffix"
  109. cd $dirPath../runtime/
  110. echo -e "$echoPrefix java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./wms-system-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8005 >>/dev/null 2>&1 & $echoSuffix"
  111. java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./wms-system-0.0.0-SNAPSHOT.jar --spring.profiles.active=global,$profiles --server.port=8005 >>/dev/null 2>&1 &
  112. echo "$serverName 启动成功!"
  113. else
  114. echo "$serverName 正在运行..."
  115. fi
  116. echo "查看日志: tail -F $logPath -n 100"
  117. }
  118. stopServer() {
  119. pid=$1
  120. if [ -z $pid ];then
  121. echo "$serverName 未运行"
  122. else
  123. echo -e "$echoPrefix ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill $echoSuffix"
  124. ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}' | xargs kill
  125. echo "$serverName 已停止!"
  126. fi
  127. }
  128. deployPull() {
  129. for ((idx=0; idx<${#gitUrlArr[@]}; idx++)); do
  130. pullCode $codePath ${gitUrlArr[idx]} ${branchArr[idx]} ${positionArr[idx]}
  131. if [ "${needBuild[idx]}" == "-1" ]; then
  132. needBuild[idx]=$isChange
  133. fi
  134. done
  135. echo "###--代码更新完成--------------------------------------------------------------------------###"
  136. echo ""
  137. }
  138. deployServer() {
  139. pid=$1
  140. deployPull
  141. buildCode
  142. echo "###--代码构建完成--------------------------------------------------------------------------###"
  143. # 重启服务
  144. if [ ! -z $pid ];then
  145. stopServer $pid
  146. sleep 8s
  147. fi
  148. # 移动编译后的文件
  149. echo -e "$echoPrefix cd $dirPath $echoSuffix"
  150. cd $dirPath
  151. echo -e "$echoPrefix rm -rf ../runtime $echoSuffix"
  152. rm -rf ../runtime
  153. echo -e "$echoPrefix cp -r ./ ../runtime $echoSuffix"
  154. cp -r ./ ../runtime
  155. echo ""
  156. startServer
  157. }
  158. restartServer() {
  159. pid=$1
  160. if [ ! -z $pid ];then
  161. stopServer $pid
  162. sleep 3s
  163. fi
  164. startServer
  165. }
  166. logs() {
  167. tail -F $logPath -n 100
  168. }
  169. pid=`ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}'`
  170. # 操作参数: pull deploy restart start stop kill log logs
  171. if [ "$cmd" == "pull" ];then
  172. deployPull
  173. if [ -z $pid ];then
  174. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  175. fi
  176. elif [ "$cmd" == "deploy" ];then
  177. deployServer $pid
  178. elif [ "$cmd" == "restart" ];then
  179. restartServer $pid
  180. elif [ "$cmd" == "start" ];then
  181. startServer $pid
  182. elif [ "$cmd" == "stop" ] || [ "$cmd" == "kill" ];then
  183. stopServer $pid
  184. elif [ "$cmd" == "log" ] || [ "$cmd" == "logs" ];then
  185. logs
  186. else
  187. if [ -z $pid ];then
  188. echo "$serverName 未运行 | 输入操作参数: pull deploy restart start stop kill log logs"
  189. else
  190. echo "输入操作参数: pull deploy restart start stop kill log logs"
  191. echo "pid=$pid | $serverName 正在运行... | 查看日志: tail -F $logPath -n 100"
  192. fi
  193. fi
  194. # 顺德-test
  195. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/dsl/shunde-test_build.sh) [cmd profiles]