#!/bin/bash # 通用Docker镜像构建+推送脚本(local cache为主 + registry cache兜底) # M-2优化:解决registry缓存导入慢(247s)和推送不稳定问题 # 用法: docker_build_push.sh [build_arg...] set -eu DOCKERFILE="$1" IMAGE_TAG="$2" CACHE_REF="$3" shift 3 BUILD_ARGS="" for arg in "$@"; do BUILD_ARGS="$BUILD_ARGS --build-arg $arg" done if ! docker buildx inspect ci-builder > /dev/null 2>&1; then docker buildx create --use --name ci-builder --driver docker-container echo "Created ci-builder" else docker buildx use ci-builder echo "Using existing ci-builder" fi docker buildx inspect --bootstrap # 从cache_ref中提取缓存名称(如 api-cache:develop -> api-cache-develop) CACHE_NAME=$(echo "$CACHE_REF" | tr '/' '_' | tr ':' '-') LOCAL_CACHE_DIR="/tmp/buildx-cache/${CACHE_NAME}" mkdir -p "$LOCAL_CACHE_DIR" # 缓存源:local优先(带自动修复),registry兜底 # 本地缓存损坏时自动清理后重试,避免snapshot not found导致构建全挂 build_with_cache_retry() { local attempt=1 local max_attempts=2 while [ $attempt -le $max_attempts ]; do local build_output local exit_code set +e build_output=$(docker buildx build \ $BUILD_ARGS \ --cache-from "type=local,src=${LOCAL_CACHE_DIR}" \ --cache-from "type=registry,ref=${CACHE_REF},ignore-error=true" \ --cache-to "type=local,dest=${LOCAL_CACHE_DIR},mode=max" \ -f "${DOCKERFILE}" \ -t "${IMAGE_TAG}" \ --push \ . 2>&1) exit_code=$? set -e if [ $exit_code -eq 0 ]; then echo "$build_output" return 0 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" echo "" echo "⚠️ Local cache appears corrupted, cleaning up and retrying (attempt $attempt/$max_attempts)..." rm -rf "${LOCAL_CACHE_DIR}" mkdir -p "${LOCAL_CACHE_DIR}" # 清理buildx builder的内部snapshot状态 docker buildx prune -f -a >/dev/null 2>&1 || true attempt=$((attempt + 1)) else # 非缓存类错误,直接输出并返回 echo "$build_output" return $exit_code fi done # 重试完还是失败,不用本地缓存最后试一次(只从registry读) echo "⚠️ All cached attempts failed, building without local cache..." docker buildx build \ $BUILD_ARGS \ --cache-from "type=registry,ref=${CACHE_REF},ignore-error=true" \ --cache-to "type=local,dest=${LOCAL_CACHE_DIR},mode=max" \ -f "${DOCKERFILE}" \ -t "${IMAGE_TAG}" \ --push \ . } echo "=== Step 1: Build & push image (local cache + registry read, with auto-repair) ===" echo "Local cache: ${LOCAL_CACHE_DIR}" echo "Registry cache: ${CACHE_REF}" echo "" build_with_cache_retry echo "" echo "Image pushed: ${IMAGE_TAG}" echo "Local cache updated" # DISABLED: registry cache too slow echo "" # DISABLED: registry cache too slow echo "=== Step 2: Sync registry cache (best effort, retries 3x) ===" # DISABLED: registry cache too slow CACHE_TO_REGISTRY="type=registry,ref=${CACHE_REF},mode=max,compression=zstd" # DISABLED: registry cache too slow # DISABLED: registry cache too slow MAX_RETRIES=3 # DISABLED: registry cache too slow SUCCESS=0 # DISABLED: registry cache too slow for attempt in $(seq 1 $MAX_RETRIES); do # DISABLED: registry cache too slow echo "Registry cache sync attempt $attempt/$MAX_RETRIES" # DISABLED: registry cache too slow if docker buildx build \ # DISABLED: registry cache too slow $BUILD_ARGS \ # DISABLED: registry cache too slow --cache-from "${CACHE_FROM_LOCAL}" \ # DISABLED: registry cache too slow --cache-to "${CACHE_TO_REGISTRY}" \ # DISABLED: registry cache too slow -f "${DOCKERFILE}" \ # DISABLED: registry cache too slow -t "${IMAGE_TAG}" \ # DISABLED: registry cache too slow --push \ # DISABLED: registry cache too slow .; then # DISABLED: registry cache too slow echo "Registry cache synced (attempt $attempt)" # DISABLED: registry cache too slow SUCCESS=1 # DISABLED: registry cache too slow break # DISABLED: registry cache too slow else # DISABLED: registry cache too slow echo "Registry cache sync failed (attempt $attempt)" # DISABLED: registry cache too slow if [ $attempt -lt $MAX_RETRIES ]; then # DISABLED: registry cache too slow WAIT=$((attempt * 5)) # DISABLED: registry cache too slow echo "Retrying in ${WAIT}s..." # DISABLED: registry cache too slow sleep $WAIT # DISABLED: registry cache too slow fi # DISABLED: registry cache too slow fi # DISABLED: registry cache too slow done # DISABLED: registry cache too slow # DISABLED: registry cache too slow if [ $SUCCESS -eq 0 ]; then # DISABLED: registry cache too slow echo "WARNING: Registry cache sync failed after $MAX_RETRIES attempts (non-fatal, local cache still works)" # DISABLED: registry cache too slow fi echo "" echo "Build completed: ${IMAGE_TAG}"