| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #!/bin/bash
- # 检查并进入仓库根目录
- repo_root=$(git rev-parse --show-toplevel 2>/dev/null || true)
- if [[ -z "${repo_root}" ]]; then
- echo "未检测到git仓库,请在项目仓库内运行此脚本。"
- exit 1
- fi
- # 获取当前代码分支
- repo_hash=$(git rev-parse HEAD 2>/dev/null || true)
- repo_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
- # 获取excel分支
- excel_hash=""
- excel_branch=""
- if [[ -d "${repo_root}/_excel/" ]]; then
- cd "${repo_root}/_excel/"
- excel_hash=$(git rev-parse HEAD 2>/dev/null || true)
- excel_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
- cd "${repo_root}"
- fi
- # 构建发布参数
- os_name=$(expr substr $(uname -s) 1 5 || true)
- build_user=$(whoami || true)
- build_time=$(date +"%Y-%m-%d %H:%M:%S" || true)
- publish_dir="${repo_root}/publish"
- publish_dir_etc="${publish_dir}/etc/"
- publish_dir_etc_data="${publish_dir_etc}data/"
- publish_dir_etc_data_map="${publish_dir_etc}data_map/"
- publish_dir_etc_lua="${publish_dir_etc}lua/"
- publish_dir_etc_profile="${publish_dir_etc}profile/"
- #publish_dir_etc_profile_include="${publish_dir_etc}profile/include/"
- main_file="${repo_root}/nodes/main.go"
- # 构建相关函数(基于原脚本逻辑,使用绝对路径读取数据/文件)
- function clean_publish() {
- rm -rf "$publish_dir" 2>/dev/null || true
- mkdir -p "$publish_dir" "$publish_dir_etc" "$publish_dir_etc_data" "$publish_dir_etc_data_map" "$publish_dir_etc_lua" "$publish_dir_etc_profile"
- }
- function go_build() {
- local outname="node"
- f_repo_hash="'f1-game/internal/version.RepoHash=${repo_hash}'"
- f_repo_branch="'f1-game/internal/version.RepoBranch=${repo_branch}'"
- f_excel_hash="'f1-game/internal/version.ExcelHash=${excel_hash}'"
- f_excel_branch="'f1-game/internal/version.ExcelBranch=${excel_branch}'"
- f_build_user="'f1-game/internal/version.BuildUser=${build_user}'"
- f_build_time="'f1-game/internal/version.BuildTime=${build_time}'"
- echo "[BUILD] ${f_repo_hash}"
- echo "[BUILD] ${f_repo_branch}"
- echo "[BUILD] ${f_excel_hash}"
- echo "[BUILD] ${f_excel_branch}"
- echo "[BUILD] ${f_build_user}"
- echo "[BUILD] ${f_build_time}"
- # go build
- 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"
- if [ $? -ne 0 ]; then
- echo "[BUILD ERROR] go build failed!"
- exit 1
- fi
- echo "[BUILD] go build complete!"
- echo ""
- }
- function copy_etc_files() {
- echo "[BUILD] copy etc files."
- mkdir -p "$publish_dir_etc_data" "$publish_dir_etc_data_map" "$publish_dir_etc_lua" || true
- cp -r "$repo_root/etc/data/"* "$publish_dir_etc_data" 2>/dev/null || true
- cp -r "$repo_root/etc/data_embed/"* "$publish_dir_etc_data" 2>/dev/null || true
- cp -r "$repo_root/etc/data_map/"* "$publish_dir_etc_data_map" 2>/dev/null || true
- cp -r "$repo_root/etc/lua/"* "$publish_dir_etc_lua" 2>/dev/null || true
- }
- function build_dev() {
- clean_publish
- echo "[BUILD] dev build..."
- export CGO_ENABLED=0
- export GOOS=linux
- export GOARCH=amd64
- go_build
- copy_etc_files
- echo "[BUILD] copy profile include files."
- cp -r "${repo_root}/etc/profile/include" "$publish_dir_etc_profile" 2>/dev/null || true
- echo "[BUILD] copy etc files."
- cp "$repo_root/etc/profile/dev.json" "$publish_dir_etc_profile" 2>/dev/null || true
- cp "$repo_root/etc/cli/cli.sh" "$publish_dir" 2>/dev/null || true
- cp "$repo_root/etc/cli/docker.sh" "$publish_dir" 2>/dev/null || true
- cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
- cp "$repo_root/etc/docker/dev.yaml" "$publish_dir" 2>/dev/null || true
- cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
- echo "[BUILD] exit."
- }
- function build_test() {
- clean_publish
- echo "[BUILD] test build..."
- export CGO_ENABLED=0
- export GOOS=linux
- export GOARCH=amd64
- go_build
- copy_etc_files
- echo "[BUILD] copy profile files."
- cp "$repo_root/etc/profile/test.json" "$publish_dir_etc_profile" 2>/dev/null || true
- echo "[BUILD] copy run script files."
- cp "$repo_root/etc/cli/docker.sh" "$publish_dir" 2>/dev/null || true
- cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
- cp -r "$repo_root/etc/docker/test.yaml" "$publish_dir/test.yaml" 2>/dev/null || true
- cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
- echo "[BUILD] exit."
- }
- function build_dev_test() {
- clean_publish
- echo "[BUILD] dev & test build..."
- export CGO_ENABLED=0
- export GOOS=linux
- export GOARCH=amd64
- go_build
- copy_etc_files
- echo "[BUILD] copy profile files."
- cp -r "$repo_root/etc/profile/include" "$publish_dir_etc_profile" 2>/dev/null || true
- cp "$repo_root/etc/profile/dev.json" "$publish_dir_etc_profile" 2>/dev/null || true
- cp "$repo_root/etc/profile/test.json" "$publish_dir_etc_profile" 2>/dev/null || true
- echo "[BUILD] copy run script files."
- cp "$repo_root/etc/cli/docker.sh" "$publish_dir" 2>/dev/null || true
- cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
- cp -r "$repo_root/etc/docker/dev.yaml" "$publish_dir/dev.yaml" 2>/dev/null || true
- cp -r "$repo_root/etc/docker/test.yaml" "$publish_dir/test.yaml" 2>/dev/null || true
- cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
- echo "[BUILD] exit."
- }
- # 若需,可实现 build_prod
- function build_prod() {
- clean_publish
- echo "[BUILD] prod profile..."
- export CGO_ENABLED=0
- export GOOS=linux
- export GOARCH=amd64
- go_build
- copy_etc_files
- echo "[BUILD] copy profile files."
- cp "$repo_root/etc/profile/prod.json" "$publish_dir_etc_profile" 2>/dev/null || true
- cp -r "$repo_root/etc/profile/include" "$publish_dir_etc_profile" 2>/dev/null || true
- cp "$repo_root/etc/cli/cli.sh" "$publish_dir" 2>/dev/null || true
- cp -r "$repo_root/tool/bin/"* "$publish_dir" 2>/dev/null || true
- cp "$repo_root/etc/docker/prod.yaml" "$publish_dir" 2>/dev/null || true
- cp "$repo_root/etc/docker/db.yaml" "$publish_dir" 2>/dev/null || true
- echo "[BUILD] exit."
- }
- echo "------------------------------------------------------"
- echo "构建参数:"
- echo "./build_code.sh dev 构建开发环境"
- echo "./build_code.sh test 构建测试环境"
- echo "./build_code.sh devtest 构建开发&测试环境"
- echo "./build_code.sh prod 构建正式环境"
- echo "------------------------------------------------------"
- repo_branch_arg=$(echo "${1:-}" | awk -F/ '{print $NF}') #外部标记的源码分支名
- excel_branch_arg=$(echo "${2:-}" | awk -F/ '{print $NF}') #外部标记的配表分支名
- if [[ -n "$repo_branch_arg" ]]; then
- repo_branch="$repo_branch_arg" #外部有传入则覆盖
- fi
- if [[ -n "$excel_branch_arg" ]]; then
- excel_branch="$excel_branch_arg" #外部有传入则覆盖
- fi
- echo "------------------------------------------------------"
- echo "环境变量:"
- echo "OS Name : ${os_name}"
- echo "Code Path : ${repo_root}"
- echo "Repo Hash : ${repo_hash}"
- echo "Repo Branch : ${repo_branch}"
- echo "Excel Hash : ${excel_hash}"
- echo "Excel Branch : ${excel_branch}"
- echo "Build User : ${build_user}"
- echo "Build Time : ${build_time}"
- echo "------------------------------------------------------"
- case "$repo_branch_arg" in
- "dev")
- build_dev
- ;;
- "test")
- build_test
- ;;
- "devtest")
- build_dev_test
- ;;
- "prod")
- build_prod
- ;;
- *)
- echo "传入的参数不正确,退出!"
- exit 1
- ;;
- esac
- echo "exit"
- exit 0
|