#!/bin/bash# 部署路径DEPLOY_DIR="/mnt/user/appdata/rsshub/deploy"# 仓库路径REPO_DIR="/mnt/user/appdata/rsshub/git"# 部署分支BRANCH="update-from-upstream"# 日志路径LOG_FILE="/var/log/deployment.log"# 启用日志记录exec>>(tee-a"$LOG_FILE")2>&1echo"==============================================="echo"---- Deployment started: $(date) ----"echo"==============================================="# 检查部署路径是否存在,不存在则创建if[!-d"$DEPLOY_DIR"];thenecho"Deployment directory $DEPLOY_DIR does not exist. Creating..."mkdir-p"$DEPLOY_DIR"if[$?-ne0];thenecho"Error: Failed to create deployment directory $DEPLOY_DIR."exit1fifi# 检查仓库是否为空if[-z"$(git--git-dir="$REPO_DIR"show-ref)"];thenecho"Error: The bare repository is empty. Push some commits to the repository first."exit1fi# 将代码从指定分支导出到部署路径echo"Checking out branch $BRANCH to $DEPLOY_DIR..."# 强制将分支内容检出到工作目录GIT_WORK_TREE="$DEPLOY_DIR"GIT_DIR="$REPO_DIR"gitcheckout-f"$BRANCH"if[$?-ne0];thenecho"Error: Git checkout failed for branch $BRANCH."exit1fi# 确保工作目录与最新提交一致echo"Resetting working tree in $DEPLOY_DIR to latest pushed content..."GIT_WORK_TREE="$DEPLOY_DIR"GIT_DIR="$REPO_DIR"gitreset--hard
if[$?-ne0];thenecho"Error: Git reset failed for branch $BRANCH."exit1fi# 进入部署路径cd"$DEPLOY_DIR"||{echo"Error: Failed to change directory to $DEPLOY_DIR";exit1;}# 检查是否存在 docker-compose.york.yml 文件if[!-f"docker-compose.fork.yml"];thenecho"Error: docker-compose.york.yml not found in $DEPLOY_DIR"exit1fi# 停止并移除现有服务echo"Stopping and removing existing containers..."dockercompose-fdocker-compose.fork.ymldown
# 重建并启动服务echo"Rebuilding and starting services..."dockercompose-fdocker-compose.fork.ymlup-d--force-recreate--build
# 部署完成echo"Deployment finished successfully: $(date)"