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"