63 lines
3.1 KiB
PowerShell
63 lines
3.1 KiB
PowerShell
# artveris one-click remote upgrade (BaoTa/PM2 deployment).
|
|
# Flow: build upgrade package locally -> upload to server -> remote auto backup (DB + code)
|
|
# -> stop pm2 -> upgrade.sh (replace code / install deps / migrate) -> restart -> health check -> prune old.
|
|
#
|
|
# Usage:
|
|
# .\scripts\upgrade-remote.ps1 # build + deploy (recommended)
|
|
# .\scripts\upgrade-remote.ps1 -SkipBuild # deploy the latest package already in .release
|
|
#
|
|
# Requires: passwordless SSH (key auth) to the server.
|
|
[CmdletBinding()]
|
|
param(
|
|
[string]$Server = 'root@8.134.255.136',
|
|
[string]$RemoteDir = '/www/upgrade',
|
|
[string]$DeployDir = '/www/wwwroot/artveris',
|
|
[string]$Pm2Name = 'server',
|
|
[switch]$SkipBuild
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
Set-StrictMode -Version Latest
|
|
# Remote output is UTF-8; align console decoding to avoid mojibake on Chinese text.
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
|
Set-Location $root
|
|
|
|
if (-not $SkipBuild) {
|
|
Write-Host '[deploy] Building upgrade package (NOTE: rebuilds apps/api/dist and will break a running local dev API)...'
|
|
$python = Join-Path $root '.venv\Scripts\python.exe'
|
|
if (-not (Test-Path -LiteralPath $python)) { $python = 'python' }
|
|
& $python (Join-Path $root 'scripts\build.py') --upgrade
|
|
if ($LASTEXITCODE -ne 0) { throw "Build failed (exit code $LASTEXITCODE)." }
|
|
}
|
|
|
|
$pkg = Get-ChildItem (Join-Path $root '.release') -Filter 'artveris-upgrade-*.tar.gz' -ErrorAction SilentlyContinue |
|
|
Sort-Object LastWriteTime -Descending | Select-Object -First 1
|
|
if ($null -eq $pkg) { throw 'No upgrade package found. Run without -SkipBuild to build one.' }
|
|
$pkgName = $pkg.Name -replace '\.tar\.gz$', ''
|
|
Write-Host ("[deploy] Package: {0} ({1} MB)" -f $pkg.Name, [math]::Round($pkg.Length / 1MB, 2))
|
|
|
|
# Upload the remote step script as LF, no BOM (CRLF breaks bash).
|
|
$stepsSrc = Join-Path $root 'scripts\upgrade-remote-steps.sh'
|
|
if (-not (Test-Path -LiteralPath $stepsSrc)) { throw "Missing $stepsSrc" }
|
|
$stepsTmp = Join-Path $env:TEMP 'upgrade-remote-steps.sh'
|
|
$stepsText = (Get-Content -LiteralPath $stepsSrc -Raw) -replace "`r`n", "`n"
|
|
[System.IO.File]::WriteAllText($stepsTmp, $stepsText, (New-Object System.Text.UTF8Encoding($false)))
|
|
|
|
Write-Host "[deploy] Uploading to ${Server}:${RemoteDir}/ ..."
|
|
ssh -o BatchMode=yes $Server "mkdir -p '$RemoteDir'"
|
|
if ($LASTEXITCODE -ne 0) { throw 'SSH connection failed. Check key-based login.' }
|
|
scp -o BatchMode=yes $pkg.FullName $stepsTmp "${Server}:${RemoteDir}/"
|
|
if ($LASTEXITCODE -ne 0) { throw 'Upload failed.' }
|
|
|
|
Write-Host '[deploy] Running remote upgrade (auto backup first; aborts and keeps backups on failure)...'
|
|
ssh -o BatchMode=yes $Server "bash '$RemoteDir/upgrade-remote-steps.sh' '$pkgName' '$DeployDir' '$Pm2Name'"
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Remote upgrade failed (exit code $LASTEXITCODE). Service may be stopped. Backups are in $RemoteDir/backup/."
|
|
}
|
|
|
|
Write-Host '[deploy] Done. Remote health check passed.'
|
|
if (-not $SkipBuild) {
|
|
Write-Host '[deploy] Tip: restart local dev API with "pnpm dev:api" if it was running before the build.'
|
|
}
|