修复部署脚本 COMPOSE_PROFILES 跳过 MinIO 不生效的问题
- 仅在未设置 COMPOSE_PROFILES 时默认激活 storage profile,避免覆盖环境变量空值 - ensure_docker 改为按数组长度判断,移除恒为 false 的死代码 - start_minio 按 profile 值匹配,避免将来新增 profile 时误判
This commit is contained in:
+53
-18
@@ -16,6 +16,11 @@ HEALTH_URL=""
|
|||||||
READY_TIMEOUT="${READY_TIMEOUT:-120}"
|
READY_TIMEOUT="${READY_TIMEOUT:-120}"
|
||||||
RESET_DB=false
|
RESET_DB=false
|
||||||
RESET_DB_CONFIRM="${RESET_DB_CONFIRM:-}"
|
RESET_DB_CONFIRM="${RESET_DB_CONFIRM:-}"
|
||||||
|
# 激活 Compose profile:默认启用 storage,让内置 MinIO 随部署自动启动。
|
||||||
|
# 若改用外部 S3/OSS,可在环境变量前置 COMPOSE_PROFILES= 赋空值跳过内置 MinIO。
|
||||||
|
if [[ -z "${COMPOSE_PROFILES+x}" ]]; then
|
||||||
|
COMPOSE_PROFILES=(--profile storage)
|
||||||
|
fi
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
printf '\033[1;34m[deploy]\033[0m %s\n' "$*"
|
printf '\033[1;34m[deploy]\033[0m %s\n' "$*"
|
||||||
@@ -45,6 +50,9 @@ print_usage() {
|
|||||||
--reset-db 会删除所有 PostgreSQL 数据,包括订单、任务、后台用户、审计日志和迁移记录。
|
--reset-db 会删除所有 PostgreSQL 数据,包括订单、任务、后台用户、审计日志和迁移记录。
|
||||||
日常结构变更请新增 002_xxx.sql 等渐进迁移,不要改已应用的历史迁移文件。
|
日常结构变更请新增 002_xxx.sql 等渐进迁移,不要改已应用的历史迁移文件。
|
||||||
它不会删除 apps/backend/data 下的平台 JSON 配置,也不会删除 Caddy 证书卷。
|
它不会删除 apps/backend/data 下的平台 JSON 配置,也不会删除 Caddy 证书卷。
|
||||||
|
|
||||||
|
默认激活 storage profile 启动内置 MinIO。若使用外部 S3/OSS,可在环境变量前置
|
||||||
|
COMPOSE_PROFILES= 赋空值跳过内置 MinIO,例如:COMPOSE_PROFILES= bash deploy/ubuntu-deploy.sh
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +95,11 @@ ensure_docker() {
|
|||||||
command -v docker >/dev/null 2>&1 || fail "未找到 docker,请先在 Ubuntu 上安装 Docker Engine。"
|
command -v docker >/dev/null 2>&1 || fail "未找到 docker,请先在 Ubuntu 上安装 Docker Engine。"
|
||||||
docker compose version >/dev/null 2>&1 || fail "未找到 docker compose 插件,请安装 Docker Compose plugin。"
|
docker compose version >/dev/null 2>&1 || fail "未找到 docker compose 插件,请安装 Docker Compose plugin。"
|
||||||
command -v curl >/dev/null 2>&1 || fail "未找到 curl,请先安装:sudo apt-get update && sudo apt-get install -y curl"
|
command -v curl >/dev/null 2>&1 || fail "未找到 curl,请先安装:sudo apt-get update && sudo apt-get install -y curl"
|
||||||
|
|
||||||
|
# 允许通过环境变量 COMPOSE_PROFILES= 跳过内置 MinIO(用外部 S3/OSS 时)。
|
||||||
|
if [[ "${#COMPOSE_PROFILES[@]}" -eq 0 ]]; then
|
||||||
|
log "COMPOSE_PROFILES 为空,跳过内置 MinIO(请确保 .env 的 STORAGE_ENDPOINT 指向外部存储)"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure_env_file() {
|
ensure_env_file() {
|
||||||
@@ -182,20 +195,20 @@ ensure_data_dir() {
|
|||||||
|
|
||||||
check_compose_config() {
|
check_compose_config() {
|
||||||
log "检查 Docker Compose 配置"
|
log "检查 Docker Compose 配置"
|
||||||
docker compose config >/dev/null || fail "Docker Compose 配置校验失败。"
|
docker compose "${COMPOSE_PROFILES[@]}" config >/dev/null || fail "Docker Compose 配置校验失败。"
|
||||||
}
|
}
|
||||||
|
|
||||||
pull_base_images() {
|
pull_base_images() {
|
||||||
log "拉取基础镜像"
|
log "拉取基础镜像"
|
||||||
docker compose pull --ignore-buildable
|
docker compose "${COMPOSE_PROFILES[@]}" pull --ignore-buildable
|
||||||
}
|
}
|
||||||
|
|
||||||
build_images() {
|
build_images() {
|
||||||
log "构建后端镜像"
|
log "构建后端镜像"
|
||||||
docker compose build backend || fail "后端镜像构建失败。"
|
docker compose "${COMPOSE_PROFILES[@]}" build backend || fail "后端镜像构建失败。"
|
||||||
|
|
||||||
log "构建 Web 镜像"
|
log "构建 Web 镜像"
|
||||||
docker compose build web || fail "Web 镜像构建失败。"
|
docker compose "${COMPOSE_PROFILES[@]}" build web || fail "Web 镜像构建失败。"
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve_compose_project_name() {
|
resolve_compose_project_name() {
|
||||||
@@ -212,7 +225,7 @@ resolve_compose_project_name() {
|
|||||||
resolve_postgres_volume_name() {
|
resolve_postgres_volume_name() {
|
||||||
local container_id volume_name project_name
|
local container_id volume_name project_name
|
||||||
|
|
||||||
container_id="$(docker compose ps -aq postgres 2>/dev/null | head -n 1 || true)"
|
container_id="$(docker compose "${COMPOSE_PROFILES[@]}" ps -aq postgres 2>/dev/null | head -n 1 || true)"
|
||||||
if [[ -n "${container_id}" ]]; then
|
if [[ -n "${container_id}" ]]; then
|
||||||
volume_name="$(
|
volume_name="$(
|
||||||
docker inspect -f '{{range .Mounts}}{{if eq .Destination "/var/lib/postgresql/data"}}{{if eq .Type "volume"}}{{.Name}}{{end}}{{end}}{{end}}' "${container_id}" 2>/dev/null || true
|
docker inspect -f '{{range .Mounts}}{{if eq .Destination "/var/lib/postgresql/data"}}{{if eq .Type "volume"}}{{.Name}}{{end}}{{end}}{{end}}' "${container_id}" 2>/dev/null || true
|
||||||
@@ -224,7 +237,7 @@ resolve_postgres_volume_name() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
volume_name="$(
|
volume_name="$(
|
||||||
docker compose volumes 2>/dev/null \
|
docker compose "${COMPOSE_PROFILES[@]}" volumes 2>/dev/null \
|
||||||
| awk '$2 ~ /(^|_)postgres_data$/ { print $2; exit }' || true
|
| awk '$2 ~ /(^|_)postgres_data$/ { print $2; exit }' || true
|
||||||
)"
|
)"
|
||||||
if [[ -n "${volume_name}" ]]; then
|
if [[ -n "${volume_name}" ]]; then
|
||||||
@@ -245,8 +258,8 @@ reset_database() {
|
|||||||
volume_name="$(resolve_postgres_volume_name)"
|
volume_name="$(resolve_postgres_volume_name)"
|
||||||
|
|
||||||
warn "开始重置数据库,将删除 Docker volume:${volume_name}"
|
warn "开始重置数据库,将删除 Docker volume:${volume_name}"
|
||||||
docker compose stop web backend postgres >/dev/null 2>&1 || true
|
docker compose "${COMPOSE_PROFILES[@]}" stop web backend postgres >/dev/null 2>&1 || true
|
||||||
docker compose rm -f -s postgres >/dev/null 2>&1 || true
|
docker compose "${COMPOSE_PROFILES[@]}" rm -f -s postgres >/dev/null 2>&1 || true
|
||||||
|
|
||||||
if docker volume inspect "${volume_name}" >/dev/null 2>&1; then
|
if docker volume inspect "${volume_name}" >/dev/null 2>&1; then
|
||||||
docker volume rm "${volume_name}" >/dev/null || fail "删除 PostgreSQL 数据卷失败:${volume_name}"
|
docker volume rm "${volume_name}" >/dev/null || fail "删除 PostgreSQL 数据卷失败:${volume_name}"
|
||||||
@@ -265,7 +278,7 @@ wait_for_service() {
|
|||||||
log "等待 ${label} 就绪"
|
log "等待 ${label} 就绪"
|
||||||
|
|
||||||
for ((attempt = 1; attempt <= READY_TIMEOUT; attempt++)); do
|
for ((attempt = 1; attempt <= READY_TIMEOUT; attempt++)); do
|
||||||
container_id="$(docker compose ps -q "${service}" 2>/dev/null || true)"
|
container_id="$(docker compose "${COMPOSE_PROFILES[@]}" ps -q "${service}" 2>/dev/null || true)"
|
||||||
if [[ -n "${container_id}" ]]; then
|
if [[ -n "${container_id}" ]]; then
|
||||||
status="$(docker inspect -f '{{.State.Status}}' "${container_id}" 2>/dev/null || true)"
|
status="$(docker inspect -f '{{.State.Status}}' "${container_id}" 2>/dev/null || true)"
|
||||||
health_status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' "${container_id}" 2>/dev/null || true)"
|
health_status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' "${container_id}" 2>/dev/null || true)"
|
||||||
@@ -294,7 +307,7 @@ wait_for_service() {
|
|||||||
|
|
||||||
start_postgres() {
|
start_postgres() {
|
||||||
log "启动 PostgreSQL"
|
log "启动 PostgreSQL"
|
||||||
if docker compose up -d --no-deps postgres; then
|
if docker compose "${COMPOSE_PROFILES[@]}" up -d --no-deps postgres; then
|
||||||
wait_for_service postgres "PostgreSQL"
|
wait_for_service postgres "PostgreSQL"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -303,9 +316,30 @@ start_postgres() {
|
|||||||
fail "PostgreSQL 启动失败,请根据上面的日志排查。"
|
fail "PostgreSQL 启动失败,请根据上面的日志排查。"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start_minio() {
|
||||||
|
# 未激活 storage profile 时跳过内置 MinIO(用外部 S3/OSS 的场景)。
|
||||||
|
local has_storage_profile=false
|
||||||
|
for arg in "${COMPOSE_PROFILES[@]}"; do
|
||||||
|
[[ "${arg}" == "storage" ]] && has_storage_profile=true
|
||||||
|
done
|
||||||
|
if [[ "${has_storage_profile}" != "true" ]]; then
|
||||||
|
log "跳过内置 MinIO(COMPOSE_PROFILES 未包含 storage)"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
log "启动 MinIO"
|
||||||
|
if docker compose "${COMPOSE_PROFILES[@]}" up -d --no-deps minio; then
|
||||||
|
wait_for_service minio "MinIO"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_diagnostics
|
||||||
|
fail "MinIO 启动失败,请根据上面的日志排查。"
|
||||||
|
}
|
||||||
|
|
||||||
run_database_migrations() {
|
run_database_migrations() {
|
||||||
log "执行数据库迁移"
|
log "执行数据库迁移"
|
||||||
if docker compose run --rm --no-deps backend node dist/db/migrate.js; then
|
if docker compose "${COMPOSE_PROFILES[@]}" run --rm --no-deps backend node dist/db/migrate.js; then
|
||||||
log "数据库迁移完成"
|
log "数据库迁移完成"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -316,7 +350,7 @@ run_database_migrations() {
|
|||||||
|
|
||||||
restart_backend() {
|
restart_backend() {
|
||||||
log "重建并启动后端"
|
log "重建并启动后端"
|
||||||
if docker compose up -d --no-deps --force-recreate backend; then
|
if docker compose "${COMPOSE_PROFILES[@]}" up -d --no-deps --force-recreate backend; then
|
||||||
wait_for_service backend "后端"
|
wait_for_service backend "后端"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -327,7 +361,7 @@ restart_backend() {
|
|||||||
|
|
||||||
restart_web() {
|
restart_web() {
|
||||||
log "重建并启动 Web 入口"
|
log "重建并启动 Web 入口"
|
||||||
if docker compose up -d --no-deps --force-recreate --remove-orphans web; then
|
if docker compose "${COMPOSE_PROFILES[@]}" up -d --no-deps --force-recreate --remove-orphans web; then
|
||||||
wait_for_service web "Web"
|
wait_for_service web "Web"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -342,6 +376,7 @@ deploy_stack() {
|
|||||||
build_images
|
build_images
|
||||||
reset_database
|
reset_database
|
||||||
start_postgres
|
start_postgres
|
||||||
|
start_minio
|
||||||
run_database_migrations
|
run_database_migrations
|
||||||
restart_backend
|
restart_backend
|
||||||
restart_web
|
restart_web
|
||||||
@@ -366,7 +401,7 @@ print_applied_migrations() {
|
|||||||
log "已应用数据库迁移"
|
log "已应用数据库迁移"
|
||||||
|
|
||||||
local migrations
|
local migrations
|
||||||
if migrations="$(docker compose exec -T backend node --input-type=module -e '
|
if migrations="$(docker compose "${COMPOSE_PROFILES[@]}" exec -T backend node --input-type=module -e '
|
||||||
const { query, closeDb } = await import("./dist/db/client.js");
|
const { query, closeDb } = await import("./dist/db/client.js");
|
||||||
try {
|
try {
|
||||||
const result = await query("SELECT filename FROM schema_migrations ORDER BY filename");
|
const result = await query("SELECT filename FROM schema_migrations ORDER BY filename");
|
||||||
@@ -385,11 +420,11 @@ try {
|
|||||||
|
|
||||||
print_diagnostics() {
|
print_diagnostics() {
|
||||||
warn "当前容器状态:"
|
warn "当前容器状态:"
|
||||||
docker compose ps || true
|
docker compose "${COMPOSE_PROFILES[@]}" ps || true
|
||||||
|
|
||||||
warn "后端最近日志:"
|
warn "后端最近日志:"
|
||||||
local backend_logs
|
local backend_logs
|
||||||
backend_logs="$(docker compose logs --tail=120 backend 2>&1 || true)"
|
backend_logs="$(docker compose "${COMPOSE_PROFILES[@]}" logs --tail=120 backend 2>&1 || true)"
|
||||||
printf '%s\n' "${backend_logs}"
|
printf '%s\n' "${backend_logs}"
|
||||||
|
|
||||||
if printf '%s\n' "${backend_logs}" | grep -q 'password authentication failed for user "postgres"'; then
|
if printf '%s\n' "${backend_logs}" | grep -q 'password authentication failed for user "postgres"'; then
|
||||||
@@ -399,7 +434,7 @@ print_diagnostics() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
warn "后端 ready 接口:"
|
warn "后端 ready 接口:"
|
||||||
docker compose exec -T backend sh -lc 'curl -fsS http://127.0.0.1:3000/health/ready || true' || true
|
docker compose "${COMPOSE_PROFILES[@]}" exec -T backend sh -lc 'curl -fsS http://127.0.0.1:3000/health/ready || true' || true
|
||||||
|
|
||||||
print_applied_migrations || true
|
print_applied_migrations || true
|
||||||
}
|
}
|
||||||
@@ -419,7 +454,7 @@ main() {
|
|||||||
print_applied_migrations
|
print_applied_migrations
|
||||||
|
|
||||||
log "部署完成"
|
log "部署完成"
|
||||||
docker compose ps
|
docker compose "${COMPOSE_PROFILES[@]}" ps
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user