chore: update project files
This commit is contained in:
208
demo/deploy.ps1
208
demo/deploy.ps1
@@ -1,78 +1,168 @@
|
||||
# Nginx 反向代理部署脚本 (PowerShell)
|
||||
# deploy.ps1 - Spring Boot + Vue 项目 Docker 部署脚本
|
||||
|
||||
Write-Host "🚀 开始部署 AIGC 平台..." -ForegroundColor Green
|
||||
Write-Host "🚀 AIGC 平台 Docker 部署脚本" -ForegroundColor Green
|
||||
Write-Host ("=" * 60)
|
||||
|
||||
# 检查 Docker
|
||||
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
|
||||
Write-Host "❌ Docker 未安装,请先安装 Docker" -ForegroundColor Red
|
||||
exit 1
|
||||
# 检查必要文件
|
||||
$requiredFiles = @("docker-compose.yml", "backend/Dockerfile", "frontend/Dockerfile", "frontend/nginx.conf")
|
||||
foreach ($file in $requiredFiles) {
|
||||
if (-not (Test-Path $file)) {
|
||||
Write-Host "❌ 缺少必要文件: $file" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
Write-Host "✅ 所有必要文件检查通过" -ForegroundColor Green
|
||||
|
||||
if (-not (Get-Command docker-compose -ErrorAction SilentlyContinue)) {
|
||||
Write-Host "❌ Docker Compose 未安装,请先安装 Docker Compose" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 构建前端
|
||||
Write-Host "📦 构建前端..." -ForegroundColor Yellow
|
||||
Push-Location frontend
|
||||
# 检查 Docker 环境
|
||||
try {
|
||||
npm install
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "❌ 前端依赖安装失败" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
npm run build
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "❌ 前端构建失败" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
# 检查前端构建产物
|
||||
if (-not (Test-Path "frontend/dist")) {
|
||||
Write-Host "❌ 前端构建产物不存在,请检查构建过程" -ForegroundColor Red
|
||||
$dockerVersion = docker --version 2>&1
|
||||
$composeVersion = docker-compose --version 2>&1
|
||||
Write-Host "✅ Docker 环境: $dockerVersion" -ForegroundColor Green
|
||||
Write-Host "✅ Docker Compose: $composeVersion" -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Host "❌ Docker 未正确安装或启动" -ForegroundColor Red
|
||||
Write-Host " 请确保已安装 Docker Desktop 并已启动" -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "✅ 前端构建完成" -ForegroundColor Green
|
||||
# 检查前端是否已构建
|
||||
if (-not (Test-Path "frontend/dist")) {
|
||||
Write-Host "`n⚠️ 前端未构建,开始构建前端..." -ForegroundColor Yellow
|
||||
Push-Location frontend
|
||||
try {
|
||||
if (Test-Path "package.json") {
|
||||
Write-Host " 安装依赖..." -ForegroundColor Cyan
|
||||
npm install
|
||||
Write-Host " 构建生产版本..." -ForegroundColor Cyan
|
||||
npm run build
|
||||
Write-Host "✅ 前端构建完成" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "⚠️ 未找到 package.json,跳过前端构建" -ForegroundColor Yellow
|
||||
}
|
||||
} catch {
|
||||
Write-Host "❌ 前端构建失败: $($_.Exception.Message)" -ForegroundColor Red
|
||||
Write-Host " 可以稍后手动构建: cd frontend && npm install && npm run build" -ForegroundColor Yellow
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
} else {
|
||||
Write-Host "✅ 前端已构建" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# 停止现有容器
|
||||
Write-Host "🛑 停止现有容器..." -ForegroundColor Yellow
|
||||
docker-compose down
|
||||
# 停止现有服务
|
||||
Write-Host "`n🛑 停止现有服务..." -ForegroundColor Yellow
|
||||
docker-compose down 2>&1 | Out-Null
|
||||
|
||||
# 构建并启动服务
|
||||
Write-Host "🔨 构建并启动服务..." -ForegroundColor Yellow
|
||||
docker-compose up -d --build
|
||||
# 构建镜像
|
||||
Write-Host "`n📦 构建 Docker 镜像..." -ForegroundColor Cyan
|
||||
Write-Host " 这可能需要几分钟时间,请耐心等待..." -ForegroundColor Gray
|
||||
$buildResult = docker-compose build --no-cache 2>&1
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "❌ 镜像构建失败" -ForegroundColor Red
|
||||
Write-Host $buildResult -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
Write-Host "✅ 镜像构建完成" -ForegroundColor Green
|
||||
|
||||
# 启动服务
|
||||
Write-Host "`n🐳 启动所有服务..." -ForegroundColor Cyan
|
||||
docker-compose up -d
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "❌ 服务启动失败" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 等待服务启动
|
||||
Write-Host "⏳ 等待服务启动..." -ForegroundColor Yellow
|
||||
Start-Sleep -Seconds 10
|
||||
Write-Host "`n⏳ 等待服务启动(60秒)..." -ForegroundColor Yellow
|
||||
Write-Host " 后端服务需要时间连接数据库并初始化..." -ForegroundColor Gray
|
||||
Start-Sleep -Seconds 60
|
||||
|
||||
# 检查服务状态
|
||||
Write-Host "📊 检查服务状态..." -ForegroundColor Yellow
|
||||
Write-Host "`n🔍 检查服务状态..." -ForegroundColor Cyan
|
||||
docker-compose ps
|
||||
|
||||
# 测试健康检查
|
||||
Write-Host "🏥 测试健康检查..." -ForegroundColor Yellow
|
||||
try {
|
||||
$response = Invoke-WebRequest -Uri "http://localhost/health" -TimeoutSec 5 -UseBasicParsing
|
||||
if ($response.StatusCode -eq 200) {
|
||||
Write-Host "✅ Nginx 健康检查通过" -ForegroundColor Green
|
||||
}
|
||||
} catch {
|
||||
Write-Host "⚠️ Nginx 健康检查失败" -ForegroundColor Yellow
|
||||
# 健康检查
|
||||
Write-Host "`n🏥 服务健康检查..." -ForegroundColor Cyan
|
||||
|
||||
# 检查 MySQL 服务
|
||||
Write-Host " 检查 MySQL..." -ForegroundColor Gray
|
||||
$mysqlStatus = docker-compose ps mysql 2>&1 | Select-String "Up"
|
||||
if ($mysqlStatus) {
|
||||
Write-Host " ✅ MySQL 服务运行中" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " ⚠️ MySQL 服务可能未正常启动" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "✅ 部署完成!" -ForegroundColor Green
|
||||
Write-Host "🌐 前端地址: http://localhost" -ForegroundColor Cyan
|
||||
Write-Host "🔗 API 地址: http://localhost/api" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host "查看日志: docker-compose logs -f" -ForegroundColor Gray
|
||||
Write-Host "停止服务: docker-compose down" -ForegroundColor Gray
|
||||
# 检查 Redis 服务
|
||||
Write-Host " 检查 Redis..." -ForegroundColor Gray
|
||||
$redisStatus = docker-compose ps redis 2>&1 | Select-String "Up"
|
||||
if ($redisStatus) {
|
||||
Write-Host " ✅ Redis 服务运行中" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " ⚠️ Redis 服务可能未正常启动" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# 检查后端服务
|
||||
Write-Host " 检查后端服务..." -ForegroundColor Gray
|
||||
try {
|
||||
# 检查后端服务是否响应
|
||||
$backendTest = Invoke-WebRequest -Uri "http://localhost:8080" -TimeoutSec 5 -ErrorAction SilentlyContinue
|
||||
if ($backendTest.StatusCode -eq 200 -or $backendTest.StatusCode -eq 404 -or $backendTest.StatusCode -eq 302) {
|
||||
Write-Host " ✅ 后端服务可访问 (状态码: $($backendTest.StatusCode))" -ForegroundColor Green
|
||||
}
|
||||
} catch {
|
||||
# 尝试检查API端点
|
||||
try {
|
||||
$apiTest = Invoke-WebRequest -Uri "http://localhost:8080/api" -TimeoutSec 5 -ErrorAction SilentlyContinue
|
||||
Write-Host " ✅ 后端API可访问" -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Host " ⚠️ 后端服务可能还在启动中,请稍后检查" -ForegroundColor Yellow
|
||||
Write-Host " 查看日志: docker-compose logs backend" -ForegroundColor Gray
|
||||
Write-Host " 提示: 首次启动可能需要1-2分钟" -ForegroundColor Gray
|
||||
}
|
||||
}
|
||||
|
||||
# 检查前端服务
|
||||
Write-Host " 检查前端服务..." -ForegroundColor Gray
|
||||
try {
|
||||
$frontendResponse = Invoke-WebRequest -Uri "http://localhost" -TimeoutSec 5 -ErrorAction SilentlyContinue
|
||||
if ($frontendResponse.StatusCode -eq 200) {
|
||||
Write-Host " ✅ 前端服务运行正常" -ForegroundColor Green
|
||||
}
|
||||
} catch {
|
||||
Write-Host " ⚠️ 前端服务检查失败: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
Write-Host " 查看日志: docker-compose logs frontend" -ForegroundColor Gray
|
||||
}
|
||||
|
||||
# 显示访问信息
|
||||
Write-Host "`n" + ("=" * 60) -ForegroundColor Green
|
||||
Write-Host "🎉 部署完成!" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "📍 访问地址:" -ForegroundColor Cyan
|
||||
Write-Host " 前端: http://localhost" -ForegroundColor White
|
||||
Write-Host " 后端API: http://localhost:8080" -ForegroundColor White
|
||||
Write-Host " API文档: http://localhost:8080/api" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "📍 数据库连接信息:" -ForegroundColor Cyan
|
||||
Write-Host " 地址: localhost:3306" -ForegroundColor White
|
||||
Write-Host " 用户: aigc_user" -ForegroundColor White
|
||||
Write-Host " 密码: aigc_password" -ForegroundColor White
|
||||
Write-Host " 数据库: aigc_platform" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "📍 Redis:" -ForegroundColor Cyan
|
||||
Write-Host " 地址: localhost:6379" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "📋 常用命令:" -ForegroundColor Yellow
|
||||
Write-Host " 查看所有服务日志: docker-compose logs -f" -ForegroundColor Gray
|
||||
Write-Host " 查看后端日志: docker-compose logs -f backend" -ForegroundColor Gray
|
||||
Write-Host " 查看前端日志: docker-compose logs -f frontend" -ForegroundColor Gray
|
||||
Write-Host " 查看服务状态: docker-compose ps" -ForegroundColor Gray
|
||||
Write-Host " 停止服务: docker-compose down" -ForegroundColor Gray
|
||||
Write-Host " 停止并删除数据: docker-compose down -v" -ForegroundColor Gray
|
||||
Write-Host " 重新部署: .\deploy.ps1" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "⚠️ 注意事项:" -ForegroundColor Yellow
|
||||
Write-Host " 1. 首次启动可能需要更长时间,请耐心等待" -ForegroundColor Gray
|
||||
Write-Host " 2. 如果服务未正常启动,请查看日志排查问题" -ForegroundColor Gray
|
||||
Write-Host " 3. 确保端口 80、8080、3306、6379 未被占用" -ForegroundColor Gray
|
||||
Write-Host ("=" * 60) -ForegroundColor Green
|
||||
|
||||
Reference in New Issue
Block a user