push.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. source "$(dirname "$0")/common.sh"
  3. while getopts "p:e:" opt; do
  4. case $opt in
  5. p)
  6. product_code="${OPTARG}"
  7. ;;
  8. e)
  9. env="${OPTARG}"
  10. ;;
  11. *)
  12. echo "用法:$0 -p 产品code [-e 发布环境]"
  13. exit 1
  14. ;;
  15. esac
  16. done
  17. if [ -z "$product_code" ]; then
  18. echo "错误:必须指定产品 code (-p)"
  19. echo "用法:$0 -p 产品code [-e 发布环境]"
  20. exit 1
  21. fi
  22. env=${env:-test}
  23. echo "${MAGENTA}正在合并代码到 ${product_code}-test 分支...${RESET}"
  24. # 切换并拉取 main 分支
  25. execute_command "git checkout main" "切换至 main 分支失败"
  26. execute_command "git pull" "拉取 main 分支最新内容失败"
  27. # 合并 main 到 ${product_code}-test
  28. execute_command "git checkout ${product_code}-test" "切换至 ${product_code}-test 分支失败"
  29. execute_command "git pull" "拉取 ${product_code}-test 分支最新内容失败"
  30. execute_command "git merge main" "合并 main 到 ${product_code}-test 分支失败" "false"
  31. merge_m2t_status=$?
  32. if [ $merge_m2t_status -ne 0 ]; then
  33. # 检查是否有冲突
  34. if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
  35. echo "${YELLOW}检测到合并冲突,请手动解决冲突...${RESET}"
  36. echo "${YELLOW}解决冲突后,按任意键继续...${RESET}"
  37. read -n 1 -s -r -p ""
  38. if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
  39. echo "${RED}错误:仍存在未解决的冲突,请处理后再继续${RESET}"
  40. exit 1
  41. fi
  42. execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
  43. else
  44. exit ${merge_m2t_status}
  45. fi
  46. fi
  47. execute_command "git push" "推送 ${product_code}-test 分支失败"
  48. # 合并 ${product_code}-test 到 ${product_code}-prod
  49. if [ "$env" = "prod" ]; then
  50. echo "${MAGENTA}正在合并代码到 ${product_code}-prod 分支...${RESET}"
  51. execute_command "git checkout ${product_code}-prod" "切换至 ${product_code}-prod 分支失败"
  52. execute_command "git pull" "拉取 ${product_code}-prod 分支最新内容失败"
  53. execute_command "git merge ${product_code}-test" "合并 ${product_code}-test 到 ${product_code}-prod 分支失败" "false"
  54. merge_t2p_status=$?
  55. if [ $merge_t2p_status -ne 0 ]; then
  56. # 检查是否有冲突
  57. if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
  58. echo "${YELLOW}检测到合并冲突,请手动解决冲突...${RESET}"
  59. echo "${YELLOW}解决冲突后,按任意键继续...${RESET}"
  60. read -n 1 -s -r -p ""
  61. if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
  62. echo "${RED}错误:仍存在未解决的冲突,请处理后再继续${RESET}"
  63. exit 1
  64. fi
  65. execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
  66. else
  67. exit ${merge_t2p_status}
  68. fi
  69. fi
  70. execute_command "git push" "推送 ${product_code}-prod 分支失败"
  71. fi
  72. # 切换到 xiaom 分支
  73. execute_command "git checkout xiaom" "切换至 xiaom 分支失败"