Skip to content

Commit 3957e13

Browse files
committed
Update.
1 parent c51d436 commit 3957e13

File tree

5 files changed

+452
-3
lines changed

5 files changed

+452
-3
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ jobs:
160160
cache-from: type=gha
161161
cache-to: type=gha,mode=max
162162
build-args: |
163-
TARGETOS=linux
164-
TARGETARCH=${{ matrix.goarch }}
165163
TZ=Asia/Shanghai
166164
tags: |
167165
${{ steps.image-name.outputs.GHCR_IMAGE_NAME }}:latest

Dockerfile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
FROM alpine:3.19 AS certs
33
RUN apk update && apk add --no-cache ca-certificates tzdata busybox-static
44

5+
# 二进制文件准备阶段
6+
FROM alpine:3.19 AS binary-prep
7+
ARG TARGETARCH
8+
WORKDIR /prep
9+
10+
# 复制所有构建产物
11+
COPY dist/ ./dist/
12+
13+
# 查找并复制正确的二进制文件
14+
RUN find ./dist -name "*linux*${TARGETARCH}*" -type f -executable | head -1 | xargs -I {} cp {} /prep/app || \
15+
find ./dist -name "*${TARGETARCH}*" -type f -executable | head -1 | xargs -I {} cp {} /prep/app || \
16+
find ./dist -name "server-dash*" -type f -executable | head -1 | xargs -I {} cp {} /prep/app
17+
18+
# 验证二进制文件存在
19+
RUN test -f /prep/app && chmod +x /prep/app
20+
521
# 最终运行阶段
622
FROM scratch
723

@@ -26,7 +42,7 @@ COPY --from=certs /bin/busybox /bin/pgrep
2642

2743
# 复制入口脚本和应用
2844
COPY ./script/entrypoint.sh /entrypoint.sh
29-
COPY dist/server-dash-${TARGETOS}-${TARGETARCH} /dashboard/app
45+
COPY --from=binary-prep /prep/app /dashboard/app
3046

3147
# 复制静态资源文件(重要:应用依赖这些文件)
3248
COPY resource/ /dashboard/resource/

script/build-for-docker.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
# ServerStatus Docker 构建脚本
4+
# 构建多架构二进制文件用于 Docker 镜像
5+
6+
set -e
7+
8+
# 颜色定义
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
BLUE='\033[0;34m'
13+
NC='\033[0m' # No Color
14+
15+
# 日志函数
16+
log_info() {
17+
echo -e "${GREEN}[INFO]${NC} $1"
18+
}
19+
20+
log_warn() {
21+
echo -e "${YELLOW}[WARN]${NC} $1"
22+
}
23+
24+
log_error() {
25+
echo -e "${RED}[ERROR]${NC} $1"
26+
}
27+
28+
# 项目信息
29+
APP_NAME="server-dash"
30+
BUILD_DIR="dist"
31+
MAIN_PATH="./cmd/dashboard"
32+
33+
# 支持的架构
34+
PLATFORMS=(
35+
"linux/amd64"
36+
"linux/arm64"
37+
"linux/s390x"
38+
)
39+
40+
# 创建构建目录
41+
mkdir -p ${BUILD_DIR}
42+
43+
log_info "开始构建 ServerStatus Dashboard..."
44+
45+
# 获取版本信息
46+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
47+
COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
48+
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
49+
50+
log_info "版本信息: ${VERSION} (${COMMIT})"
51+
52+
# 构建标志
53+
LDFLAGS="-s -w -X github.com/xOS/ServerStatus/service/singleton.Version=${VERSION}"
54+
55+
# 构建各个平台的二进制文件
56+
for platform in "${PLATFORMS[@]}"; do
57+
IFS='/' read -r GOOS GOARCH <<< "$platform"
58+
59+
output_name="${APP_NAME}-${GOOS}-${GOARCH}"
60+
output_path="${BUILD_DIR}/${output_name}"
61+
62+
log_info "构建 ${GOOS}/${GOARCH}..."
63+
64+
env GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build \
65+
-ldflags="${LDFLAGS}" \
66+
-trimpath \
67+
-o ${output_path} \
68+
${MAIN_PATH}
69+
70+
if [ $? -eq 0 ]; then
71+
log_info "${output_name} 构建成功"
72+
73+
# 显示文件信息
74+
if command -v ls &> /dev/null; then
75+
ls -lh ${output_path}
76+
fi
77+
else
78+
log_error "${output_name} 构建失败"
79+
exit 1
80+
fi
81+
done
82+
83+
log_info "所有二进制文件构建完成!"
84+
log_info "构建文件位于: ${BUILD_DIR}/"
85+
86+
# 列出所有构建的文件
87+
echo ""
88+
log_info "构建结果:"
89+
ls -lh ${BUILD_DIR}/

script/debug-docker-build.sh

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#!/bin/bash
2+
3+
# Docker 构建调试脚本
4+
# 用于调试 GitHub Actions 中的 Docker 构建问题
5+
6+
set -e
7+
8+
# 颜色定义
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
BLUE='\033[0;34m'
13+
NC='\033[0m'
14+
15+
log_info() {
16+
echo -e "${GREEN}[INFO]${NC} $1"
17+
}
18+
19+
log_warn() {
20+
echo -e "${YELLOW}[WARN]${NC} $1"
21+
}
22+
23+
log_error() {
24+
echo -e "${RED}[ERROR]${NC} $1"
25+
}
26+
27+
log_debug() {
28+
echo -e "${BLUE}[DEBUG]${NC} $1"
29+
}
30+
31+
# 模拟 GitHub Actions 环境
32+
simulate_github_actions() {
33+
log_info "模拟 GitHub Actions 构建环境..."
34+
35+
# 检查是否有构建产物
36+
if [ ! -d "dist" ] || [ -z "$(ls -A dist 2>/dev/null)" ]; then
37+
log_warn "dist 目录为空,运行构建脚本..."
38+
./script/build-for-docker.sh
39+
fi
40+
41+
# 显示构建产物
42+
log_info "当前构建产物:"
43+
ls -la dist/ || log_error "dist 目录不存在"
44+
45+
# 模拟 GitHub Actions 的文件移动过程
46+
log_info "模拟 GitHub Actions 文件处理..."
47+
48+
# 创建临时目录模拟 assets 结构
49+
rm -rf temp-assets
50+
mkdir -p temp-assets/server-dash-linux-amd64
51+
mkdir -p temp-assets/server-dash-linux-arm64
52+
mkdir -p temp-assets/server-dash-linux-s390x
53+
54+
# 复制文件到模拟的 assets 结构
55+
if [ -f "dist/server-dash-linux-amd64" ]; then
56+
cp dist/server-dash-linux-amd64 temp-assets/server-dash-linux-amd64/
57+
fi
58+
if [ -f "dist/server-dash-linux-arm64" ]; then
59+
cp dist/server-dash-linux-arm64 temp-assets/server-dash-linux-arm64/
60+
fi
61+
if [ -f "dist/server-dash-linux-s390x" ]; then
62+
cp dist/server-dash-linux-s390x temp-assets/server-dash-linux-s390x/
63+
fi
64+
65+
# 模拟 GitHub Actions 的移动操作
66+
log_debug "模拟: chmod -R +x ./temp-assets/*"
67+
chmod -R +x ./temp-assets/*
68+
69+
log_debug "模拟: mkdir dist-new && mv ./temp-assets/*/*/* ./dist-new"
70+
mkdir -p dist-new
71+
find temp-assets -type f -executable -exec mv {} dist-new/ \;
72+
73+
# 显示处理后的结果
74+
log_info "处理后的构建产物:"
75+
ls -la dist-new/ || log_error "dist-new 目录为空"
76+
77+
# 备份原始 dist 并替换
78+
if [ -d "dist" ]; then
79+
mv dist dist-backup
80+
fi
81+
mv dist-new dist
82+
83+
log_info "文件处理完成,准备测试 Docker 构建"
84+
}
85+
86+
# 测试 Docker 构建(不推送)
87+
test_docker_build() {
88+
local arch="${1:-amd64}"
89+
90+
log_info "测试 Docker 构建 (架构: $arch)..."
91+
92+
if ! command -v docker &> /dev/null; then
93+
log_error "Docker 未安装,跳过构建测试"
94+
return 1
95+
fi
96+
97+
if ! docker info &> /dev/null; then
98+
log_error "Docker 服务未运行,跳过构建测试"
99+
return 1
100+
fi
101+
102+
# 构建测试镜像
103+
log_debug "运行: docker build --platform linux/$arch --build-arg TARGETARCH=$arch -t serverstatus-test:$arch ."
104+
105+
if docker build --platform "linux/$arch" --build-arg "TARGETARCH=$arch" -t "serverstatus-test:$arch" .; then
106+
log_info "✓ Docker 构建成功 (架构: $arch)"
107+
108+
# 测试镜像内容
109+
log_debug "检查镜像内容..."
110+
docker run --rm "serverstatus-test:$arch" ls -la /dashboard/
111+
112+
return 0
113+
else
114+
log_error "✗ Docker 构建失败 (架构: $arch)"
115+
return 1
116+
fi
117+
}
118+
119+
# 清理测试环境
120+
cleanup() {
121+
log_info "清理测试环境..."
122+
123+
# 恢复原始 dist 目录
124+
if [ -d "dist-backup" ]; then
125+
rm -rf dist
126+
mv dist-backup dist
127+
log_debug "已恢复原始 dist 目录"
128+
fi
129+
130+
# 清理临时文件
131+
rm -rf temp-assets dist-new
132+
133+
# 清理测试镜像
134+
if command -v docker &> /dev/null && docker info &> /dev/null; then
135+
docker images --format "table {{.Repository}}:{{.Tag}}" | grep "serverstatus-test" | while read image; do
136+
log_debug "删除测试镜像: $image"
137+
docker rmi "$image" 2>/dev/null || true
138+
done
139+
fi
140+
141+
log_info "清理完成"
142+
}
143+
144+
# 显示帮助
145+
show_help() {
146+
echo "Docker 构建调试脚本"
147+
echo ""
148+
echo "使用方法:"
149+
echo " $0 [选项]"
150+
echo ""
151+
echo "选项:"
152+
echo " --simulate-only 只模拟 GitHub Actions 环境,不进行构建测试"
153+
echo " --build-only 只进行构建测试,不模拟环境"
154+
echo " --arch ARCH 指定测试架构 (amd64, arm64, s390x)"
155+
echo " --cleanup 清理测试环境"
156+
echo " --help, -h 显示此帮助信息"
157+
echo ""
158+
echo "示例:"
159+
echo " $0 # 完整测试流程"
160+
echo " $0 --arch arm64 # 测试 ARM64 架构"
161+
echo " $0 --simulate-only # 只模拟环境"
162+
echo " $0 --cleanup # 清理测试环境"
163+
}
164+
165+
# 主函数
166+
main() {
167+
local simulate_only=false
168+
local build_only=false
169+
local arch="amd64"
170+
local cleanup_only=false
171+
172+
# 解析参数
173+
while [[ $# -gt 0 ]]; do
174+
case $1 in
175+
--simulate-only)
176+
simulate_only=true
177+
shift
178+
;;
179+
--build-only)
180+
build_only=true
181+
shift
182+
;;
183+
--arch)
184+
arch="$2"
185+
shift 2
186+
;;
187+
--cleanup)
188+
cleanup_only=true
189+
shift
190+
;;
191+
--help|-h)
192+
show_help
193+
exit 0
194+
;;
195+
*)
196+
log_error "未知选项: $1"
197+
show_help
198+
exit 1
199+
;;
200+
esac
201+
done
202+
203+
# 设置清理陷阱
204+
trap cleanup EXIT
205+
206+
if [ "$cleanup_only" = true ]; then
207+
cleanup
208+
exit 0
209+
fi
210+
211+
log_info "开始 Docker 构建调试..."
212+
echo ""
213+
214+
# 模拟 GitHub Actions 环境
215+
if [ "$build_only" != true ]; then
216+
simulate_github_actions
217+
echo ""
218+
fi
219+
220+
# 测试 Docker 构建
221+
if [ "$simulate_only" != true ]; then
222+
if test_docker_build "$arch"; then
223+
log_info "🎉 Docker 构建测试通过!"
224+
else
225+
log_error "❌ Docker 构建测试失败!"
226+
exit 1
227+
fi
228+
fi
229+
230+
log_info "调试完成"
231+
}
232+
233+
# 运行主函数
234+
main "$@"

0 commit comments

Comments
 (0)