ci(workflow): 更新 GitHub Actions 发布配置和构建脚本
更新 action-gh-release 从 v1 到 v2 优化构建脚本,增加错误处理和命令检查 使用更安全的脚本编写方式,避免全局变量污染 改进跨平台构建的兼容性和可维护性
This commit is contained in:
1 parent
97276224ee
commit
f341159f1a
2 files changed
+55
-38
No files matched your search
@@ -71,7 +71,7 @@ jobs:
|
|||||||
|
|
||||||
- name: 创建自动发布
|
- name: 创建自动发布
|
||||||
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
|
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
|
||||||
uses: softprops/action-gh-release@v1
|
uses: softprops/action-gh-release@v2
|
||||||
with:
|
with:
|
||||||
name: Auto-Build-${{ steps.version.outputs.date }}-${{ steps.version.outputs.sha_short }}
|
name: Auto-Build-${{ steps.version.outputs.date }}-${{ steps.version.outputs.sha_short }}
|
||||||
tag_name: auto-${{ steps.version.outputs.date }}-${{ steps.version.outputs.sha_short }}
|
tag_name: auto-${{ steps.version.outputs.date }}-${{ steps.version.outputs.sha_short }}
|
||||||
|
|||||||
+54
-37
@@ -1,4 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -Eeuo pipefail
|
||||||
|
IFS=$'\n\t'
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
# 解析命令行参数
|
# 解析命令行参数
|
||||||
FULL_BUILD=false
|
FULL_BUILD=false
|
||||||
@@ -25,6 +28,12 @@ check_upx() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
require_cmd() {
|
||||||
|
if ! command -v "$1" >/dev/null 2>&1; then
|
||||||
|
echo "$1 未找到,请安装后再试"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# 清理已有构建产物
|
# 清理已有构建产物
|
||||||
echo "清理已有构建产物..."
|
echo "清理已有构建产物..."
|
||||||
@@ -94,14 +103,18 @@ if [ "$FULL_BUILD" = true ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "构建前端..."
|
echo "构建前端..."
|
||||||
|
require_cmd pnpm
|
||||||
|
require_cmd go
|
||||||
pnpm run build
|
pnpm run build
|
||||||
|
|
||||||
echo "复制前端文件到服务端..."
|
echo "复制前端文件到服务端..."
|
||||||
rm -rf ./src-server/web/*
|
rm -rf ./src-server/web/*
|
||||||
mkdir -p ./src-server/web/
|
mkdir -p ./src-server/web/
|
||||||
|
if compgen -G "./dist/*" > /dev/null; then
|
||||||
cp -r ./dist/* ./src-server/web/
|
cp -r ./dist/* ./src-server/web/
|
||||||
|
fi
|
||||||
|
|
||||||
cd ./src-server
|
cd "$SCRIPT_DIR/src-server"
|
||||||
|
|
||||||
# ===== 构建阶段 =====
|
# ===== 构建阶段 =====
|
||||||
echo "===== 开始构建阶段 ====="
|
echo "===== 开始构建阶段 ====="
|
||||||
@@ -111,20 +124,16 @@ echo "构建基础版本..."
|
|||||||
|
|
||||||
# Windows AMD64 无GUI
|
# Windows AMD64 无GUI
|
||||||
echo "构建 Windows AMD64 无GUI 版本"
|
echo "构建 Windows AMD64 无GUI 版本"
|
||||||
go env -w GOOS=windows GOARCH=amd64
|
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o "$SCRIPT_DIR/miao-directory-amd64-win.exe"
|
||||||
go build -ldflags="-s -w" -o ../miao-directory-amd64-win.exe
|
|
||||||
|
|
||||||
# Linux AMD64 无GUI
|
# Linux AMD64 无GUI
|
||||||
echo "构建 Linux AMD64 无GUI 版本"
|
echo "构建 Linux AMD64 无GUI 版本"
|
||||||
go env -w GOOS=linux GOARCH=amd64
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o "$SCRIPT_DIR/miao-directory-amd64-linux"
|
||||||
export CGO_ENABLED=0
|
chmod +x "$SCRIPT_DIR/miao-directory-amd64-linux"
|
||||||
go build -ldflags="-s -w" -o ../miao-directory-amd64-linux
|
|
||||||
export CGO_ENABLED=1
|
|
||||||
|
|
||||||
# Windows AMD64 GUI
|
# Windows AMD64 GUI
|
||||||
echo "构建 Windows AMD64 GUI 版本"
|
echo "构建 Windows AMD64 GUI 版本"
|
||||||
go env -w GOOS=windows GOARCH=amd64
|
GOOS=windows GOARCH=amd64 go build -tags webview -ldflags="-s -w -H=windowsgui" -o "$SCRIPT_DIR/miao-directory-amd64-win.gui.exe"
|
||||||
go build -tags webview -ldflags="-s -w -H=windowsgui" -o ../miao-directory-amd64-win.gui.exe
|
|
||||||
|
|
||||||
# 完整构建 - 额外的系统和架构
|
# 完整构建 - 额外的系统和架构
|
||||||
if [ "$FULL_BUILD" = true ]; then
|
if [ "$FULL_BUILD" = true ]; then
|
||||||
@@ -136,25 +145,31 @@ if [ "$FULL_BUILD" = true ]; then
|
|||||||
mv resource.syso resource.syso.bak
|
mv resource.syso resource.syso.bak
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 无需GUI和syso的构建批次
|
: # CGO 在每次构建调用中禁用,无需全局 export
|
||||||
export CGO_ENABLED=0
|
|
||||||
|
|
||||||
# 定义操作系统和架构映射
|
# 定义操作系统和架构映射
|
||||||
declare -A OS_ARCHS
|
# 在工具函数处增加大小写格式化,避免使用 Bash 4 的 ${var^}/${var^^}
|
||||||
OS_ARCHS["linux"]="arm64 386 mips mips64 mips64le ppc64 ppc64le riscv64 s390x"
|
uc_first() {
|
||||||
OS_ARCHS["darwin"]="amd64 arm64"
|
local s="$1"
|
||||||
OS_ARCHS["windows"]="arm64 386"
|
local first="${s:0:1}"
|
||||||
OS_ARCHS["freebsd"]="amd64 arm64 386"
|
local rest="${s:1}"
|
||||||
OS_ARCHS["openbsd"]="amd64 arm64 386"
|
printf "%s%s" "$(printf "%s" "$first" | tr '[:lower:]' '[:upper:]')" "$rest"
|
||||||
# OS_ARCHS["netbsd"]="amd64 arm64 386"
|
}
|
||||||
|
|
||||||
# 遍历操作系统和架构进行构建
|
to_upper() {
|
||||||
for os in "${!OS_ARCHS[@]}"; do
|
printf "%s" "$1" | tr '[:lower:]' '[:upper:]'
|
||||||
echo "构建 ${os^} 系列版本..."
|
}
|
||||||
go env -w GOOS=$os
|
OS_LIST="linux darwin windows freebsd openbsd"
|
||||||
|
for os in $OS_LIST; do
|
||||||
# 获取当前操作系统支持的架构列表
|
echo "构建 $(uc_first "$os") 系列版本..."
|
||||||
archs=${OS_ARCHS[$os]}
|
case "$os" in
|
||||||
|
linux) archs="arm64 386 mips mips64 mips64le ppc64 ppc64le riscv64 s390x" ;;
|
||||||
|
darwin) archs="amd64 arm64" ;;
|
||||||
|
windows) archs="arm64 386" ;;
|
||||||
|
freebsd) archs="amd64 arm64 386" ;;
|
||||||
|
openbsd) archs="amd64 arm64 386" ;;
|
||||||
|
*) archs="" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
for arch in $archs; do
|
for arch in $archs; do
|
||||||
# 设置输出文件名后缀
|
# 设置输出文件名后缀
|
||||||
@@ -163,17 +178,19 @@ if [ "$FULL_BUILD" = true ]; then
|
|||||||
suffix=".exe"
|
suffix=".exe"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "构建 ${os^} ${arch^^} 版本"
|
echo "构建 $(uc_first "$os") $(to_upper "$arch") 版本"
|
||||||
go env -w GOARCH=$arch
|
|
||||||
|
|
||||||
# 根据条件设置不同的输出文件名格式
|
# 根据条件设置不同的输出文件名格式
|
||||||
if [[ "$os" == "darwin" || ("$os" == "linux" && "$arch" == "arm64") || ("$os" == "windows" && "$arch" == "arm64") ]]; then
|
if [[ "$os" == "darwin" || ("$os" == "linux" && "$arch" == "arm64") || ("$os" == "windows" && "$arch" == "arm64") ]]; then
|
||||||
output_file="../miao-directory-${arch}-${os}${suffix}"
|
output_file="$SCRIPT_DIR/miao-directory-${arch}-${os}${suffix}"
|
||||||
else
|
else
|
||||||
output_file="../miao-directory-x-${arch}-${os}${suffix}"
|
output_file="$SCRIPT_DIR/miao-directory-x-${arch}-${os}${suffix}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
go build -ldflags="-s -w" -o "$output_file"
|
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build -ldflags="-s -w" -o "$output_file"
|
||||||
|
if [ "$os" != "windows" ]; then
|
||||||
|
chmod +x "$output_file"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -183,7 +200,7 @@ if [ "$FULL_BUILD" = true ]; then
|
|||||||
mv resource.syso.bak resource.syso
|
mv resource.syso.bak resource.syso
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export CGO_ENABLED=1
|
: # 无需恢复 CGO,全程使用内联变量
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ===== 压缩阶段 =====
|
# ===== 压缩阶段 =====
|
||||||
@@ -207,15 +224,15 @@ if check_upx; then
|
|||||||
|
|
||||||
# 压缩Windows无GUI版本
|
# 压缩Windows无GUI版本
|
||||||
echo "正在压缩Windows AMD64无GUI版本"
|
echo "正在压缩Windows AMD64无GUI版本"
|
||||||
upx $COMPRESSION_LEVEL -o ../miao-directory-amd64-win.upx.exe ../miao-directory-amd64-win.exe
|
upx $COMPRESSION_LEVEL -o "$SCRIPT_DIR/miao-directory-amd64-win.upx.exe" "$SCRIPT_DIR/miao-directory-amd64-win.exe"
|
||||||
|
|
||||||
# 压缩Linux AMD64版本
|
# 压缩Linux AMD64版本
|
||||||
echo "正在压缩Linux AMD64版本"
|
echo "正在压缩Linux AMD64版本"
|
||||||
upx $COMPRESSION_LEVEL -o ../miao-directory-amd64-linux.upx ../miao-directory-amd64-linux
|
upx $COMPRESSION_LEVEL -o "$SCRIPT_DIR/miao-directory-amd64-linux.upx" "$SCRIPT_DIR/miao-directory-amd64-linux"
|
||||||
|
|
||||||
# 压缩Windows GUI版本
|
# 压缩Windows GUI版本
|
||||||
echo "正在压缩Windows AMD64 GUI版本"
|
echo "正在压缩Windows AMD64 GUI版本"
|
||||||
upx $COMPRESSION_LEVEL -o ../miao-directory-amd64-win.gui.upx.exe ../miao-directory-amd64-win.gui.exe
|
upx $COMPRESSION_LEVEL -o "$SCRIPT_DIR/miao-directory-amd64-win.gui.upx.exe" "$SCRIPT_DIR/miao-directory-amd64-win.gui.exe"
|
||||||
|
|
||||||
# 压缩额外架构版本
|
# 压缩额外架构版本
|
||||||
if [ "$FULL_BUILD" = true ]; then
|
if [ "$FULL_BUILD" = true ]; then
|
||||||
@@ -223,15 +240,15 @@ if check_upx; then
|
|||||||
|
|
||||||
# 压缩Linux ARM64版本
|
# 压缩Linux ARM64版本
|
||||||
echo "正在压缩Linux ARM64版本"
|
echo "正在压缩Linux ARM64版本"
|
||||||
upx $COMPRESSION_LEVEL -o ../miao-directory-arm64-linux.upx ../miao-directory-arm64-linux
|
upx $COMPRESSION_LEVEL -o "$SCRIPT_DIR/miao-directory-arm64-linux.upx" "$SCRIPT_DIR/miao-directory-arm64-linux"
|
||||||
|
|
||||||
# # 压缩macOS AMD64版本
|
# # 压缩macOS AMD64版本
|
||||||
# echo "正在压缩macOS AMD64版本"
|
# echo "正在压缩macOS AMD64版本"
|
||||||
# upx $COMPRESSION_LEVEL -o ../miao-directory-amd64-darwin.upx ../miao-directory-amd64-darwin
|
# upx $COMPRESSION_LEVEL -o "$SCRIPT_DIR/miao-directory-amd64-darwin.upx" "$SCRIPT_DIR/miao-directory-amd64-darwin"
|
||||||
|
|
||||||
# # 压缩macOS ARM64版本
|
# # 压缩macOS ARM64版本
|
||||||
# echo "正在压缩macOS ARM64版本"
|
# echo "正在压缩macOS ARM64版本"
|
||||||
# upx $COMPRESSION_LEVEL -o ../miao-directory-arm64-darwin.upx ../miao-directory-arm64-darwin
|
# upx $COMPRESSION_LEVEL -o "$SCRIPT_DIR/miao-directory-arm64-darwin.upx" "$SCRIPT_DIR/miao-directory-arm64-darwin"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "所有压缩完成"
|
echo "所有压缩完成"
|
||||||
|
|||||||
Reference in new issue
Block a user