03xxljob.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. #set -x
  3. set -e
  4. codePath=/home/www/deploy/xxl-job
  5. gitUrlArr=(
  6. "http://git.yvanui.com/lizhiwei/xxl-job.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. isChange="1"
  21. pullCode() {
  22. # Usage: pullCode "codePath" "gitUrl" "branch" "position"
  23. path=$1 # 代码保存路径
  24. gitUrl=$2 # git仓库地址
  25. branch=${3:-"master"} # git分支
  26. position=${4:-""} # 使用“.”clone到当前文件夹
  27. isChange="1"
  28. # 创建文件夹
  29. if [ ! -d "$path" ]; then
  30. echo -e "$echoPrefix 请检查服务器是否正确,再执行: mkdir -p $path $echoSuffix"
  31. exit
  32. fi
  33. # git clone
  34. echo -e "$echoPrefix cd $path $echoSuffix"
  35. cd $path
  36. if [ "`ls -A $path`" == "" ] || [ ! -d "$path/$position" ] || [ "`ls -A $path/$position`" = "" ]; then
  37. echo -e "$echoPrefix git clone $gitUrl $position $echoSuffix"
  38. git clone $gitUrl $position
  39. isChange="2"
  40. fi
  41. # 进入文件夹,切换分支,git pull
  42. if [ "$position" != "" ]; then
  43. echo -e "$echoPrefix cd $position $echoSuffix"
  44. cd $position
  45. fi
  46. echo -e "$echoPrefix git checkout $branch $echoSuffix"
  47. git checkout $branch
  48. echo -e "$echoPrefix git checkout . $echoSuffix"
  49. git checkout .
  50. echo -e "$echoPrefix git pull $echoSuffix"
  51. if [ "`git pull`" == "Already up-to-date." ] && [ $isChange == "1" ]; then
  52. isChange="0"
  53. fi
  54. echo ""
  55. }
  56. deployPull() {
  57. for ((idx=0; idx<${#gitUrlArr[@]}; idx++)); do
  58. pullCode $codePath ${gitUrlArr[idx]} ${branchArr[idx]} ${positionArr[idx]}
  59. if [ "${needBuild[idx]}" == "-1" ]; then
  60. needBuild[idx]=$isChange
  61. fi
  62. done
  63. echo "###--代码更新完成--------------------------------------------------------------------------###"
  64. echo ""
  65. }
  66. cmd=$1
  67. if [ "$cmd" == "pull" ];then
  68. deployPull
  69. else
  70. echo "输入操作参数: pull"
  71. fi
  72. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/03xxljob.sh) [cmd]