#!/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兜底 CACHE_FROM_LOCAL="type=local,src=${LOCAL_CACHE_DIR}" CACHE_FROM_REGISTRY="type=registry,ref=${CACHE_REF},ignore-error=true" # 本地缓存目标(必选,mode=max最大化命中率) CACHE_TO_LOCAL="type=local,dest=${LOCAL_CACHE_DIR},mode=max" echo "=== Step 1: Build & push image (local cache read-write + registry read) ===" echo "Local cache: ${LOCAL_CACHE_DIR}" echo "Registry cache: ${CACHE_REF}" echo "" docker buildx build \ $BUILD_ARGS \ --cache-from "${CACHE_FROM_LOCAL}" \ --cache-from "${CACHE_FROM_REGISTRY}" \ --cache-to "${CACHE_TO_LOCAL}" \ -f "${DOCKERFILE}" \ -t "${IMAGE_TAG}" \ --push \ . 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}"