deploy.ps1 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # 前端部署脚本
  2. # 日期:2025-08-07
  3. # ===== 配置区域 =====
  4. $sourceDir = "D:\project\ln-web\dist"
  5. $remoteUser = "liujia"
  6. $remoteIP = "43.137.10.199"
  7. $remotePath = "/work/web/src"
  8. $sshPort = 22
  9. # ====================
  10. # 创建时间戳压缩包
  11. $timestamp = Get-Date -Format "yyyyMMddHHmmss"
  12. $zipPath = "D:\project\dist-$timestamp.zip"
  13. Write-Host "🗜️ 正在压缩文件..." -ForegroundColor Cyan
  14. Compress-Archive -Path "$sourceDir\*" -DestinationPath $zipPath -CompressionLevel Optimal
  15. $zipSizeMB = [math]::Round((Get-Item $zipPath).Length / 1MB, 2)
  16. Write-Host "🚀 传输压缩包到服务器 ($zipSizeMB MB)..." -ForegroundColor Cyan
  17. scp -P $sshPort $zipPath "${remoteUser}@${remoteIP}:~/dist-latest.zip"
  18. Write-Host "📦 服务器端解压..." -ForegroundColor Cyan
  19. # 执行压缩包解压命令
  20. $sshCommand = "mkdir -p '$remotePath' && unzip -qo ~/dist-latest.zip -d '$remotePath' && rm ~/dist-latest.zip"
  21. # 执行命令并捕获输出
  22. $result = ssh -p $sshPort ${remoteUser}@${remoteIP} $sshCommand 2>&1
  23. # 检查命令执行状态
  24. if ($LASTEXITCODE -eq 0) {
  25. $success = $true
  26. Write-Host "✅ 服务器解压成功" -ForegroundColor Green
  27. } else {
  28. $success = $false
  29. Write-Host "❌ 服务器解压失败: $result" -ForegroundColor Red
  30. }
  31. Write-Host "🧹 清理本地文件..." -ForegroundColor DarkGray
  32. Remove-Item $zipPath -ErrorAction SilentlyContinue
  33. if ($success) {
  34. Write-Host "✅ 部署完成! 压缩包大小: $zipSizeMB MB" -ForegroundColor Green
  35. } else {
  36. Write-Host "❌ 部署失败!" -ForegroundColor Red
  37. Write-Host "手动修复命令:" -ForegroundColor Yellow
  38. Write-Host "ssh -p $sshPort ${remoteUser}@${remoteIP} 'unzip -o ~/dist-latest.zip -d `"$remotePath`"'"
  39. # 添加额外诊断信息
  40. Write-Host "`n诊断信息:" -ForegroundColor Cyan
  41. Write-Host "1. 确保目标目录存在:"
  42. Write-Host " ssh -p $sshPort ${remoteUser}@${remoteIP} 'ls -ld `"$remotePath`"'"
  43. Write-Host "2. 检查磁盘空间:"
  44. Write-Host " ssh -p $sshPort ${remoteUser}@${remoteIP} 'df -h'"
  45. Write-Host "3. 检查文件权限:"
  46. Write-Host " ssh -p $sshPort ${remoteUser}@${remoteIP} 'stat `"$remotePath`"'"
  47. }