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.

build_and_deploy.sh 2.1 kB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. #记录开始时间
  3. startTime=$(date +%s)
  4. # 登录到目标环境
  5. baseDir="/home/somuns/ci4s"
  6. cd ${baseDir}
  7. #build
  8. # 默认参数
  9. branch="master"
  10. service="manage-front"
  11. env="dev"
  12. #
  13. show_help() {
  14. echo "Usage: $0 [-b branch] [-s service] [-e environment]"
  15. echo
  16. echo "Options:"
  17. echo " -b Branch to deploy, default: master"
  18. echo " -s Service to deploy (manage-front, manage, front, all, system default: manage-front)"
  19. echo " -e Environment (e.g., dev, test, default: dev)"
  20. echo " -h Show this help message"
  21. }
  22. # 解析命令行选项
  23. while getopts "b:s:e:h" opt; do
  24. case $opt in
  25. b) branch=$OPTARG ;;
  26. s) service=$OPTARG ;;
  27. e) env=$OPTARG ;;
  28. h) show_help; exit 0 ;;
  29. \?) echo "Invalid option -$OPTARG" >&2; show_help; exit 1 ;;
  30. esac
  31. done
  32. # 拉取指定分支的最新代码
  33. echo "Checking out and pulling branch $branch..."
  34. git stash
  35. git checkout $branch
  36. if [ $? -ne 0 ]; then
  37. echo "切换到分支 $branch 失败,请检查分支名称是否正确!"
  38. exit 1
  39. fi
  40. git stash
  41. git pull origin $branch
  42. if [ $? -ne 0 ]; then
  43. echo "拉取代码失败,请检查网络或联系管理员!"
  44. exit 1
  45. fi
  46. chmod +777 ${baseDir}/k8s/*.sh
  47. valid_services=("manage-front" "manage" "front" "all" "auth" "gateway" "system")
  48. if [[ ! " ${valid_services[@]} " =~ " $service " ]]; then
  49. echo "Invalid service name: $service" >&2
  50. echo "Valid services are: ${valid_services[*]}"
  51. exit 1
  52. fi
  53. valid_envs=("dev" "test" "test1")
  54. if [[ ! " ${valid_envs[@]} " =~ " $env " ]]; then
  55. echo "Invalid environment: $env" >&2
  56. echo "Valid environments are: ${valid_envs[*]}"
  57. exit 1
  58. fi
  59. echo "start build"
  60. sh ${baseDir}/k8s/build.sh -b ${branch} -s ${service}
  61. if [ $? -ne 0 ]; then
  62. echo "Build failed"
  63. exit 1
  64. fi
  65. echo "build success"
  66. # 部署
  67. echo "start deploy"
  68. sh ${baseDir}/k8s/deploy.sh -s ${service} -e ${env}
  69. if [ $? -ne 0 ]; then
  70. echo "Deploy failed"
  71. exit 1
  72. fi
  73. echo "deploy success"
  74. # 记录结束时间
  75. endTime=$(date +%s)
  76. # 计算运行时间
  77. duration=$(( $endTime - $startTime ))
  78. echo "编译发布总耗时: $duration 秒"