forked from hslr-s/sun-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
211 lines (180 loc) · 5.31 KB
/
build.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
REPO=$(
cd $(dirname $0)
pwd
)
COMMIT_SHA=$(git rev-parse --short HEAD)
# VERSION=$(git describe --tags)
VERSION="v${cut -d '|' -f 2 ./service/assets/version}"
LATEST_TAG=$(git describe --tags --abbrev=0)
FRONTEND="false"
BINARY="false"
RELEASE="false"
debugInfo() {
echo "Repo: $REPO"
echo "Build frontend: $FRONTEND"
echo "Build binary: $BINARY"
echo "Release: $RELEASE"
echo "Version: $VERSION"
echo "Commit: $COMMIT_SHA"
echo "LATEST_TAG: $LATEST_TAG"
}
buildFrontend() {
cd $REPO
pwd
# npm install pnpm -g
pnpm install
pnpm run build
}
buildBackEndAssets() {
cd $REPO/service
# export PATH=$PATH:/root/go/bin
go install -a -v github.com/go-bindata/go-bindata/...@latest
go install -a -v github.com/elazarl/go-bindata-assetfs/...@latest
go-bindata-assetfs -o=assets/bindata.go -pkg=assets assets/...
}
# buildBinary() {
# cd $REPO/service
# # mv "${REPO}/dist" "${REPO}/web"
# go build -o "sun-panel" --ldflags="-X sun-panel/global.RUNCODE=release" main.go
# }
_build() {
cd $REPO/service
pwd
local osarch=$1
IFS=/ read -r -a arr <<<"$osarch"
os="${arr[0]}"
arch="${arr[1]}"
gcc="${arr[2]}"
# Go build to build the binary.
export GOOS=$os
export GOARCH=$arch
export CC=$gcc
export CGO_ENABLED=1
pathRelease=$REPO/release
if [ -n "$VERSION" ]; then
outPath="sun-panel_${VERSION}_${os}_${arch}"
elif [ -n "$LATEST_TAG" ]; then
outPath="sun-panel_${LATEST_TAG}_${os}_${arch}"
else
outPath="sun-panel_${COMMIT_SHA}_${os}_${arch}"
fi
outname="${pathRelease}/${outPath}/sun-panel"
go build -o "${outname}" --ldflags="-X sun-panel/global.RUNCODE=release" main.go
cd $pathRelease
# copy front file
cp -r "${REPO}/dist" "${pathRelease}/${outPath}/web"
echo "Release ${outPath}"
if [ "$os" = "windows" ]; then
mv $outname $outPath/sun-panel.exe
zip -r "${pathRelease}/${outPath}.zip" $outPath
else
mv $outname $outPath/sun-panel
tar -zcvf "${pathRelease}/${outPath}.tar.gz" $outPath
fi
rm -rf "${pathRelease}/${outPath}"
}
# 定义函数BuildReleaseLinuxMusl,用于构建正式版Linux-musl平台的二进制文件(参考Alist构建方案)
buildReleaseLinuxMusl() {
cd $REPO/service
ldflags="-X sun-panel/global.RUNCODE=release"
pathRelease=$REPO/release
# 清理.git目录,创建build目录,并下载交叉编译工具
# rm -rf .git/
# mkdir -p "build"
muslflags="--extldflags '-static -fpic' $ldflags"
BASE="https://musl.nn.ci/"
# FILES=(x86_64-linux-musl-cross aarch64-linux-musl-cross mips-linux-musl-cross mips64-linux-musl-cross mips64el-linux-musl-cross mipsel-linux-musl-cross powerpc64le-linux-musl-cross s390x-linux-musl-cross)
FILES=(x86_64-linux-musl-cross)
for i in "${FILES[@]}"; do
url="${BASE}${i}.tgz"
curl -L -o "${i}.tgz" "${url}"
tar xf "${i}.tgz" --strip-components 1 -C /usr/local
rm -f "${i}.tgz"
done
# OS_ARCHES=(linux-musl-amd64 linux-musl-arm64 linux-musl-mips linux-musl-mips64 linux-musl-mips64le linux-musl-mipsle linux-musl-ppc64le linux-musl-s390x)
# CGO_ARGS=(x86_64-linux-musl-gcc aarch64-linux-musl-gcc mips-linux-musl-gcc mips64-linux-musl-gcc mips64el-linux-musl-gcc mipsel-linux-musl-gcc powerpc64le-linux-musl-gcc s390x-linux-musl-gcc)
# 暂时仅编译amd64
OS_ARCHES=(linux-musl-amd64)
CGO_ARGS=(x86_64-linux-musl-gcc)
for i in "${!OS_ARCHES[@]}"; do
os_arch=${OS_ARCHES[$i]}
cgo_cc=${CGO_ARGS[$i]}
echo building for ${os_arch}
export GOOS=${os_arch%%-*}
export GOARCH=${os_arch##*-}
export CC=${cgo_cc}
export CGO_ENABLED=1
if [ -n "$VERSION" ]; then
outPath="sun-panel_${VERSION}_${GOOS}_musl_${GOARCH}"
elif [ -n "$LATEST_TAG" ]; then
outPath="sun-panel_${LATEST_TAG}_${GOOS}_musl_${GOARCH}"
else
outPath="sun-panel_${COMMIT_SHA}_${GOOS}_musl_${GOARCH}"
fi
outname="${pathRelease}/${outPath}/sun-panel"
go build -o "${outname}" -ldflags="$muslflags" main.go
# go build -o "${outname}" -ldflags="$muslflags" -tags=jsoniter main.go
done
cd $pathRelease
# copy front file
cp -r "${REPO}/dist" "${pathRelease}/${outPath}/web"
echo "Release ${outPath}"
mv $outname $outPath/sun-panel
tar -zcvf "${pathRelease}/${outPath}.tar.gz" $outPath
rm -rf "${pathRelease}/${outPath}"
}
release() {
cd $REPO/service
## List of architectures and OS to test coss compilation.
SUPPORTED_OSARCH="linux/amd64/gcc linux/arm/arm-linux-gnueabihf-gcc windows/amd64/x86_64-w64-mingw32-gcc linux/arm64/aarch64-linux-gnu-gcc"
echo "Release builds for OS/Arch/CC: ${SUPPORTED_OSARCH}"
for each_osarch in ${SUPPORTED_OSARCH}; do
_build "${each_osarch}"
done
# 临时方案解决centos无法运行的问题
buildReleaseLinuxMusl
}
usage() {
# echo "Usage: $0 [-f] [-c] [-b] [-r]" 1>&2
echo "Usage: $0 [-f] [-b] [-r]" 1>&2
exit 1
}
while getopts "bfcrd" o; do
case "${o}" in
b)
FRONTEND="true"
BINARY="true"
;;
f)
FRONTEND="true"
;;
c)
BINARY="true"
;;
r)
FRONTEND="true"
RELEASE="true"
;;
d)
DEBUG="true"
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
if [ "$DEBUG" = "true" ]; then
debugInfo
fi
if [ "$FRONTEND" = "true" ]; then
buildFrontend
fi
# if [ "$BINARY" = "true" ]; then
# buildBinary
# fi
if [ "$RELEASE" = "true" ]; then
buildBackEndAssets
release
fi