区分开发部署与全量部署,减少样式迭代发布负担

Constraint: 需要保留公网可见的真实部署路径,同时降低日常样式改动的部署成本
Rejected: 仅保留 --fast | 不会重建镜像,无法可靠看到代码/样式变更
Confidence: high
Scope-risk: narrow
Directive: 日常页面/样式迭代优先使用 --dev-deploy,资源或日志更新再使用 --full-deploy
Tested: bash -n scripts/deploy-homepage.sh; scripts/deploy-homepage.sh --help; ./scripts/deploy-homepage.sh --dev-deploy; ./scripts/deploy-homepage.sh --full-deploy
Not-tested: 生产域名切换后的反代行为
This commit is contained in:
SepComet
2026-05-10 15:27:08 +08:00
parent 84159db417
commit 811f8199e0
4 changed files with 155 additions and 19 deletions
+138 -4
View File
@@ -36,6 +36,14 @@ RUN_LOCAL_REBUILD="${RUN_LOCAL_REBUILD:-true}"
# 默认值:true
RUN_STATIC_ASSET_SYNC="${RUN_STATIC_ASSET_SYNC:-true}"
# 是否执行日志同步
# 默认值:true
RUN_LOG_SYNC="${RUN_LOG_SYNC:-true}"
# 刷新 Docker 服务时是否强制重新构建镜像
# 默认值:true
DOCKER_FORCE_BUILD="${DOCKER_FORCE_BUILD:-true}"
# 锁文件路径,避免定时任务重入
# 默认值:/tmp/personal-homepage-deploy.lock
LOCK_FILE="${LOCK_FILE:-/tmp/personal-homepage-deploy.lock}"
@@ -53,6 +61,33 @@ fail() {
exit 1
}
usage() {
cat <<'EOF'
用法:
deploy-homepage.sh [options]
选项:
--preset <dev|full|fast> 使用预设部署档位
--dev-deploy 等价于 --preset dev
--full-deploy 等价于 --preset full
--run-static-asset-sync <true|false> 是否同步静态资源
--run-log-sync <true|false> 是否同步日志目录
--run-local-rebuild <true|false> 是否在宿主机执行 npm run rebuild
--docker-force-build <true|false> docker compose up 时是否加 --build
--rsync-delete <true|false> rsync 时是否启用 --delete
--fast 快速模式(仅刷新容器,不做同步与宿主机 rebuild)
-h, --help 显示帮助
说明:
- 预设档位:
dev = 仅代码/样式部署(不做静态资源同步与日志同步;仍会 docker --build)
full = 全量部署(静态资源 + 日志 + 宿主机 rebuild + docker --build)
fast = 仅快速刷新容器(不做同步、不本地 rebuild、且不 docker --build)
- 命令行参数优先级高于环境变量。
- 布尔值支持: true/false/1/0/yes/no/on/off
EOF
}
require_command() {
command -v "$1" >/dev/null 2>&1 || fail "缺少命令: $1"
}
@@ -64,6 +99,93 @@ is_true() {
esac
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--preset)
[[ $# -ge 2 ]] || fail "参数缺少值: $1"
case "${2,,}" in
dev)
RUN_STATIC_ASSET_SYNC=false
RUN_LOG_SYNC=false
RUN_LOCAL_REBUILD=false
DOCKER_FORCE_BUILD=true
;;
full)
RUN_STATIC_ASSET_SYNC=true
RUN_LOG_SYNC=true
RUN_LOCAL_REBUILD=true
DOCKER_FORCE_BUILD=true
;;
fast)
RUN_STATIC_ASSET_SYNC=false
RUN_LOG_SYNC=false
RUN_LOCAL_REBUILD=false
DOCKER_FORCE_BUILD=false
;;
*)
fail "不支持的 preset: $2(可选: dev|full|fast)"
;;
esac
shift 2
;;
--dev-deploy)
RUN_STATIC_ASSET_SYNC=false
RUN_LOG_SYNC=false
RUN_LOCAL_REBUILD=false
DOCKER_FORCE_BUILD=true
shift
;;
--full-deploy)
RUN_STATIC_ASSET_SYNC=true
RUN_LOG_SYNC=true
RUN_LOCAL_REBUILD=true
DOCKER_FORCE_BUILD=true
shift
;;
--run-static-asset-sync)
[[ $# -ge 2 ]] || fail "参数缺少值: $1"
RUN_STATIC_ASSET_SYNC="$2"
shift 2
;;
--run-log-sync)
[[ $# -ge 2 ]] || fail "参数缺少值: $1"
RUN_LOG_SYNC="$2"
shift 2
;;
--run-local-rebuild)
[[ $# -ge 2 ]] || fail "参数缺少值: $1"
RUN_LOCAL_REBUILD="$2"
shift 2
;;
--docker-force-build)
[[ $# -ge 2 ]] || fail "参数缺少值: $1"
DOCKER_FORCE_BUILD="$2"
shift 2
;;
--rsync-delete)
[[ $# -ge 2 ]] || fail "参数缺少值: $1"
RSYNC_DELETE="$2"
shift 2
;;
--fast)
RUN_STATIC_ASSET_SYNC=false
RUN_LOG_SYNC=false
RUN_LOCAL_REBUILD=false
DOCKER_FORCE_BUILD=false
shift
;;
-h|--help)
usage
exit 0
;;
*)
fail "未知参数: $1(使用 --help 查看可用参数)"
;;
esac
done
}
sync_logs() {
mkdir -p "$LOG_TARGET_DIR"
@@ -108,7 +230,11 @@ refresh_docker_service() {
log "刷新 Docker 服务: $DOCKER_SERVICE_NAME"
(
cd "$SITE_DIR"
docker compose -f "$DOCKER_COMPOSE_FILE" up -d --build "$DOCKER_SERVICE_NAME"
if is_true "$DOCKER_FORCE_BUILD"; then
docker compose -f "$DOCKER_COMPOSE_FILE" up -d --build "$DOCKER_SERVICE_NAME"
else
docker compose -f "$DOCKER_COMPOSE_FILE" up -d "$DOCKER_SERVICE_NAME"
fi
)
}
@@ -135,8 +261,10 @@ main_impl() {
log "DOCKER_COMPOSE_FILE=$DOCKER_COMPOSE_FILE"
log "DOCKER_SERVICE_NAME=$DOCKER_SERVICE_NAME"
log "RSYNC_DELETE=$RSYNC_DELETE"
log "RUN_LOG_SYNC=$RUN_LOG_SYNC"
log "RUN_STATIC_ASSET_SYNC=$RUN_STATIC_ASSET_SYNC"
log "RUN_LOCAL_REBUILD=$RUN_LOCAL_REBUILD"
log "DOCKER_FORCE_BUILD=$DOCKER_FORCE_BUILD"
log "LOCK_FILE=$LOCK_FILE"
if is_true "$RUN_STATIC_ASSET_SYNC"; then
@@ -145,7 +273,11 @@ main_impl() {
log "跳过静态资源同步(RUN_STATIC_ASSET_SYNC=false)"
fi
sync_logs
if is_true "$RUN_LOG_SYNC"; then
sync_logs
else
log "跳过日志同步(RUN_LOG_SYNC=false)"
fi
if is_true "$RUN_LOCAL_REBUILD"; then
run_local_rebuild
@@ -159,9 +291,11 @@ main_impl() {
}
main() {
parse_args "$@"
if ! command -v flock >/dev/null 2>&1; then
log "未找到 flock,跳过锁保护"
main_impl "$@"
main_impl
return 0
fi
@@ -173,7 +307,7 @@ main() {
fi
log "已获取部署锁: $LOCK_FILE"
main_impl "$@"
main_impl
}
main "$@"