feat: deploy.sh支持--target blue|green蓝绿部署+分目录隔离
This commit is contained in:
58
deploy.sh
58
deploy.sh
@@ -36,6 +36,7 @@ LOG_LINES=50; VIEW_STATUS=false; POST_DEPLOY_LOGS=false; INIT_ENV=false
|
|||||||
DEPLOY_SERVICE=false; BACKUP_PATH=""
|
DEPLOY_SERVICE=false; BACKUP_PATH=""
|
||||||
|
|
||||||
# ---------- 选项解析 ----------
|
# ---------- 选项解析 ----------
|
||||||
|
BG_TARGET="" # blue 或 green(仅 production 有效)
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
--dry-run) DRY_RUN=true ;;
|
--dry-run) DRY_RUN=true ;;
|
||||||
@@ -51,20 +52,23 @@ while [[ $# -gt 0 ]]; do
|
|||||||
--post-logs) POST_DEPLOY_LOGS=true ;;
|
--post-logs) POST_DEPLOY_LOGS=true ;;
|
||||||
--init-env) INIT_ENV=true ;;
|
--init-env) INIT_ENV=true ;;
|
||||||
--deploy-service) DEPLOY_SERVICE=true ;;
|
--deploy-service) DEPLOY_SERVICE=true ;;
|
||||||
|
--target) BG_TARGET="${2:-}"; shift ;;
|
||||||
--help|-h)
|
--help|-h)
|
||||||
echo "用法: $0 [development|production] [options]"
|
echo "用法: $0 [development|production] [options]"
|
||||||
|
echo ""
|
||||||
echo "选项:"
|
echo "选项:"
|
||||||
echo " --dry-run 预览模式"
|
echo " --dry-run 预览模式"
|
||||||
echo " --yes, -y 跳过确认"
|
echo " --yes, -y 跳过确认"
|
||||||
echo " --skip-tests 跳过部署后测试"
|
echo " --skip-tests 跳过部署后测试"
|
||||||
echo " --rollback 回滚到指定备份"
|
echo " --target blue|green 蓝绿部署目标(仅 production)"
|
||||||
echo " --backup-list 列出可用备份"
|
echo " --rollback 回滚到指定备份"
|
||||||
echo " --logs [N] 查看后端日志(默认50行)"
|
echo " --backup-list 列出可用备份"
|
||||||
echo " --tail [N] 实时跟踪日志"
|
echo " --logs [N] 查看后端日志(默认50行)"
|
||||||
echo " --status 查看服务状态"
|
echo " --tail [N] 实时跟踪日志"
|
||||||
echo " --post-logs 部署后显示日志"
|
echo " --status 查看服务状态"
|
||||||
echo " --init-env 初始化 systemd service 模板"
|
echo " --post-logs 部署后显示日志"
|
||||||
echo " --deploy-service 上传 systemd service 文件到服务器"
|
echo " --init-env 初始化 systemd service 模板"
|
||||||
|
echo " --deploy-service 上传 systemd service 文件到服务器"
|
||||||
exit 0 ;;
|
exit 0 ;;
|
||||||
*)
|
*)
|
||||||
if [[ "$1" == "development" || "$1" == "production" ]]; then
|
if [[ "$1" == "development" || "$1" == "production" ]]; then
|
||||||
@@ -75,6 +79,15 @@ while [[ $# -gt 0 ]]; do
|
|||||||
esac; shift
|
esac; shift
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# ---------- 蓝绿目标检测 ----------
|
||||||
|
if [ "${APP_ENV}" = "production" ] && [ -n "$BG_TARGET" ]; then
|
||||||
|
case "$BG_TARGET" in
|
||||||
|
blue) BG_PORT="4433"; BG_DIR="rust_backend_blue"; BG_SVC="rust-backend-blue.service" ;;
|
||||||
|
green) BG_PORT="4434"; BG_DIR="rust_backend_green"; BG_SVC="rust-backend-green.service" ;;
|
||||||
|
*) echo "错误: --target 只能是 blue 或 green"; exit 1 ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
# ---------- 环境配置 ----------
|
# ---------- 环境配置 ----------
|
||||||
case "${APP_ENV}" in
|
case "${APP_ENV}" in
|
||||||
development)
|
development)
|
||||||
@@ -86,12 +99,18 @@ case "${APP_ENV}" in
|
|||||||
DB_NAME="milkydata_dev"
|
DB_NAME="milkydata_dev"
|
||||||
DB_USER="milkydata" ;;
|
DB_USER="milkydata" ;;
|
||||||
production)
|
production)
|
||||||
REMOTE_DIR="/root/rust/rust_backend"
|
if [ -n "$BG_TARGET" ]; then
|
||||||
SERVICE_NAME="rust-backend.service"
|
REMOTE_DIR="/root/rust/${BG_DIR}"
|
||||||
|
SERVICE_NAME="${BG_SVC}"
|
||||||
|
BACKEND_PORT="${BG_PORT}"
|
||||||
|
else
|
||||||
|
REMOTE_DIR="/root/rust/rust_backend"
|
||||||
|
SERVICE_NAME="rust-backend.service"
|
||||||
|
BACKEND_PORT="4433"
|
||||||
|
fi
|
||||||
TEST_DOMAIN="https://xmclassmate.top"
|
TEST_DOMAIN="https://xmclassmate.top"
|
||||||
BACKEND_PORT="4433"
|
|
||||||
DB_CONTAINER="1Panel-postgresql-FtMo"
|
DB_CONTAINER="1Panel-postgresql-FtMo"
|
||||||
DB_NAME="milkydata_dev"
|
DB_NAME="milkydata"
|
||||||
DB_USER="milkydata" ;;
|
DB_USER="milkydata" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@@ -132,7 +151,14 @@ if run_tests; then
|
|||||||
log_to_file "DEPLOY success"
|
log_to_file "DEPLOY success"
|
||||||
send_webhook "success" "部署完成"
|
send_webhook "success" "部署完成"
|
||||||
echo ""; log_info "========== 部署完成!=========="
|
echo ""; log_info "========== 部署完成!=========="
|
||||||
echo -e " Git: ${GIT_VERSION}"; echo -e " 环境: ${APP_ENV}"; echo ""
|
echo -e " Git: ${GIT_VERSION}"; echo -e " 环境: ${APP_ENV}"
|
||||||
|
if [ -n "$BG_TARGET" ]; then
|
||||||
|
echo -e " 目标: ${BG_TARGET} (:${BG_PORT})"
|
||||||
|
echo -e ""
|
||||||
|
echo -e " ${YELLOW}切换命令:${NC}"
|
||||||
|
echo -e " ./scripts/switch-env.sh --switch ${BG_TARGET}"
|
||||||
|
fi
|
||||||
|
echo ""
|
||||||
[ "$POST_DEPLOY_LOGS" = true ] && { echo ""; view_logs; }
|
[ "$POST_DEPLOY_LOGS" = true ] && { echo ""; view_logs; }
|
||||||
else
|
else
|
||||||
log_error "========== 部署失败,已自动回滚 =========="
|
log_error "========== 部署失败,已自动回滚 =========="
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ Wants=network.target
|
|||||||
Type=simple
|
Type=simple
|
||||||
User=root
|
User=root
|
||||||
Group=root
|
Group=root
|
||||||
WorkingDirectory=/root/rust/rust_backend
|
WorkingDirectory=/root/rust/rust_backend_blue
|
||||||
ExecStart=/root/rust/rust_backend/rust-backend
|
ExecStart=/root/rust/rust_backend_blue/rust-backend
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=5
|
RestartSec=5
|
||||||
StandardOutput=journal
|
StandardOutput=journal
|
||||||
@@ -31,7 +31,7 @@ NoNewPrivileges=yes
|
|||||||
PrivateTmp=yes
|
PrivateTmp=yes
|
||||||
ProtectSystem=strict
|
ProtectSystem=strict
|
||||||
ProtectHome=read-only
|
ProtectHome=read-only
|
||||||
ReadWritePaths=/root/rust/rust_backend
|
ReadWritePaths=/root/rust/rust_backend_blue
|
||||||
|
|
||||||
SyslogIdentifier=rust-backend-blue
|
SyslogIdentifier=rust-backend-blue
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ Wants=network.target
|
|||||||
Type=simple
|
Type=simple
|
||||||
User=root
|
User=root
|
||||||
Group=root
|
Group=root
|
||||||
WorkingDirectory=/root/rust/rust_backend
|
WorkingDirectory=/root/rust/rust_backend_green
|
||||||
ExecStart=/root/rust/rust_backend/rust-backend
|
ExecStart=/root/rust/rust_backend_green/rust-backend
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=5
|
RestartSec=5
|
||||||
StandardOutput=journal
|
StandardOutput=journal
|
||||||
@@ -31,7 +31,7 @@ NoNewPrivileges=yes
|
|||||||
PrivateTmp=yes
|
PrivateTmp=yes
|
||||||
ProtectSystem=strict
|
ProtectSystem=strict
|
||||||
ProtectHome=read-only
|
ProtectHome=read-only
|
||||||
ReadWritePaths=/root/rust/rust_backend
|
ReadWritePaths=/root/rust/rust_backend_green
|
||||||
|
|
||||||
SyslogIdentifier=rust-backend-green
|
SyslogIdentifier=rust-backend-green
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user