From fcbac56989acd35e79aafacb3ed10c134bf5ad4f Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Tue, 18 Aug 2026 13:37:55 +0800 Subject: [PATCH] fix: add 25min timeout to docker buildx build to prevent infinite hang --- scripts/ci/docker_build_push.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scripts/ci/docker_build_push.sh b/scripts/ci/docker_build_push.sh index f3ba4bb80..bff4325ca 100755 --- a/scripts/ci/docker_build_push.sh +++ b/scripts/ci/docker_build_push.sh @@ -3,6 +3,9 @@ # 用法: docker_build_push.sh [--no-cache] [build_arg...] set -eu +# 单次 build 超时时间(秒),防止 docker buildx build 无限挂起 +BUILD_TIMEOUT=1500 + NO_CACHE_FLAG="" if [ "$1" = "--no-cache" ]; then NO_CACHE_FLAG="--no-cache" @@ -43,7 +46,7 @@ build_with_cache_retry() { local build_output local exit_code set +e - build_output=$(docker buildx build \ + build_output=$(timeout ${BUILD_TIMEOUT} docker buildx build \ $NO_CACHE_FLAG \ $BUILD_ARGS \ --cache-from "type=local,src=${LOCAL_CACHE_DIR}" \ @@ -60,6 +63,12 @@ build_with_cache_retry() { echo "$build_output" return 0 fi + # 超时退出(exit code 124) + if [ $exit_code -eq 124 ]; then + echo "❌ Docker build TIMEOUT after ${BUILD_TIMEOUT}s - build hung and was killed" + echo "$build_output" | tail -20 + return $exit_code + fi # 检测到缓存损坏类错误,清掉本地缓存重试 if echo "$build_output" | grep -qE "parent snapshot.*not found|snapshot.*does not exist|cache.*corrupt|failed to compute cache key"; then echo "$build_output" @@ -68,7 +77,7 @@ build_with_cache_retry() { rm -rf "${LOCAL_CACHE_DIR}" mkdir -p "${LOCAL_CACHE_DIR}" # 清理buildx builder的内部snapshot状态 - docker buildx prune -f -a >/dev/null 2>&1 || true + docker buildx prune -f -a > /dev/null 2>&1 || true attempt=$((attempt + 1)) else # 非缓存类错误,直接输出并返回 @@ -78,7 +87,7 @@ build_with_cache_retry() { done # 重试完还是失败,不用本地缓存最后试一次(只从registry读) echo "⚠️ All cached attempts failed, building without local cache..." - docker buildx build \ + timeout ${BUILD_TIMEOUT} docker buildx build \ $NO_CACHE_FLAG \ $BUILD_ARGS \ --cache-from "type=registry,ref=${CACHE_REF}" \ @@ -93,6 +102,7 @@ build_with_cache_retry() { echo "=== Step 1: Build & push image (local cache + registry cache, with auto-repair) ===" echo "Local cache: ${LOCAL_CACHE_DIR}" echo "Registry cache: ${CACHE_REF}" +echo "Build timeout: ${BUILD_TIMEOUT}s" echo "" build_with_cache_retry -- 2.54.0