# df-im-gateway 一键发布脚本(复用 publish-u-relay 模式) # 用法: powershell -File scripts\publish-im-gateway.ps1 [-SkipBuild] [-SkipDeploy] # 流程: cargo test → musl 交叉编译 → scp 上传(.new 中转) → systemd 原子切换 → 9191 验证 param( [switch]$SkipBuild, [switch]$SkipDeploy ) $ErrorActionPreference = "Stop" $Repo = "E:/wk-lab/devflow" $Target = "x86_64-unknown-linux-musl" $Server = "root@39.99.243.191" $RemoteDir = "/opt/df-im-gateway" $ServiceName = "df-im-gateway" $LocalBin = "$Repo/target-musl/$Target/release/df-im-gateway" function Step($msg) { Write-Host "`n=== $msg ===" -ForegroundColor Cyan } if (-not $SkipBuild) { Step "Step 0: 单测基线(df-im-gateway)" cargo test -p df-im-gateway --manifest-path "$Repo/Cargo.toml" if ($LASTEXITCODE -ne 0) { throw "单测失败,中止发布" } Step "Step 1: musl 交叉编译(rust-lld self-contained)" $env:CARGO_TARGET_DIR = "$Repo/target-musl" cargo build --release -p df-im-gateway --manifest-path "$Repo/Cargo.toml" --target $Target if ($LASTEXITCODE -ne 0) { throw "编译失败,中止发布" } Remove-Item Env:\CARGO_TARGET_DIR -ErrorAction SilentlyContinue if (-not (Test-Path $LocalBin)) { throw "产物不存在: $LocalBin" } $size = [math]::Round((Get-Item $LocalBin).Length / 1MB, 1) Write-Host "产物: $LocalBin (${size}MB)" } if ($SkipDeploy) { Write-Host "`nSkipDeploy: 仅构建完成"; exit 0 } Step "Step 2: 服务器预检 + 备份" ssh $Server "mkdir -p $RemoteDir && test -f $RemoteDir/df-im-gateway && cp $RemoteDir/df-im-gateway $RemoteDir/df-im-gateway.bak.`$(date +%Y%m%d%H%M%S) || echo '首次部署,无备份'" Step "Step 3: 上传(.new 中转防 Text file busy)" scp $LocalBin "${Server}:$RemoteDir/df-im-gateway.new" if ($LASTEXITCODE -ne 0) { throw "scp 失败" } Step "Step 4: 原子切换 + systemd 重启" ssh $Server "bash -s" @' set -e cd /opt/df-im-gateway systemctl stop df-im-gateway 2>/dev/null || true mv -f df-im-gateway.new df-im-gateway chmod +x df-im-gateway systemctl start df-im-gateway sleep 1 systemctl is-active df-im-gateway '@ if ($LASTEXITCODE -ne 0) { throw "切换/启动失败,检查 journalctl -u df-im-gateway"; } Step "Step 5: 健康验证" ssh $Server "ss -tlnp | grep 9191 && journalctl -u df-im-gateway -n 5 --no-pager | sed 's/\x1b\[[0-9;]*m//g'" if ($LASTEXITCODE -ne 0) { Write-Host "❌ 9191 未监听,执行回滚..." -ForegroundColor Red ssh $Server "bash -s" @' set -e cd /opt/df-im-gateway systemctl stop df-im-gateway LATEST=$(ls -t df-im-gateway.bak.* 2>/dev/null | head -1) if [ -n "$LATEST" ]; then cp "$LATEST" df-im-gateway; chmod +x df-im-gateway; systemctl start df-im-gateway; echo "回滚完成: $LATEST"; else echo "无备份可回滚(首次部署失败,保持停止)"; fi '@ throw "健康检查失败,已回滚" } Write-Host "`n✅ df-im-gateway 发布完成 (9191)" -ForegroundColor Green