Files
vps-manager/deploy/update.sh
T

43 lines
1.3 KiB
Bash
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
# vps-manager 自动更新脚本(由 systemd timer 定时触发)
# 从 Gitea 拉取最新代码,如有更新则重启服务
#
# 可配置项(环境变量):
# VPS_MANAGER_DIR 项目根目录(默认 /opt/vps-manager
# VPS_MANAGER_SVC systemd 服务名(默认 vps-manager
# GIT_BRANCH 跟踪的远端分支(默认 main)
#
# 使用示例:
# VPS_MANAGER_DIR=/srv/vps ./update.sh
# 或在 systemd unit 中设置 Environment=VPS_MANAGER_DIR=/srv/vps
set -euo pipefail
APP_DIR="${VPS_MANAGER_DIR:-/opt/vps-manager}"
SERVICE_NAME="${VPS_MANAGER_SVC:-vps-manager}"
BRANCH="${GIT_BRANCH:-main}"
cd "$APP_DIR"
git fetch -q origin "$BRANCH"
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse "origin/$BRANCH")
if [ "$LOCAL" = "$REMOTE" ]; then
echo "[$(date '+%F %T')] 代码已是最新,无需更新"
exit 0
fi
echo "[$(date '+%F %T')] 检测到新版本: ${LOCAL:0:8} -> ${REMOTE:0:8}"
git pull --ff-only -q origin "$BRANCH"
# 依赖如有变更自动安装(无变化时 pip 会快速跳过)
if [ -x ".venv/bin/pip" ]; then
.venv/bin/pip install -q -r requirements.txt 2>/dev/null || true
fi
# 重启服务加载新代码
systemctl restart "$SERVICE_NAME"
echo "[$(date '+%F %T')] 已重启 $SERVICE_NAME 服务"