-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·94 lines (77 loc) · 2.3 KB
/
build.sh
File metadata and controls
executable file
·94 lines (77 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
WORKDIR=$(pwd)
SERVER_NAME=confkeeper
VERSION=${1:-dev}
go_args=""
if [ "${VERSION}"x != "dev"x ]; then
go_args="-trimpath"
fi
# 创建静态文件目录及默认页面
if [ ! -d static/ ]; then
mkdir -p static/
touch static/index.html
fi
# 检查依赖工具
if ! command -v xz &> /dev/null; then
apt update && apt install -y xz-utils
fi
if ! command -v md5sum &>/dev/null; then
apt update && apt install -y coreutils
fi
# 检查是否安装 upx
if ! command -v upx &>/dev/null; then
echo "安装 upx..."
apt update && apt install -y upx
fi
# 下载依赖
go mod download
# 定义多平台编译目标
platforms=(
"linux/amd64"
"linux/arm64"
"linux/386"
"linux/arm"
"linux/riscv64"
"darwin/amd64"
"darwin/arm64"
"windows/amd64"
"windows/arm64"
)
# 主构建流程
mkdir -p dist/release
for platform in "${platforms[@]}"; do
# 分割平台信息
GOOS=${platform%/*}
GOARCH=${platform#*/}
# 生成文件名
if [ -z ${VERSION} ]; then
BINARY="${SERVER_NAME}_${GOOS}_${GOARCH}"
else
BINARY="${SERVER_NAME}_${VERSION}_${GOOS}_${GOARCH}"
fi
[ "$GOOS" = "windows" ] && BINARY="${BINARY}.exe"
# 目标路径
OUTPUT_FILE="dist/release/${BINARY}"
UPX_OUTPUT_FILE="dist/release/${SERVER_NAME}_${VERSION}_upx_${GOOS}_${GOARCH}"
[ "$GOOS" = "windows" ] && UPX_OUTPUT_FILE="${UPX_OUTPUT_FILE}.exe"
# 编译
echo "编译中: ${GOOS}-${GOARCH}..."
env GOOS="$GOOS" GOARCH="$GOARCH" \
go build ${go_args} -ldflags '-w -s' -o "$OUTPUT_FILE"
if [ "${VERSION}"x != "dev"x ]; then
# 使用 upx 压缩
echo "使用 upx 压缩: ${OUTPUT_FILE}..."
upx -o "$UPX_OUTPUT_FILE" "$OUTPUT_FILE"
# 压缩,仅包含可执行文件本身
tar -cJf "${UPX_OUTPUT_FILE}.tar.xz" -C "$(dirname "$UPX_OUTPUT_FILE")" "$(basename "$UPX_OUTPUT_FILE")"
echo "生成文件: ${UPX_OUTPUT_FILE}.tar.xz"
tar -cJf "${OUTPUT_FILE}.tar.xz" -C "$(dirname "$OUTPUT_FILE")" "$(basename "$OUTPUT_FILE")"
echo "生成文件: ${OUTPUT_FILE}.tar.xz"
fi
done
# 生成所有 dist/release 下文件的 md5
echo "生成 dist/release 下所有文件的 .md5 文件..."
for file in dist/release/*; do
[ -f "$file" ] || continue
md5sum "$file" > "${file}.md5"
done