#!/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="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 "错误: 服务器上未安装 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。"