|
@@ -1,60 +1,71 @@
|
|
|
-# 前端部署脚本
|
|
|
+# 前端部署脚本
|
|
|
# 日期:2025-08-07
|
|
|
|
|
|
-# ===== 配置区域 =====
|
|
|
+# ========== 🛠️ 配置参数 ==========
|
|
|
$sourceDir = "D:\project\ln-web\dist"
|
|
|
$remoteUser = "liujia"
|
|
|
$remoteIP = "43.137.10.199"
|
|
|
-$remotePath = "/work/web/src"
|
|
|
+$remotePath = "/work/web/test" # 实际路径 /work/web/dist
|
|
|
$sshPort = 22
|
|
|
-# ====================
|
|
|
|
|
|
-# 创建时间戳压缩包
|
|
|
+# === 准备文件 ===
|
|
|
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
|
|
|
$zipPath = "D:\project\dist-$timestamp.zip"
|
|
|
|
|
|
-Write-Host "🗜️ 正在压缩文件..." -ForegroundColor Cyan
|
|
|
+Write-Host "`n🗜️ 正在压缩 dist..." -ForegroundColor Cyan
|
|
|
Compress-Archive -Path "$sourceDir\*" -DestinationPath $zipPath -CompressionLevel Optimal
|
|
|
$zipSizeMB = [math]::Round((Get-Item $zipPath).Length / 1MB, 2)
|
|
|
|
|
|
-Write-Host "🚀 传输压缩包到服务器 ($zipSizeMB MB)..." -ForegroundColor Cyan
|
|
|
+Write-Host "`n🚀 上传压缩包到服务器 ($zipSizeMB MB)..." -ForegroundColor Cyan
|
|
|
scp -P $sshPort $zipPath "${remoteUser}@${remoteIP}:~/dist-latest.zip"
|
|
|
|
|
|
-Write-Host "📦 服务器端解压..." -ForegroundColor Cyan
|
|
|
-
|
|
|
-# 执行压缩包解压命令
|
|
|
-$sshCommand = "mkdir -p '$remotePath' && unzip -qo ~/dist-latest.zip -d '$remotePath' && rm ~/dist-latest.zip"
|
|
|
-
|
|
|
-# 执行命令并捕获输出
|
|
|
-$result = ssh -p $sshPort ${remoteUser}@${remoteIP} $sshCommand 2>&1
|
|
|
-
|
|
|
-# 检查命令执行状态
|
|
|
-if ($LASTEXITCODE -eq 0) {
|
|
|
- $success = $true
|
|
|
- Write-Host "✅ 服务器解压成功" -ForegroundColor Green
|
|
|
-} else {
|
|
|
- $success = $false
|
|
|
- Write-Host "❌ 服务器解压失败: $result" -ForegroundColor Red
|
|
|
-}
|
|
|
-
|
|
|
-Write-Host "🧹 清理本地文件..." -ForegroundColor DarkGray
|
|
|
-Remove-Item $zipPath -ErrorAction SilentlyContinue
|
|
|
-
|
|
|
-if ($success) {
|
|
|
- Write-Host "✅ 部署完成! 压缩包大小: $zipSizeMB MB" -ForegroundColor Green
|
|
|
-} else {
|
|
|
- Write-Host "❌ 部署失败!" -ForegroundColor Red
|
|
|
- Write-Host "手动修复命令:" -ForegroundColor Yellow
|
|
|
- Write-Host "ssh -p $sshPort ${remoteUser}@${remoteIP} 'unzip -o ~/dist-latest.zip -d `"$remotePath`"'"
|
|
|
-
|
|
|
- # 添加额外诊断信息
|
|
|
- Write-Host "`n诊断信息:" -ForegroundColor Cyan
|
|
|
- Write-Host "1. 确保目标目录存在:"
|
|
|
- Write-Host " ssh -p $sshPort ${remoteUser}@${remoteIP} 'ls -ld `"$remotePath`"'"
|
|
|
-
|
|
|
- Write-Host "2. 检查磁盘空间:"
|
|
|
- Write-Host " ssh -p $sshPort ${remoteUser}@${remoteIP} 'df -h'"
|
|
|
-
|
|
|
- Write-Host "3. 检查文件权限:"
|
|
|
- Write-Host " ssh -p $sshPort ${remoteUser}@${remoteIP} 'stat `"$remotePath`"'"
|
|
|
-}
|
|
|
+# === 生成远程 bash 部署脚本 ===
|
|
|
+$remoteScript = @'
|
|
|
+#!/bin/bash
|
|
|
+set -e
|
|
|
+
|
|
|
+timestamp={0}
|
|
|
+tmpdir="/tmp/dist-temp-$timestamp"
|
|
|
+
|
|
|
+echo "📁 创建临时目录 $tmpdir"
|
|
|
+mkdir -p "$tmpdir"
|
|
|
+
|
|
|
+echo "📦 解压上传文件"
|
|
|
+unzip -qo ~/dist-latest.zip -d "$tmpdir"
|
|
|
+
|
|
|
+echo "🔒 设置 root 权限"
|
|
|
+sudo chown -R root:root "$tmpdir"
|
|
|
+
|
|
|
+if [ -d "{1}" ]; then
|
|
|
+ echo "🕐 备份旧目录"
|
|
|
+ sudo mv "{1}" "{1}-backup-$timestamp"
|
|
|
+fi
|
|
|
+
|
|
|
+echo "🚀 替换新目录"
|
|
|
+sudo mv "$tmpdir" "{1}"
|
|
|
+
|
|
|
+echo "🧹 删除压缩包"
|
|
|
+rm ~/dist-latest.zip
|
|
|
+
|
|
|
+echo "✅ 远程部署完成: {1} 替换成功"
|
|
|
+'@ -f $timestamp, $remotePath
|
|
|
+
|
|
|
+
|
|
|
+# === 写入为 LF 格式(避免 \r 问题)===
|
|
|
+$localScriptPath = "D:\project\deploy-temp.sh"
|
|
|
+$remoteScriptLF = $remoteScript -replace "`r", ""
|
|
|
+Set-Content -Path $localScriptPath -Value $remoteScriptLF -Encoding UTF8
|
|
|
+
|
|
|
+# === 上传并执行远程脚本 ===
|
|
|
+Write-Host "`n📤 上传远程部署脚本..." -ForegroundColor Cyan
|
|
|
+scp -P $sshPort $localScriptPath "${remoteUser}@${remoteIP}:~/deploy-temp.sh"
|
|
|
+
|
|
|
+Write-Host "`n📦 正在服务器上原子部署 ..." -ForegroundColor Cyan
|
|
|
+ssh -p $sshPort ${remoteUser}@${remoteIP} "bash ~/deploy-temp.sh"
|
|
|
+
|
|
|
+# === 清理 ===
|
|
|
+Write-Host "`n🧹 清理临时文件..." -ForegroundColor DarkGray
|
|
|
+ssh -p $sshPort ${remoteUser}@${remoteIP} 'rm ~/deploy-temp.sh'
|
|
|
+Remove-Item $zipPath, $localScriptPath -Force -ErrorAction SilentlyContinue
|
|
|
+
|
|
|
+Write-Host "`n✅ 部署完成,压缩包大小:$zipSizeMB MB" -ForegroundColor Green
|