107 lines
3.7 KiB
Bash
Executable File
107 lines
3.7 KiB
Bash
Executable File
#!/bin/bash
|
||
set -euo pipefail
|
||
|
||
# ============================================================================
|
||
# deploy_starraid.sh — Star Raid 一键部署(2026-08-06)
|
||
# ----------------------------------------------------------------------------
|
||
# 参考项目本体 agents 的 deploy_agents.sh / refresh_agents.sh 双脚本模式:
|
||
# 本地:git push → rsync 上传 → SSH 触发服务器 refresh_starraid.sh
|
||
# 服务器:/opt/starraid/refresh_starraid.sh(重启 + 健康检查 + nginx reload)
|
||
#
|
||
# 用法:
|
||
# bash deploy_starraid.sh # 默认:push main + rsync + 触发刷新
|
||
# bash deploy_starraid.sh --no-push # 跳过 git push(例如已手动 push)
|
||
# REMOTE_HOST=xxx bash deploy_starraid.sh # 覆盖远程主机
|
||
#
|
||
# 说明:
|
||
# - rsync 排除 .env(生产密钥)、demo/、.DS_Store
|
||
# - Star Raid 是纯静态 + node 服务,无前端构建步骤
|
||
# - 游戏服务器目录 /opt/starraid 无 git,采用 rsync 直传(与项目本体 git pull 模式不同)
|
||
# ============================================================================
|
||
|
||
# ── 配置(可通过环境变量覆盖) ──
|
||
REMOTE_HOST="${REMOTE_HOST:-59.110.19.30}"
|
||
REMOTE_USER="${REMOTE_USER:-root}"
|
||
REMOTE_PORT="${REMOTE_PORT:-22}"
|
||
REMOTE_DIR="${REMOTE_DIR:-/opt/starraid}"
|
||
GIT_BRANCH="${GIT_BRANCH:-main}"
|
||
|
||
LOCAL_ROOT="$(cd "$(dirname "$0")" && pwd)"
|
||
GAME_DIR="$LOCAL_ROOT/star-raid"
|
||
SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
|
||
|
||
log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; }
|
||
|
||
need_cmd() {
|
||
if ! command -v "$1" >/dev/null 2>&1; then
|
||
log "缺少命令 $1,请先安装后重试"
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
ssh_exec() { ssh -p "$REMOTE_PORT" $SSH_OPTS "$REMOTE_USER@$REMOTE_HOST" "$@"; }
|
||
|
||
# ── 参数解析 ──
|
||
SKIP_PUSH=false
|
||
for arg in "$@"; do
|
||
case "$arg" in
|
||
--no-push) SKIP_PUSH=true ;;
|
||
-h|--help)
|
||
echo "用法: bash deploy_starraid.sh [--no-push]"
|
||
echo " --no-push 跳过 git push(仅 rsync 上传 + 服务器刷新)"
|
||
exit 0
|
||
;;
|
||
*) log "未知参数: $arg(支持 --no-push)"; exit 1 ;;
|
||
esac
|
||
done
|
||
|
||
main() {
|
||
need_cmd ssh
|
||
need_cmd rsync
|
||
|
||
# ── 0. 前置检查 ──
|
||
if [[ ! -d "$GAME_DIR" ]]; then
|
||
log "错误: 未找到游戏目录 $GAME_DIR"
|
||
exit 1
|
||
fi
|
||
|
||
# ── 1. Git Push ──
|
||
if $SKIP_PUSH; then
|
||
log "1/3 跳过 git push(--no-push)"
|
||
else
|
||
log "1/3 git push → origin/$GIT_BRANCH"
|
||
cd "$LOCAL_ROOT"
|
||
if ! git diff --quiet --exit-code 2>/dev/null || ! git diff --cached --quiet --exit-code 2>/dev/null; then
|
||
log "警告:有未提交的改动,请先 commit 或 stash"
|
||
log " - 查看: git status"
|
||
log " - 提交: git add -A && git commit -m '...'"
|
||
log " - 暂存: git stash push -m 'WIP'"
|
||
log "若仍要部署,请使用 --no-push 跳过此步骤"
|
||
exit 1
|
||
fi
|
||
git push origin "$GIT_BRANCH"
|
||
log "push 完成"
|
||
fi
|
||
|
||
# ── 2. rsync 上传 ──
|
||
log "2/3 rsync 上传 → $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR"
|
||
# 注意:不用 --delete——服务器端 refresh_starraid.sh 等本地没有的文件必须保留;
|
||
# 且避免本地临时缺文件时连带删除服务器文件(与手动 rsync 命令保持一致)
|
||
rsync -az \
|
||
--exclude='.env' \
|
||
--exclude='.env.example' \
|
||
--exclude='.DS_Store' \
|
||
--exclude='demo/' \
|
||
--exclude='*.log' \
|
||
--exclude='refresh_starraid.sh' \
|
||
"$GAME_DIR/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/"
|
||
log "上传完成"
|
||
|
||
# ── 3. 触发服务器刷新 ──
|
||
log "3/3 触发服务器 refresh_starraid.sh"
|
||
ssh_exec "bash $REMOTE_DIR/refresh_starraid.sh"
|
||
log "全部完成 🚀 https://starraid.cyjai.com"
|
||
}
|
||
|
||
main "$@"
|