Files
hgtk-landingpage/scripts/deploy.sh
2026-04-30 13:33:05 +08:00

85 lines
2.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 本地构建 Astro将 dist/ 同步到服务器(与仓库根目录 Caddyfile 的 root 一致)。
#
# 依赖:
# 本机: Node/npm、rsync、sshpassmacOS: brew install rsync sshpass
# 远端: 必须安装 rsyncUbuntu: apt update && apt install -y rsync
#
# 安全:请勿将填写真实密码后的本文件 push 到公开仓库;更强做法是给 root 配 SSH 公钥并关掉密码登录。
set -euo pipefail
# ========= 部署配置 =========
DEPLOY_HOST="121.4.96.87" # 例: 云服务器公网 IP
DEPLOY_USER="root"
DEPLOY_PATH="/var/www/HeguangtongkunLanding"
DEPLOY_SSH_PORT="22"
# 在下面填写 root 的 SSH 登录密码(勿提交到 git
DEPLOY_SSH_PASSWORD="deB-nB5JW!eDKkU"
# 1 = 同时把仓库根目录 Caddyfile 传到 $DEPLOY_PATH/Caddyfile
DEPLOY_SYNC_CADDYFILE="0"
# ============================
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
if [[ -z "$DEPLOY_HOST" ]]; then
echo "错误: 请在脚本顶部填写 DEPLOY_HOST" >&2
exit 1
fi
if [[ -z "$DEPLOY_SSH_PASSWORD" ]]; then
echo "错误: 请在脚本顶部填写 DEPLOY_SSH_PASSWORD" >&2
exit 1
fi
command -v sshpass >/dev/null 2>&1 || {
echo "错误: 未找到 sshpass请先安装后再运行本脚本" >&2
exit 1
}
export SSHPASS="$DEPLOY_SSH_PASSWORD"
SSH_OPTS=(
-p "$DEPLOY_SSH_PORT"
-o StrictHostKeyChecking=accept-new
-o PubkeyAuthentication=no
-o PreferredAuthentications=password
)
RSYNC_RSH="sshpass -e ssh ${SSH_OPTS[*]}"
REMOTE="${DEPLOY_USER}@${DEPLOY_HOST}"
remote() {
sshpass -e ssh "${SSH_OPTS[@]}" "$REMOTE" "$@"
}
unset_ssh_pass() {
unset SSHPASS
}
trap unset_ssh_pass EXIT
echo "==> Install & build"
npm ci
npm run build
echo "==> Ensure remote dist directory exists"
remote "mkdir -p '${DEPLOY_PATH}/dist'"
echo "==> Check remote rsync"
if ! remote "command -v rsync >/dev/null 2>&1"; then
echo "错误: 服务器上未安装 rsyncrsync 同步需要远端也有该命令。" >&2
echo "请在服务器执行: apt-get update && apt-get install -y rsync" >&2
exit 1
fi
echo "==> Rsync dist/ -> ${REMOTE}:${DEPLOY_PATH}/dist/"
rsync -avz --delete -e "$RSYNC_RSH" \
dist/ "${REMOTE}:${DEPLOY_PATH}/dist/"
if [[ "$DEPLOY_SYNC_CADDYFILE" == "1" ]]; then
echo "==> Rsync Caddyfile"
rsync -avz -e "$RSYNC_RSH" \
"${ROOT}/Caddyfile" "${REMOTE}:${DEPLOY_PATH}/Caddyfile"
echo " 若 Caddy 引用该路径,请在服务器执行: caddy reload --config <你的 Caddyfile 路径>"
fi
echo "==> Done. 静态站点已更新;仅改静态文件时一般无需重启 Caddy。"