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 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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")
  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")
  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.181"
  40. elif [ "$env" == "test" ]; then
  41. remote_ip="172.20.32.185"
  42. else
  43. echo "Invalid environment - $env"
  44. exit 1
  45. fi
  46. baseDir=/home/somuns/ci4s
  47. tag=$(date +'%Y%m%d%H%M')
  48. remote_deploy_dir=/home/deploy/manage-platform
  49. # 构建镜像函数
  50. build_image() {
  51. local dockerfile=$1
  52. local image=$2
  53. cd ${baseDir}/k8s/dockerfiles
  54. docker build -t ${image} -f ${dockerfile} .
  55. if [ $? -ne 0 ]; then
  56. echo "Build ${image} image fail"
  57. exit 1
  58. fi
  59. docker push ${image}
  60. }
  61. # 复制和替换 YAML 文件函数
  62. prepare_yaml() {
  63. local yaml_file=$1
  64. local image=$2
  65. placeholder="\${${yaml_file%.yaml}-image}"
  66. cd ${baseDir}/k8s/template-yaml
  67. cp -rf ${yaml_file} deploy/
  68. cd deploy/
  69. sed -i "s|${placeholder}|${image}|g" ${yaml_file}
  70. if [ $? -ne 0 ]; then
  71. echo "Replace ${image} image fail"
  72. exit 1
  73. fi
  74. # 建立远程目录并备份文件
  75. 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"
  76. if [ $? -ne 0 ]; then
  77. echo "Failed to create remote directory or backup ${yaml_file}"
  78. exit 1
  79. else
  80. echo "Successfully created remote directory and backup ${yaml_file}"
  81. fi
  82. scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
  83. if [ $? -ne 0 ]; then
  84. echo "Failed to copy ${yaml_file}"
  85. exit 1
  86. else
  87. echo "Successfully copied ${yaml_file}"
  88. fi
  89. }
  90. # 部署服务函数
  91. deploy_service() {
  92. local yaml_file=$1
  93. ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}"
  94. if [ $? -ne 0 ]; then
  95. echo "Failed to deploy ${yaml_file}"
  96. exit 1
  97. else
  98. echo "Successfully deployed ${yaml_file}"
  99. fi
  100. }
  101. deploy_nacos() {
  102. local yaml_file=$1
  103. scp ${baseDir}/k8s/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
  104. deploy_service ${yaml_file}
  105. }
  106. build_and_deploy() {
  107. local dockerfile=$1
  108. local image=$2
  109. local yaml_file=$3
  110. build_image ${dockerfile} ${image}
  111. prepare_yaml ${yaml_file} ${image}
  112. deploy_service ${yaml_file}
  113. }
  114. # 构建和部署 manage 服务
  115. if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then
  116. build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
  117. fi
  118. # 构建和部署 front 服务
  119. if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then
  120. build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
  121. fi
  122. if [ "$service" == "all" ]; then
  123. #部署前端
  124. build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml"
  125. #部署管理平台
  126. build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml"
  127. #部署认证中心
  128. build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml"
  129. #部署网关
  130. build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml"
  131. #部署系统服务
  132. build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml"
  133. #部署配置中心
  134. deploy_nacos "k8s-3nacos.yaml"
  135. fi
  136. # 记录结束时间
  137. end=$(date +%s)
  138. echo "部署成功, 耗时: $((end-start))秒"