构建过程增加进度条

This commit is contained in:
2026-05-21 22:39:26 +08:00
parent eca76cdf48
commit f35b04096d

View File

@@ -244,53 +244,18 @@ build_single_image() {
next_stage next_stage
stage_header "$label" "$est_sec" stage_header "$label" "$est_sec"
# 创建临时文件捕获构建日志 # Docker 默认输出自带进度条 [1/6] [2/6] 等,直接显示到终端
local logfile docker compose -f "$COMPOSE_FILE" build "$service" 2>&1
logfile=$(mktemp /tmp/youdu-wiki-build-XXXXXX.log)
# 在后台构建
docker compose -f "$COMPOSE_FILE" build --progress=plain "$service" >"$logfile" 2>&1 &
local build_pid=$!
# 显示旋转动画,监控构建日志中的关键行
start_spinner "构建中 (可另开终端查看: tail -f $logfile)"
local last_summary=""
while kill -0 "$build_pid" 2>/dev/null; do
# 尝试从日志中提取当前步骤
local current_step
current_step=$(grep -E "^\s*#\d+|^\s*\[.*\]|^#\d+.*RUN|DONE|CACHED|ERROR" "$logfile" 2>/dev/null | tail -1 || true)
if [ -n "$current_step" ] && [ "$current_step" != "$last_summary" ]; then
last_summary="$current_step"
# 清理 spinner 行,打印步骤信息
printf "\r\033[K ${DIM}→ %s${NC}\n" "$(echo "$current_step" | sed 's/^[[:space:]]*//' | cut -c1-80)"
start_spinner "构建中"
fi
sleep 1
done
stop_spinner
# 检查构建结果
wait "$build_pid"
local exit_code=$? local exit_code=$?
local elapsed=$(( $(date +%s) - STAGE_START )) local elapsed=$(( $(date +%s) - STAGE_START ))
if [ $exit_code -eq 0 ]; then if [ $exit_code -eq 0 ]; then
clear_line
printf " ${GREEN}${NC} %s ${DIM}(耗时 %s)${NC}\n" "$label 构建成功" "$(format_time $elapsed)" printf " ${GREEN}${NC} %s ${DIM}(耗时 %s)${NC}\n" "$label 构建成功" "$(format_time $elapsed)"
else else
clear_line
printf " ${RED}${NC} %s ${DIM}(耗时 %s)${NC}\n" "$label 构建失败" "$(format_time $elapsed)" printf " ${RED}${NC} %s ${DIM}(耗时 %s)${NC}\n" "$label 构建失败" "$(format_time $elapsed)"
echo ""
echo -e " ${RED}构建日志 (最后 30 行):${NC}"
echo " ─────────────────────────────────────────"
tail -30 "$logfile" | while IFS= read -r line; do echo " $line"; done
echo " ─────────────────────────────────────────"
echo " 完整日志: $logfile"
rm -f "$logfile"
exit 1 exit 1
fi fi
rm -f "$logfile"
} }
# ============================================ # ============================================