Files
hospital_performance/spug/deploy-docker-compose.sh

130 lines
4.0 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.

#!/bin/bash
# Spug Docker Compose 自动部署脚本
# 用途:使用 docker-compose 快速部署医院绩效考核系统
set -e
# ==================== 配置参数 ====================
PROJECT_DIR="${SPUG_DEPLOY_DIR:-/var/www/hospital-performance}"
GIT_REPO="${SPUG_GIT_URL:-https://gitea.gentronhealth.com/chenqi/hospital_performance.git}"
GIT_BRANCH="${SPUG_GIT_BRANCH:-main}"
# Docker 配置
DOCKER_TAG="${DOCKER_TAG:-latest}"
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-hospital-performance}"
# 环境变量
export DATABASE_HOST="${DATABASE_HOST:-192.168.110.252}"
export DATABASE_PORT="${DATABASE_PORT:-15432}"
export DATABASE_USER="${DATABASE_USER:-postgresql}"
export DATABASE_PASSWORD="${DATABASE_PASSWORD:-Jchl1528}"
export DATABASE_NAME="${DATABASE_NAME:-hospital_performance}"
export SECRET_KEY="${SECRET_KEY:-change-this-secret-key-in-production}"
export DEBUG="${DEBUG:-False}"
export BACKEND_PORT="${BACKEND_PORT:-8000}"
export FRONTEND_PORT="${FRONTEND_PORT:-80}"
export DOCKER_TAG="${DOCKER_TAG}"
# 日志配置
LOG_FILE="${LOG_FILE:-/var/log/spug/deploy-docker-compose.log}"
mkdir -p /var/log/spug 2>/dev/null || true
# ==================== 工具函数 ====================
log() {
local level=$1
shift
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[${timestamp}] [${level}] $*" | tee -a "${LOG_FILE}"
}
info() { log "INFO" "$@"; }
error() { log "ERROR" "$@"; }
success() { log "SUCCESS" "$@"; }
# ==================== 主流程 ====================
main() {
info "========================================"
info "Spug Docker Compose 自动部署开始"
info "项目目录:${PROJECT_DIR}"
info "Git 分支:${GIT_BRANCH}"
info "========================================"
# 进入项目目录
cd "${PROJECT_DIR}"
# 更新代码
info "========== 更新代码 =========="
if [ ! -d ".git" ]; then
info "首次部署,克隆仓库..."
git clone "${GIT_REPO}" .
git checkout "${GIT_BRANCH}"
else
info "更新现有代码..."
git fetch origin "${GIT_BRANCH}"
git reset --hard "origin/${GIT_BRANCH}"
git clean -fd
fi
commit_hash=$(git rev-parse --short HEAD)
commit_msg=$(git log -1 --pretty=format:"%s")
success "✓ 代码更新完成,当前版本:${commit_hash} - ${commit_msg}"
# 停止旧容器
info "========== 停止旧服务 =========="
if docker-compose ps &>/dev/null; then
docker-compose down || true
success "✓ 旧服务已停止"
else
info "未发现运行中的服务"
fi
# 构建镜像
info "========== 构建 Docker 镜像 =========="
docker-compose build --no-cache
# 启动服务
info "========== 启动服务 =========="
docker-compose up -d
# 等待服务启动
info "等待服务启动..."
sleep 15
# 健康检查
info "========== 执行健康检查 =========="
max_attempts=10
attempt=1
while [ $attempt -le $max_attempts ]; do
if curl -f -s "http://localhost:${BACKEND_PORT}/api/v1/health" > /dev/null 2>&1; then
success "✓ 后端 API 健康检查通过"
break
else
if [ $attempt -eq $max_attempts ]; then
error "✗ 后端 API 健康检查失败"
docker-compose logs backend
exit 1
fi
info "后端服务未就绪,等待... (${attempt}/${max_attempts})"
sleep 5
attempt=$((attempt + 1))
fi
done
# 查看状态
info "========== 服务状态 =========="
docker-compose ps
# 清理旧镜像
info "========== 清理旧镜像 =========="
docker image prune -f
info "========================================"
success "🎉 Docker Compose 部署成功完成!"
info "后端地址http://localhost:${BACKEND_PORT}"
info "前端地址http://localhost:${FRONTEND_PORT}"
info "========================================"
}
main "$@"