<# .SYNOPSIS 统一部署脚本 - 符合大厂标准的自动化部署工具 .DESCRIPTION 功能: 1. 支持多环境部署(dev/test/prod) 2. 参数化构建选项 3. 完整的日志记录 4. 健康检查验证 5. 通知告警机制 .PARAMETER Environment 部署环境:dev/test/prod(默认:dev) .PARAMETER Services 指定要部署的服务列表,默认全部 .PARAMETER BuildBackend 是否构建后端服务(默认:true) .PARAMETER BuildFrontend 是否构建前端项目(默认:true) .PARAMETER BuildImages 是否构建容器镜像(默认:true) .PARAMETER PushImages 是否推送镜像到仓库(默认:false) .PARAMETER SkipTests 是否跳过测试(默认:true) .PARAMETER Verify 是否执行健康检查(默认:true) .EXAMPLE # 部署到开发环境 .\scripts\deploy.ps1 -Environment dev # 部署到生产环境并推送镜像 .\scripts\deploy.ps1 -Environment prod -PushImages -SkipTests:$false # 只部署特定服务 .\scripts\deploy.ps1 -Services auth-service,user-service # 完整构建并部署 .\scripts\deploy.ps1 -BuildBackend -BuildFrontend -BuildImages -PushImages -Deploy -Verify #> param( [string]$Environment = "local-dev", [string[]]$Services = @("api-gateway", "auth-service", "user-service", "order-service", "merchant-service", "product-service", "rider-service", "upload-service", "marketing-service", "map-service"), [bool]$BuildBackend = $true, [bool]$BuildFrontend = $true, [bool]$BuildImages = $true, [bool]$PushImages = $false, [bool]$SkipTests = $true, [bool]$Verify = $true ) # 确保在项目根目录执行 $scriptPath = $MyInvocation.MyCommand.Definition $projectRoot = Split-Path (Split-Path $scriptPath -Parent) -Parent Set-Location $projectRoot # 导入工具函数 Import-Module "$projectRoot\scripts\utils.ps1" -Force # 初始化日志 Initialize-Logger Write-Log "======================================" Write-Log "风七六平台部署脚本" Write-Log "日期: $(Get-Date)" Write-Log "环境: $Environment" Write-Log "服务列表: $($Services -join ', ')" Write-Log "======================================" try { # 1. 获取版本号 Write-Log "`n[阶段1/6] 获取版本号" $VERSION = Get-Version Write-Log "🎯 本次部署版本: v$VERSION" # 2. 更新版本号 Write-Log "`n[阶段2/6] 更新版本号" foreach ($service in $Services) { Update-PomVersion -ServiceName $service -Version $VERSION -ProjectRoot $projectRoot } Update-ComposeVersion -Version $VERSION -ProjectRoot $projectRoot -Environment $Environment # 3. 构建后端服务 if ($BuildBackend) { Write-Log "`n[阶段3/6] 构建后端服务" foreach ($service in $Services) { if (-not (Build-Service -ServiceName $service -ProjectRoot $projectRoot -SkipTests $SkipTests)) { throw "$service 构建失败" } } } # 4. 构建前端项目 if ($BuildFrontend) { Write-Log "`n[阶段4/6] 构建前端项目" $frontendProjects = @("platform_web", "merchant_web", "official_website") foreach ($project in $frontendProjects) { $frontendPath = Join-Path $projectRoot "frontend" $project if (Test-Path $frontendPath) { Write-Log "构建前端: $project" Set-Location $frontendPath $result = Invoke-WithRetry -Command { npm install --silent npm run build } -OperationName "构建前端 $project" -MaxRetries 2 if ($result) { Write-Log "$project 构建成功" "SUCCESS" } else { Write-Log "$project 构建失败" "ERROR" throw "$project 构建失败" } } else { Write-Log "$frontendPath 不存在,跳过" "WARN" } } Set-Location $projectRoot } # 5. 构建镜像 if ($BuildImages) { Write-Log "`n[阶段5/6] 构建容器镜像" foreach ($service in $Services) { if (-not (Build-Image -ServiceName $service -Version $VERSION -Environment $Environment -ProjectRoot $projectRoot)) { throw "$service 镜像构建失败" } } } # 6. 推送镜像 if ($PushImages) { Write-Log "`n[阶段6/6] 推送镜像到仓库" foreach ($service in $Services) { if (-not (Push-Image -ServiceName $service -Version $VERSION -Environment $Environment)) { throw "$service 镜像推送失败" } } } # 7. 健康检查验证 if ($Verify) { Write-Log "`n[验证阶段] 执行健康检查" $healthChecks = @( @{Name = "API Gateway"; Url = "http://localhost:8089/api/health"}, @{Name = "Nacos"; Url = "http://localhost:8848/nacos/v1/ns/health"} ) $allHealthy = $true foreach ($check in $healthChecks) { if (-not (Test-ServiceHealth -ServiceName $check.Name -HealthUrl $check.Url)) { $allHealthy = $false } } if (-not $allHealthy) { throw "健康检查未全部通过" } } # 发送成功通知 $successMessage = "🎉 部署成功!`n版本: v$VERSION`n环境: $Environment`n服务: $($Services -join ', ')`n时间: $(Get-Date)" Write-Log $successMessage "SUCCESS" Send-Notification -Message $successMessage -Level "SUCCESS" } catch { Write-Log "部署失败: $_" "ERROR" Send-Notification -Message "❌ 部署失败!`n环境: $Environment`n错误: $_" -Level "ERROR" exit 1 } Write-Log "`n======================================" Write-Log "部署脚本执行完成" Write-Log "======================================"