diff --git a/deploy.sh b/deploy.sh index bebe316..85d2b6f 100755 --- a/deploy.sh +++ b/deploy.sh @@ -129,6 +129,7 @@ if [ "$ROLLBACK" = true ]; then rollback; exit 0; fi # ---------- 主部署流程 ---------- show_deploy_info confirm_deploy +check_not_active check_dependencies check_service_file check_ssl_expiry diff --git a/lib/common.sh b/lib/common.sh index b0e05fb..cb29bdf 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -106,6 +106,31 @@ check_service_file() { log_info "service 配置检查通过" } +# ---------- 蓝绿部署:检查目标是否为当前活动环境 ---------- +NGINX_CONTAINER="1Panel-openresty-ABu5" +ROOT_PROXY_CONF="/www/sites/xmclassmate.top/proxy/root.conf" + +check_not_active() { + [ -z "$BG_TARGET" ] && return 0 # 非蓝绿部署,跳过 + + local active_env + active_env=$(remote "docker exec ${NGINX_CONTAINER} cat ${ROOT_PROXY_CONF} 2>/dev/null" 2>/dev/null | grep -oP '127\.0\.0\.1:(4433|4434)' | head -1 || echo "") + + local current_target + case "$active_env" in + *4433) current_target="blue" ;; + *4434) current_target="green" ;; + *) current_target="unknown" ;; + esac + + if [ "$current_target" = "$BG_TARGET" ]; then + log_error "禁止部署: ${BG_TARGET} (:${BG_PORT}) 是当前活动环境" + log_error "请部署到待命环境: $( [ "$BG_TARGET" = "blue" ] && echo 'green' || echo 'blue' )" + exit 1 + fi + log_info "目标 ${BG_TARGET} 非活动环境(当前: ${current_target}),可以部署 ✅" +} + deploy_service_file() { log_step "部署 systemd service 文件..." [ "$DRY_RUN" = true ] && { log_dry "上传 $(get_service_file_local) → /etc/systemd/system/${SERVICE_NAME}"; return 0; }