| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 | # 前端部署脚本# 日期:2025-08-07# ========== 🛠️ 配置参数 ==========$sourceDir = "D:\project\ln-web\dist"$remoteUser = "liujia"$remoteIP = "43.137.10.199"$remotePath = "/work/web/dist" # 实际路径  /work/web/dist$sshPort = 22# === 准备文件 ===$timestamp = Get-Date -Format "yyyyMMddHHmmss"$zipPath = "D:\project\dist-$timestamp.zip"Write-Host "`n🗜️ 正在压缩 dist..." -ForegroundColor CyanCompress-Archive -Path "$sourceDir\*" -DestinationPath $zipPath -CompressionLevel Optimal$zipSizeMB = [math]::Round((Get-Item $zipPath).Length / 1MB, 2)Write-Host "`n🚀 上传压缩包到服务器 ($zipSizeMB MB)..." -ForegroundColor Cyanscp -P $sshPort $zipPath "${remoteUser}@${remoteIP}:~/dist-latest.zip"# === 生成远程 bash 部署脚本 ===$remoteScript = @'#!/bin/bashset -etimestamp={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"fiecho "🚀 替换新目录"sudo mv "$tmpdir" "{1}"echo "🧹 删除压缩包"rm ~/dist-latest.zipecho "✅ 远程部署完成: {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 Cyanscp -P $sshPort $localScriptPath "${remoteUser}@${remoteIP}:~/deploy-temp.sh"Write-Host "`n📦 正在服务器上原子部署 ..." -ForegroundColor Cyanssh -p $sshPort ${remoteUser}@${remoteIP} "bash ~/deploy-temp.sh"# === 清理 ===Write-Host "`n🧹 清理临时文件..." -ForegroundColor DarkGrayssh -p $sshPort ${remoteUser}@${remoteIP} 'rm ~/deploy-temp.sh'Remove-Item $zipPath, $localScriptPath -Force -ErrorAction SilentlyContinueWrite-Host "`n✅ 部署完成,压缩包大小:$zipSizeMB MB" -ForegroundColor Green
 |