Files
sunhpc-go/build-sunhpc.sh

32 lines
1.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e # 出错立即退出,便于定位问题
# ========== 核心配置(根据你的项目修改) ==========
# 1. 必须和go.mod中的module名称一致关键
MODULE_NAME="sunhpc"
# 2. 编译的入口文件路径你的main.go在cmd/sunhpc下
ENTRY_FILE="./cmd/sunhpc/main.go"
# 3. 输出的可执行文件名称
APP_NAME="sunhpc"
# 4. 自定义版本号
APP_VERSION="v1.0.0"
# ========== 获取Git和编译信息兼容无Git环境 ==========
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
BUILD_TIME=$(date +"%Y-%m-%d-%H:%M:%S")
# ========== 编译核心修复ldflags格式 ==========
# 关键:去掉反斜杠,用单层双引号包裹,内部换行分隔参数
go build -ldflags "
-X ${MODULE_NAME}/pkg/info.Version=${APP_VERSION}
-X ${MODULE_NAME}/pkg/info.BuildTime=${BUILD_TIME}
-X ${MODULE_NAME}/pkg/info.GitCommit=${GIT_COMMIT}
-X ${MODULE_NAME}/pkg/info.GitBranch=${GIT_BRANCH}
" -o ${APP_NAME} ${ENTRY_FILE}
# ========== 验证提示 ==========
echo "- 执行文件:./${APP_NAME}"
echo "- 版本信息:${APP_VERSION} (Git: ${GIT_COMMIT} @ ${GIT_BRANCH})"
echo "- 编译时间:${BUILD_TIME}"