Browse Source
Provide scripts/deploy.sh and npm run deploy for build + rsync-like upload to the production nginx dist directory over SSH.main
2 changed files with 82 additions and 1 deletions
@ -0,0 +1,80 @@ |
|||
#!/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 |
|||
|
|||
echo "==> 同步 dist/ -> ${DEPLOY_HOST}:${DEPLOY_PATH}" |
|||
# macOS tar 可能带 Apple xattr;远端 GNU tar 会告警,不影响内容 |
|||
tar -C dist -czf - . \ |
|||
| ssh -o BatchMode=yes "$DEPLOY_HOST" \ |
|||
"mkdir -p '$DEPLOY_PATH' && rm -rf '$DEPLOY_PATH'/* && 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" |
|||
Loading…
Reference in new issue