build_code.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/bash
  2. # 检查并进入仓库根目录
  3. repo_root=$(git rev-parse --show-toplevel 2>/dev/null || true)
  4. if [[ -z "${repo_root}" ]]; then
  5. echo "未检测到git仓库,请在项目仓库内运行此脚本。"
  6. exit 1
  7. fi
  8. # 获取当前代码分支
  9. repo_hash=$(git rev-parse HEAD 2>/dev/null || true)
  10. repo_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
  11. # 获取excel分支
  12. excel_hash=""
  13. excel_branch=""
  14. if [[ -d "${repo_root}/_excel/" ]]; then
  15. cd "${repo_root}/_excel/"
  16. excel_hash=$(git rev-parse HEAD 2>/dev/null || true)
  17. excel_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
  18. cd "${repo_root}"
  19. fi
  20. # 构建发布参数
  21. os_name=$(expr substr $(uname -s) 1 5 || true)
  22. build_user=$(whoami || true)
  23. build_time=$(date +"%Y-%m-%d %H:%M:%S" || true)
  24. publish_dir="${repo_root}/publish"
  25. publish_dir_etc="${publish_dir}/etc/"
  26. publish_dir_etc_data="${publish_dir_etc}data/"
  27. publish_dir_etc_data_map="${publish_dir_etc}data_map/"
  28. publish_dir_etc_lua="${publish_dir_etc}lua/"
  29. publish_dir_etc_profile="${publish_dir_etc}profile/"
  30. #publish_dir_etc_profile_include="${publish_dir_etc}profile/include/"
  31. main_file="${repo_root}/nodes/main.go"
  32. # 构建相关函数(基于原脚本逻辑,使用绝对路径读取数据/文件)
  33. function clean_publish() {
  34. rm -rf "$publish_dir" 2>/dev/null || true
  35. mkdir -p "$publish_dir" "$publish_dir_etc" "$publish_dir_etc_data" "$publish_dir_etc_data_map" "$publish_dir_etc_lua" "$publish_dir_etc_profile"
  36. }
  37. function go_build() {
  38. local outname="node"
  39. f_repo_hash="'f1-game/internal/version.RepoHash=${repo_hash}'"
  40. f_repo_branch="'f1-game/internal/version.RepoBranch=${repo_branch}'"
  41. f_excel_hash="'f1-game/internal/version.ExcelHash=${excel_hash}'"
  42. f_excel_branch="'f1-game/internal/version.ExcelBranch=${excel_branch}'"
  43. f_build_user="'f1-game/internal/version.BuildUser=${build_user}'"
  44. f_build_time="'f1-game/internal/version.BuildTime=${build_time}'"
  45. echo "[BUILD] ${f_repo_hash}"
  46. echo "[BUILD] ${f_repo_branch}"
  47. echo "[BUILD] ${f_excel_hash}"
  48. echo "[BUILD] ${f_excel_branch}"
  49. echo "[BUILD] ${f_build_user}"
  50. echo "[BUILD] ${f_build_time}"
  51. # go build
  52. go build -ldflags "-X ${f_repo_hash} -X ${f_repo_branch} -X ${f_excel_hash} -X ${f_excel_branch} -X ${f_build_user} -X ${f_build_time}" -o "$publish_dir/${outname}" "./nodes/main.go"
  53. if [ $? -ne 0 ]; then
  54. echo "[BUILD ERROR] go build failed!"
  55. exit 1
  56. fi
  57. echo "[BUILD] go build complete!"
  58. echo ""
  59. }
  60. function copy_etc_files() {
  61. echo "[BUILD] copy etc files."
  62. mkdir -p "$publish_dir_etc_data" "$publish_dir_etc_data_map" "$publish_dir_etc_lua" || true
  63. cp -r "$repo_root/etc/data/"* "$publish_dir_etc_data" 2>/dev/null || true
  64. cp -r "$repo_root/etc/data_embed/"* "$publish_dir_etc_data" 2>/dev/null || true
  65. cp -r "$repo_root/etc/data_map/"* "$publish_dir_etc_data_map" 2>/dev/null || true
  66. cp -r "$repo_root/etc/lua/"* "$publish_dir_etc_lua" 2>/dev/null || true
  67. }
  68. function build_dev() {
  69. clean_publish
  70. echo "[BUILD] dev build..."
  71. export CGO_ENABLED=0
  72. export GOOS=linux
  73. export GOARCH=amd64
  74. go_build
  75. copy_etc_files
  76. echo "[BUILD] copy profile include files."
  77. cp -r "${repo_root}/etc/profile/include" "$publish_dir_etc_profile" 2>/dev/null || true
  78. echo "[BUILD] copy etc files."
  79. cp "$repo_root/etc/profile/dev.json" "$publish_dir_etc_profile" 2>/dev/null || true
  80. cp "$repo_root/etc/cli/cli.sh" "$publish_dir" 2>/dev/null || true
  81. cp "$repo_root/etc/cli/docker.sh" "$publish_dir" 2>/dev/null || true
  82. cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
  83. cp "$repo_root/etc/docker/dev.yaml" "$publish_dir" 2>/dev/null || true
  84. cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
  85. echo "[BUILD] exit."
  86. }
  87. function build_test() {
  88. clean_publish
  89. echo "[BUILD] test build..."
  90. export CGO_ENABLED=0
  91. export GOOS=linux
  92. export GOARCH=amd64
  93. go_build
  94. copy_etc_files
  95. echo "[BUILD] copy profile files."
  96. cp "$repo_root/etc/profile/test.json" "$publish_dir_etc_profile" 2>/dev/null || true
  97. echo "[BUILD] copy run script files."
  98. cp "$repo_root/etc/cli/docker.sh" "$publish_dir" 2>/dev/null || true
  99. cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
  100. cp -r "$repo_root/etc/docker/test.yaml" "$publish_dir/test.yaml" 2>/dev/null || true
  101. cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
  102. echo "[BUILD] exit."
  103. }
  104. function build_dev_test() {
  105. clean_publish
  106. echo "[BUILD] dev & test build..."
  107. export CGO_ENABLED=0
  108. export GOOS=linux
  109. export GOARCH=amd64
  110. go_build
  111. copy_etc_files
  112. echo "[BUILD] copy profile files."
  113. cp -r "$repo_root/etc/profile/include" "$publish_dir_etc_profile" 2>/dev/null || true
  114. cp "$repo_root/etc/profile/dev.json" "$publish_dir_etc_profile" 2>/dev/null || true
  115. cp "$repo_root/etc/profile/test.json" "$publish_dir_etc_profile" 2>/dev/null || true
  116. echo "[BUILD] copy run script files."
  117. cp "$repo_root/etc/cli/docker.sh" "$publish_dir" 2>/dev/null || true
  118. cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
  119. cp -r "$repo_root/etc/docker/dev.yaml" "$publish_dir/dev.yaml" 2>/dev/null || true
  120. cp -r "$repo_root/etc/docker/test.yaml" "$publish_dir/test.yaml" 2>/dev/null || true
  121. cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
  122. echo "[BUILD] exit."
  123. }
  124. # 若需,可实现 build_prod
  125. function build_prod() {
  126. clean_publish
  127. echo "[BUILD] prod profile..."
  128. export CGO_ENABLED=0
  129. export GOOS=linux
  130. export GOARCH=amd64
  131. go_build
  132. copy_etc_files
  133. echo "[BUILD] copy profile files."
  134. cp "$repo_root/etc/profile/prod.json" "$publish_dir_etc_profile" 2>/dev/null || true
  135. cp -r "$repo_root/etc/profile/include" "$publish_dir_etc_profile" 2>/dev/null || true
  136. cp "$repo_root/etc/cli/cli.sh" "$publish_dir" 2>/dev/null || true
  137. cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
  138. cp "$repo_root/etc/docker/prod.yaml" "$publish_dir" 2>/dev/null || true
  139. cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
  140. echo "[BUILD] exit."
  141. }
  142. echo "------------------------------------------------------"
  143. echo "构建参数:"
  144. echo "./build_code.sh dev 构建开发环境"
  145. echo "./build_code.sh test 构建测试环境"
  146. echo "./build_code.sh devtest 构建开发&测试环境"
  147. echo "./build_code.sh prod 构建正式环境"
  148. echo "------------------------------------------------------"
  149. repo_branch_arg=$(echo "${1:-}" | awk -F/ '{print $NF}') #外部标记的源码分支名
  150. excel_branch_arg=$(echo "${2:-}" | awk -F/ '{print $NF}') #外部标记的配表分支名
  151. if [[ -n "$repo_branch_arg" ]]; then
  152. repo_branch="$repo_branch_arg" #外部有传入则覆盖
  153. fi
  154. if [[ -n "$excel_branch_arg" ]]; then
  155. excel_branch="$excel_branch_arg" #外部有传入则覆盖
  156. fi
  157. echo "------------------------------------------------------"
  158. echo "环境变量:"
  159. echo "OS Name : ${os_name}"
  160. echo "Code Path : ${repo_root}"
  161. echo "Repo Hash : ${repo_hash}"
  162. echo "Repo Branch : ${repo_branch}"
  163. echo "Excel Hash : ${excel_hash}"
  164. echo "Excel Branch : ${excel_branch}"
  165. echo "Build User : ${build_user}"
  166. echo "Build Time : ${build_time}"
  167. echo "------------------------------------------------------"
  168. case "$repo_branch_arg" in
  169. "dev")
  170. build_dev
  171. ;;
  172. "test")
  173. build_test
  174. ;;
  175. "devtest")
  176. build_dev_test
  177. ;;
  178. "prod")
  179. build_prod
  180. ;;
  181. *)
  182. echo "传入的参数不正确,退出!"
  183. exit 1
  184. ;;
  185. esac
  186. echo "exit"
  187. exit 0