You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

deploy.sh 5.2 kB

1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/bin/bash
  2. # 记录开始时间
  3. start=$(date +%s)
  4. # 默认参数
  5. service="manage-front"
  6. env="dev"
  7. show_help() {
  8. echo "Usage: $0 [-s service] [-e environment]"
  9. echo
  10. echo "Options:"
  11. echo " -s Service to deploy (manage-front, manage, front, all, system default: manage-front)"
  12. echo " -e Environment (e.g., dev, test, default: dev)"
  13. echo " -h Show this help message"
  14. }
  15. # 解析命令行参数
  16. while getopts "s:e:h" opt; do
  17. case $opt in
  18. s) service=$OPTARG ;;
  19. e) env=$OPTARG ;;
  20. h) show_help; exit 0 ;;
  21. \?) echo "Invalid option -$OPTARG" >&2; exit 1 ;;
  22. esac
  23. done
  24. echo "Deploy service: $service, environment: $env"
  25. valid_services=("manage-front" "manage" "front" "all" "auth" "gateway" "system")
  26. if [[ ! " ${valid_services[@]} " =~ " $service " ]]; then
  27. echo "Invalid service name: $service" >&2
  28. echo "Valid services are: ${valid_services[*]}"
  29. exit 1
  30. fi
  31. valid_envs=("dev" "test" "test1")
  32. if [[ ! " ${valid_envs[@]} " =~ " $env " ]]; then
  33. echo "Invalid environment: $env" >&2
  34. echo "Valid environments are: ${valid_envs[*]}"
  35. exit 1
  36. fi
  37. # 根据环境设置 IP 地址
  38. if [ "$env" == "dev" ]; then
  39. remote_ip="172.20.32.197"
  40. elif [ "$env" == "test" ]; then
  41. remote_ip="172.20.32.185"
  42. elif [ "$env" == "test1" ]; then
  43. remote_ip="172.20.32.235"
  44. else
  45. echo "Invalid environment - $env"
  46. exit 1
  47. fi
  48. baseDir=/home/somuns/ci4s
  49. tag=$(date +'%Y%m%d%H%M')
  50. remote_deploy_dir=/home/deploy/manage-platform
  51. # 构建镜像函数
  52. build_image() {
  53. local dockerfile=$1
  54. local image=$2
  55. cd ${baseDir}/k8s/dockerfiles
  56. docker build -t ${image} -f ${dockerfile} .
  57. if [ $? -ne 0 ]; then
  58. echo "Build ${image} image fail"
  59. exit 1
  60. fi
  61. docker push ${image}
  62. }
  63. # 复制和替换 YAML 文件函数
  64. prepare_yaml() {
  65. local yaml_file=$1
  66. local image=$2
  67. placeholder="\${${yaml_file%.yaml}-image}"
  68. cd ${baseDir}/k8s/template-yaml
  69. cp -rf ${yaml_file} deploy/
  70. cd deploy/
  71. sed -i "s|${placeholder}|${image}|g" ${yaml_file}
  72. if [ $? -ne 0 ]; then
  73. echo "Replace ${image} image fail"
  74. exit 1
  75. fi
  76. # 建立远程目录并备份文件
  77. ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi"
  78. if [ $? -ne 0 ]; then
  79. echo "Failed to create remote directory or backup ${yaml_file}"
  80. exit 1
  81. else
  82. echo "Successfully created remote directory and backup ${yaml_file}"
  83. fi
  84. scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
  85. if [ $? -ne 0 ]; then
  86. echo "Failed to copy ${yaml_file}"
  87. exit 1
  88. else
  89. echo "Successfully copied ${yaml_file}"
  90. fi
  91. }
  92. # 部署服务函数
  93. deploy_service() {
  94. local yaml_file=$1
  95. ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}"
  96. if [ $? -ne 0 ]; then
  97. echo "Failed to deploy ${yaml_file}"
  98. exit 1
  99. else
  100. echo "Successfully deployed ${yaml_file}"
  101. fi
  102. }
  103. deploy_nacos() {
  104. local yaml_file=$1
  105. scp ${baseDir}/k8s/template-yaml/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
  106. deploy_service ${yaml_file}
  107. }
  108. build_and_deploy() {
  109. local dockerfile=$1
  110. local image=$2
  111. local yaml_file=$3
  112. build_image ${dockerfile} ${image}
  113. prepare_yaml ${yaml_file} ${image}
  114. deploy_service ${yaml_file}
  115. }
  116. if [ "$service" == "front" ]; then
  117. build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
  118. fi
  119. # 构建和部署 manage 服务
  120. if [ "$service" == "manage" ]; then
  121. build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
  122. fi
  123. if [ "$service" == "auth" ]; then
  124. #部署认证中心
  125. build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml"
  126. fi
  127. if [ "$service" == "gateway" ]; then
  128. #部署网关
  129. build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml"
  130. fi
  131. if [ "$service" == "system" ]; then
  132. #部署系统服务
  133. build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml"
  134. fi
  135. # 构建和部署 front 服务
  136. if [ "$service" == "manage-front" ]; then
  137. build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
  138. build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
  139. fi
  140. if [ "$service" == "all" ]; then
  141. #部署前端
  142. build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
  143. #部署管理平台
  144. build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
  145. #部署认证中心
  146. build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml"
  147. #部署网关
  148. build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml"
  149. #部署系统服务
  150. build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml"
  151. #部署配置中心
  152. deploy_nacos "k8s-3nacos.yaml"
  153. fi
  154. # 记录结束时间
  155. end=$(date +%s)
  156. echo "部署成功, 耗时: $((end-start))秒"