You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mail-yubao/deploy/install-prebuilt-image.sh

66 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# 预构建镜像部署 — 服务器不编译,仅 load + 启动
# 用法:
# bash deploy/install-prebuilt-image.sh
# bash deploy/install-prebuilt-image.sh /path/to/yubao-app-image.tar
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
COMPOSE_FILE="${ROOT_DIR}/deploy/docker-compose.prebuilt.yml"
ENV_FILE="${ROOT_DIR}/.env"
IMAGE_TAG="yubao-app:latest"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() { echo -e "${GREEN}[prebuilt]${NC} $*"; }
warn() { echo -e "${YELLOW}[prebuilt]${NC} $*"; }
err() { echo -e "${RED}[prebuilt]${NC} $*" >&2; }
TAR_PATH="${1:-${ROOT_DIR}/deploy/yubao-app-image.tar}"
if [[ ! -f "$ENV_FILE" ]]; then
err "缺少 .env,请先配置 DATABASE_URL 等"
exit 1
fi
if ! command -v docker >/dev/null 2>&1; then
err "未安装 Docker"
exit 1
fi
mkdir -p "${ROOT_DIR}/data"
if docker image inspect "$IMAGE_TAG" >/dev/null 2>&1; then
log "镜像已存在: $IMAGE_TAG"
elif [[ -f "$TAR_PATH" ]]; then
log "加载镜像: $TAR_PATH"
docker load -i "$TAR_PATH"
else
err "未找到镜像 $IMAGE_TAG,也未找到 tar: $TAR_PATH"
err "请先从开发机上传 deploy/yubao-app-image.tar"
exit 1
fi
cd "$ROOT_DIR"
compose=(docker compose --env-file "$ENV_FILE" -f "$COMPOSE_FILE")
log "启动 web + worker(不编译)…"
"${compose[@]}" up -d
log "同步数据库结构…"
"${compose[@]}" run --rm --no-deps web pnpm exec prisma db push --skip-generate
log "同步默认账号…"
"${compose[@]}" run --rm --no-deps web pnpm db:seed || true
"${compose[@]}" ps
APP_PORT="$(grep -E '^APP_PORT=' "$ENV_FILE" | tail -1 | cut -d= -f2- | tr -d '\r' || echo 30327)"
PUBLIC_HOST="$(grep -E '^PUBLIC_HOST=' "$ENV_FILE" | tail -1 | cut -d= -f2- | tr -d '\r' || echo 127.0.0.1)"
log "完成 → http://${PUBLIC_HOST}:${APP_PORT}/login"
warn "更新版本:开发机重新 build-image-local.ps1 → 上传新 tar → bash deploy/update-prebuilt-image.sh"