qiangdanxitong/deploy_from_git.sh
2026-03-13 10:26:26 +08:00

116 lines
3.6 KiB
Bash
Executable File
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.

#!/bin/bash
###############################################################################
# 从Git拉取代码并部署到服务器
# 服务器: 45.192.99.48
###############################################################################
SERVER_IP="45.192.99.48"
SERVER_USER="root"
SERVER_PASSWORD="1blZllMIp7"
APP_DIR="/opt/designhub"
GIT_REPO="http://chenpei:jsjj654321@git.1080p.hk/chenpei/qiangdanxitong.git"
SERVICE_NAME="designhub-backend"
SERVICE_ALIAS="designhub"
# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'
echo "========================================"
echo -e "${GREEN}从Git拉取代码并部署${NC}"
echo "========================================"
echo ""
echo "服务器: ${SERVER_IP}"
echo "Git仓库: ${GIT_REPO}"
echo ""
# 检查sshpass
if ! command -v sshpass &> /dev/null; then
echo "正在安装sshpass..."
if [[ "$OSTYPE" == "darwin"* ]]; then
brew install hudochenkov/sshpass/sshpass 2>/dev/null || {
echo -e "${RED}请先安装sshpass: brew install hudochenkov/sshpass/sshpass${NC}"
exit 1
}
fi
fi
# 在服务器上执行部署
sshpass -p "${SERVER_PASSWORD}" ssh -o StrictHostKeyChecking=no ${SERVER_USER}@${SERVER_IP} << ENDSSH
set -e
echo -e "${BLUE}[1/6] 检查Git仓库...${NC}"
cd ${APP_DIR}
if [ ! -d ".git" ]; then
echo "初始化Git仓库..."
# 先暂存现有文件
git init
git remote add origin ${GIT_REPO} 2>/dev/null || git remote set-url origin ${GIT_REPO}
# 添加现有文件到Git
git add -A 2>/dev/null || true
git commit -m "Backup existing files" 2>/dev/null || true
# 拉取最新代码(允许合并)
git pull origin main --allow-unrelated-histories || git fetch origin main:main && git checkout main
else
echo "拉取最新代码..."
git fetch origin
git reset --hard origin/main
fi
echo -e "${BLUE}[2/6] 备份当前JAR文件...${NC}"
if [ -f "${APP_DIR}/designhub.jar" ]; then
cp ${APP_DIR}/designhub.jar ${APP_DIR}/designhub.jar.backup.\$(date +%Y%m%d_%H%M%S) || true
fi
echo -e "${BLUE}[3/6] 构建后端...${NC}"
cd ${APP_DIR}/backend
mvn clean package -DskipTests
echo -e "${BLUE}[4/6] 复制JAR文件...${NC}"
cp target/designhub-backend-1.0.0.jar ${APP_DIR}/designhub.jar
echo -e "${BLUE}[5/6] 重启服务...${NC}"
# 确保服务别名和主服务名指向同一个 systemd 单元
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then
if [ -f "/etc/systemd/system/${SERVICE_ALIAS}.service" ] && [ ! -L "/etc/systemd/system/${SERVICE_ALIAS}.service" ]; then
mv "/etc/systemd/system/${SERVICE_ALIAS}.service" "/etc/systemd/system/${SERVICE_ALIAS}.service.bak.\$(date +%Y%m%d_%H%M%S)"
fi
ln -sfn "/etc/systemd/system/${SERVICE_NAME}.service" "/etc/systemd/system/${SERVICE_ALIAS}.service"
systemctl daemon-reload
fi
# 先停止服务
systemctl stop ${SERVICE_NAME} 2>/dev/null || true
systemctl stop ${SERVICE_ALIAS} 2>/dev/null || true
sleep 2
# 确保端口5123没有被占用(清理僵尸Java进程)
pkill -9 -f "/opt/designhub/designhub.jar" 2>/dev/null || true
sleep 2
# 确认端口已释放
if ss -tlnp | grep -q ":5123"; then
echo "端口5123仍被占用,强制清理..."
fuser -k 5123/tcp 2>/dev/null || true
sleep 2
fi
# 重置失败状态并启动服务
systemctl reset-failed ${SERVICE_NAME} ${SERVICE_ALIAS} 2>/dev/null || true
systemctl start ${SERVICE_NAME}
echo ""
echo -e "${GREEN}等待服务启动...${NC}"
sleep 15
echo ""
echo -e "${BLUE}[6/6] 检查服务状态...${NC}"
systemctl status ${SERVICE_NAME} --no-pager | head -15
echo ""
echo -e "${GREEN}✅ 部署完成!${NC}"
ENDSSH
echo ""
echo -e "${GREEN}✅ 部署脚本执行完成!${NC}"