From 167d186c8526831b38efcbfd180200278744e5ad Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Wed, 15 Jul 2026 12:58:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(ci):=20M-2=20local=20cache=E4=B8=BA?= =?UTF-8?q?=E4=B8=BB=20+=20registry=20cache=E5=85=9C=E5=BA=95=20-=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BC=93=E5=AD=98=E5=AF=BC=E5=85=A5=E6=85=A2?= =?UTF-8?q?247s=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/ci/docker_build_push.sh | 44 ++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/scripts/ci/docker_build_push.sh b/scripts/ci/docker_build_push.sh index 84fb2c891..d032991f2 100755 --- a/scripts/ci/docker_build_push.sh +++ b/scripts/ci/docker_build_push.sh @@ -1,5 +1,6 @@ #!/bin/bash -# 通用Docker镜像构建+推送脚本(构建与缓存解耦) +# 通用Docker镜像构建+推送脚本(local cache为主 + registry cache兜底) +# M-2优化:解决registry缓存导入慢(247s)和推送不稳定问题 # 用法: docker_build_push.sh [build_arg...] set -eu @@ -21,40 +22,59 @@ else fi docker buildx inspect --bootstrap -CACHE_FROM="type=registry,ref=${CACHE_REF},ignore-error=true" +# 从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 "" -echo "=== Step 1: Build & push image (read-only cache) ===" docker buildx build \ $BUILD_ARGS \ - --cache-from "${CACHE_FROM}" \ + --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" echo "" -echo "=== Step 2: Push cache (best effort, retries 3x) ===" -CACHE_TO="type=registry,ref=${CACHE_REF},mode=max,compression=zstd" +echo "=== Step 2: Sync registry cache (best effort, retries 3x) ===" +CACHE_TO_REGISTRY="type=registry,ref=${CACHE_REF},mode=max,compression=zstd" MAX_RETRIES=3 SUCCESS=0 for attempt in $(seq 1 $MAX_RETRIES); do - echo "Cache push attempt $attempt/$MAX_RETRIES" + echo "Registry cache sync attempt $attempt/$MAX_RETRIES" if docker buildx build \ $BUILD_ARGS \ - --cache-from "${CACHE_FROM}" \ - --cache-to "${CACHE_TO}" \ + --cache-from "${CACHE_FROM_LOCAL}" \ + --cache-to "${CACHE_TO_REGISTRY}" \ -f "${DOCKERFILE}" \ -t "${IMAGE_TAG}" \ --push \ .; then - echo "Cache pushed (attempt $attempt)" + echo "Registry cache synced (attempt $attempt)" SUCCESS=1 break else - echo "Cache push failed (attempt $attempt)" + echo "Registry cache sync failed (attempt $attempt)" if [ $attempt -lt $MAX_RETRIES ]; then WAIT=$((attempt * 5)) echo "Retrying in ${WAIT}s..." @@ -64,7 +84,7 @@ for attempt in $(seq 1 $MAX_RETRIES); do done if [ $SUCCESS -eq 0 ]; then - echo "WARNING: Cache push failed after $MAX_RETRIES attempts (non-fatal)" + echo "WARNING: Registry cache sync failed after $MAX_RETRIES attempts (non-fatal, local cache still works)" fi echo "" -- 2.54.0