This commit is contained in:
toom1996
2026-10-20 23:11:57 +08:00
parent 5db90af73a
commit acc0928401
5 changed files with 313 additions and 148 deletions

View File

@ -6,6 +6,7 @@
# ./deploy.sh web 只更新前端
# ./deploy.sh --rollback 切回上一个槽(不动代码,秒级)
# ./deploy.sh all --no-pull 用服务器上现有代码构建
# ./deploy.sh web --no-cache 强制不用构建缓存(前端 SSG 依赖后端数据,数据变了要强制重建)
# ./deploy.sh --prune 只清理旧镜像(保留最近 KEEP_IMAGES 个,默认 5)
# GIT_BRANCH=dev ./deploy.sh all 临时指定分支(优先于 .env)
#
@ -16,14 +17,16 @@ cd "$(dirname "$0")"
TARGET="backend"
NO_PULL=0
NO_CACHE=0
PRUNE_ONLY=0
for arg in "$@"; do
case "$arg" in
backend|web|all) TARGET="$arg" ;;
--rollback) TARGET="rollback" ;;
--no-pull) NO_PULL=1 ;;
--no-cache) NO_CACHE=1 ;;
--prune) PRUNE_ONLY=1 ;;
*) echo "用法: ./deploy.sh [backend|web|all|--rollback|--prune] [--no-pull]"; exit 1 ;;
*) echo "用法: ./deploy.sh [backend|web|all|--rollback|--prune] [--no-pull] [--no-cache]"; exit 1 ;;
esac
done
@ -148,7 +151,9 @@ roll() { # $1=component(backend|web)
old=$(cur_slot "$comp"); idle=$(other_slot "$old")
echo "==> $comp:活跃槽 $old,更新空闲槽 $idle(镜像 $RELEASE)"
docker compose build "${comp}_a" >/dev/null
local build_flag=""
[ "$NO_CACHE" = "1" ] && build_flag="--no-cache"
docker compose build $build_flag "${comp}_a" >/dev/null
# latest 只是给手动执行 compose 命令用的便利别名,deploy 自己从不依赖它
docker tag "fashion-${comp}:${RELEASE}" "fashion-${comp}:latest" 2>/dev/null || true
@ -214,12 +219,14 @@ else
export RELEASE="${RELEASE:-latest}"
fi
# 首次部署先让代理起来(前端构建期要用 18091 取 SSG 数据)
if ! healthy proxy; then
echo "==> 启动代理(首次会一并拉起 db 与 a 槽)"
docker compose up -d proxy
fi
# 首次部署:先写切换点,再只启动代理。
# - render_active 必须先于 proxy 启动,否则 include 的 map 为空,nginx 会因未知变量启动失败。
# - --no-deps 避免首次起代理时顺带构建前端(此时 18091 尚未就绪,SSG 取数会失败)。
render_active
if ! healthy proxy; then
echo "==> 启动代理(首次只起代理,槽位由 roll 流程构建)"
docker compose up -d --no-deps proxy
fi
if [ "$TARGET" = "rollback" ]; then
do_rollback