01deploy.sh 9.2 KB

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