feat: allow branch selection via CLI and improve auto-detection logic in installation script and update documentation

This commit is contained in:
baoweise-bot
2026-05-31 14:30:29 +08:00
parent 8be03e0e33
commit ec0e4b2ee6
2 changed files with 99 additions and 27 deletions
+28 -10
View File
@@ -82,14 +82,29 @@ fi
# 4. Clone or pull the repository
INSTALL_DIR="/opt/aimilivpn"
DEFAULT_DEPLOY_BRANCH="bate"
# 默认部署分支,生产正式版设为 main
DEFAULT_DEPLOY_BRANCH="main"
# Auto-detect checked-out branch if it's already installed
# 优先获取外部传入的参数作为目标分支,例如 bash install.sh bate
CMD_BRANCH="$1"
# 自动检测本地已安装版本当前所在的分支
CURRENT_BRANCH=""
if [ -d "${INSTALL_DIR}/.git" ]; then
CURRENT_BRANCH=$(cd "${INSTALL_DIR}" && git rev-parse --abbrev-ref HEAD 2>/dev/null)
fi
DEPLOY_BRANCH="${CURRENT_BRANCH:-$DEFAULT_DEPLOY_BRANCH}"
# 确立部署分支优先级:
# 1. 命令行传入的参数 (如 $1 = main/bate)
# 2. 已安装则使用当前分支 CURRENT_BRANCH
# 3. 默认兜底使用 DEFAULT_DEPLOY_BRANCH (即 main)
if [ -n "$CMD_BRANCH" ]; then
DEPLOY_BRANCH="$CMD_BRANCH"
elif [ -n "$CURRENT_BRANCH" ]; then
DEPLOY_BRANCH="$CURRENT_BRANCH"
else
DEPLOY_BRANCH="$DEFAULT_DEPLOY_BRANCH"
fi
echo -e "\n${YELLOW}[2/4] 正在从 GitHub 部署源代码到 ${INSTALL_DIR} (目标分支: ${DEPLOY_BRANCH})...${PLAIN}"
if [ -f "${INSTALL_DIR}/.local_dev" ]; then
@@ -526,13 +541,16 @@ def update_service():
# Fetch remote origin updates
subprocess.run(["git", "fetch", "--all"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Detect remote branch (check origin/main, then origin/master)
branch = "main"
for b in ["main", "master"]:
chk = subprocess.run(["git", "rev-parse", "--verify", f"origin/{b}"], capture_output=True, text=True)
if chk.returncode == 0:
branch = b
break
# Detect remote branch (prefer current local branch, fallback to origin/main or origin/master)
curr = subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True)
branch = curr.stdout.strip() if curr.returncode == 0 else ""
if not branch or branch == "HEAD":
branch = "main"
for b in ["main", "master"]:
chk = subprocess.run(["git", "rev-parse", "--verify", f"origin/{b}"], capture_output=True, text=True)
if chk.returncode == 0:
branch = b
break
local_commit = subprocess.run(["git", "rev-parse", "HEAD"], capture_output=True, text=True).stdout.strip()
remote_commit = subprocess.run(["git", "rev-parse", f"origin/{branch}"], capture_output=True, text=True).stdout.strip()