From c19e0d9387e5bcc77e93a2ed0ad12edeaccee602 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 16 Jul 2026 21:26:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(ci):=20staging=E9=83=A8=E7=BD=B2=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=A2=9E=E5=8A=A0Registry=E7=99=BB=E5=BD=95=E5=92=8C?= =?UTF-8?q?=E6=8B=89=E9=95=9C=E5=83=8F=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #390: Registry登录与拉镜像重试 - staging部署前自动登录本地Registry - docker pull增加重试机制,应对网络抖动 - 提升部署稳定性 --- scripts/ci_staging_deploy.sh | 54 +++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/scripts/ci_staging_deploy.sh b/scripts/ci_staging_deploy.sh index e90d6c1b6..1f15d0417 100755 --- a/scripts/ci_staging_deploy.sh +++ b/scripts/ci_staging_deploy.sh @@ -15,6 +15,40 @@ set -eu +# ---- 重试工具函数 ---- +retry_cmd() { + local max_attempts=$1 + local backoff=$2 + shift 2 + local attempt=1 + while [ $attempt -le $max_attempts ]; do + if "$@"; then + return 0 + fi + echo " attempt $attempt/$max_attempts failed, retrying in ${backoff}s..." + sleep $backoff + backoff=$((backoff * 2)) + attempt=$((attempt + 1)) + done + echo " ERROR: failed after $max_attempts retries" + return 1 +} + +retry_docker_login() { + echo "Logging in to registry (up to 3 retries)" + if retry_cmd 3 5 sh -c "printf %s "$REGISTRY_TOKEN" | docker login "$REGISTRY_HOST" -u "$REGISTRY_USER" --password-stdin"; then + return 0 + fi + echo "WARN: docker login failed after retries, will try pull anyway" + return 0 +} + +retry_docker_pull() { + local image=$1 + echo "Pulling $image (up to 3 retries)" + retry_cmd 3 10 docker pull "$image" +} + IMAGE_TAG="${IMAGE_TAG:-}" REGISTRY="${REGISTRY:-xiaoxia-registry.cn-hangzhou.cr.aliyuncs.com/xiaoxiakeji}" REGISTRY_USER="${ACR_USERNAME:-${REGISTRY_USER:-nick0415343655}}" @@ -41,11 +75,11 @@ echo "==========================================" # ---- 登录 Registry ---- if [ -n "$REGISTRY_TOKEN" ]; then - echo "Logging in to registry: $REGISTRY" + echo "==========================================" + echo " Login to Registry (with retries)" + echo "==========================================" REGISTRY_HOST=$(echo "$REGISTRY" | cut -d/ -f1) - printf %s "$REGISTRY_TOKEN" | docker login "$REGISTRY_HOST" -u "$REGISTRY_USER" --password-stdin 2>/dev/null || { - echo "WARN: docker login failed, will try to pull anyway" - } + retry_docker_login fi # ---- Pull 新版本镜像 ---- @@ -57,12 +91,12 @@ LOCAL_API="xiaoxia-saas-api:${IMAGE_TAG}" LOCAL_WORKER="xiaoxia-saas-worker:${IMAGE_TAG}" LOCAL_WEB="xiaoxia-saas-web:${IMAGE_TAG}" -echo "Pulling API image..." -docker pull "$REGISTRY_API" -echo "Pulling Worker image..." -docker pull "$REGISTRY_WORKER" -echo "Pulling Web image..." -docker pull "$REGISTRY_WEB" +echo "==========================================" +echo " Pull images (with retries)" +echo "==========================================" +retry_docker_pull "$REGISTRY_API" +retry_docker_pull "$REGISTRY_WORKER" +retry_docker_pull "$REGISTRY_WEB" # Re-tag 成本地名 docker tag "$REGISTRY_API" "$LOCAL_API"