#!/usr/bin/env bash # 部署 laic-frontend 到生产 nginx 目录 # # 用法: # npm run deploy # ./scripts/deploy.sh # ./scripts/deploy.sh --skip-build # # 依赖: # - Node.js / npm # - ssh 能免密登录 DEPLOY_HOST(默认 jg-serv1,见 ~/.ssh/config) # # 可选环境变量: # DEPLOY_HOST=jg-serv1 # DEPLOY_PATH=/usr/share/nginx/laic-frontend/dist set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" DEPLOY_HOST="${DEPLOY_HOST:-jg-serv1}" DEPLOY_PATH="${DEPLOY_PATH:-/usr/share/nginx/laic-frontend/dist}" SKIP_BUILD=0 for arg in "$@"; do case "$arg" in --skip-build) SKIP_BUILD=1 ;; -h|--help) sed -n '2,20p' "$0" exit 0 ;; *) echo "未知参数: $arg" >&2 echo "支持: --skip-build | --help" >&2 exit 1 ;; esac done need() { command -v "$1" >/dev/null 2>&1 || { echo "缺少命令: $1" >&2 exit 1 } } need npm need ssh need tar echo "==> 检查 SSH: $DEPLOY_HOST" if ! ssh -o BatchMode=yes -o ConnectTimeout=8 "$DEPLOY_HOST" 'echo ok' >/dev/null; then echo "无法免密 SSH 到 $DEPLOY_HOST。请先配置 ~/.ssh/config 与密钥。" >&2 exit 1 fi if [[ "$SKIP_BUILD" -eq 0 ]]; then echo "==> npm run build" npm run build else echo "==> 跳过 build(--skip-build)" fi if [[ ! -d dist ]] || [[ -z "$(ls -A dist 2>/dev/null || true)" ]]; then echo "dist/ 不存在或为空,请先构建。" >&2 exit 1 fi # 清理本地 dist 里的 macOS 垃圾,避免打进包 find dist \( -name '.DS_Store' -o -name '._*' -o -name '.AppleDouble' \) -delete 2>/dev/null || true echo "==> 同步 dist/ -> ${DEPLOY_HOST}:${DEPLOY_PATH}" # COPYFILE_DISABLE 避免 macOS 再生成 AppleDouble;远端 xattr 告警可忽略 COPYFILE_DISABLE=1 tar -C dist -czf - \ --exclude='.DS_Store' \ --exclude='._*' \ --exclude='.AppleDouble' \ . \ | ssh -o BatchMode=yes "$DEPLOY_HOST" \ "mkdir -p '$DEPLOY_PATH' && find '$DEPLOY_PATH' -mindepth 1 -delete && tar -C '$DEPLOY_PATH' -xzf -" echo "==> 校验远端文件" ssh -o BatchMode=yes "$DEPLOY_HOST" \ "test -f '$DEPLOY_PATH/index.html' && ls -lah '$DEPLOY_PATH' | head -20" echo "DEPLOY_OK host=$DEPLOY_HOST path=$DEPLOY_PATH"