部署脚本

This commit is contained in:
Kevin
2026-04-03 11:25:35 +08:00
parent be26d87075
commit ec3ebf54bb
2 changed files with 96 additions and 0 deletions

12
Caddyfile Normal file
View File

@@ -0,0 +1,12 @@
# Astro 静态构建目录:部署前执行 npm run build并把 root 改成服务器上 dist 的绝对路径。
# 示例:/var/www/HeguangtongkunLanding/dist
heguangtongkun.com {
redir https://www.heguangtongkun.com{uri} permanent
}
www.heguangtongkun.com {
root * /var/www/HeguangtongkunLanding/dist
encode gzip zstd
file_server
}

84
scripts/deploy.sh Executable file
View File

@@ -0,0 +1,84 @@
#!/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="101.43.50.143" # 例: 云服务器公网 IP
DEPLOY_USER="root"
DEPLOY_PATH="/var/www/HeguangtongkunLanding"
DEPLOY_SSH_PORT="22"
# 在下面填写 root 的 SSH 登录密码(勿提交到 git
DEPLOY_SSH_PASSWORD="JbqT9MQILV9RdpYD"
# 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。"