diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..5a28e41 --- /dev/null +++ b/Caddyfile @@ -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 +} diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..9be36f8 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +# 本地构建 Astro,将 dist/ 同步到服务器(与仓库根目录 Caddyfile 的 root 一致)。 +# +# 依赖: +# 本机: Node/npm、rsync、sshpass(macOS: brew install rsync sshpass) +# 远端: 必须安装 rsync(Ubuntu: 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 "错误: 服务器上未安装 rsync,rsync 同步需要远端也有该命令。" >&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。"