Files
brass-and-sigil/scripts/Deploy-Brass.ps1
T
Matt Sijbers 4e31f5ab1a Deploy-Brass: prevent silent loss of launcher metadata in manifest
Two safeguards so a -Stage Pack run never strips launcherVersion/
launcherUrl from the deployed manifest (the original bug that left
old launchers unable to see upgrade prompts):

  1. Always pass -LauncherExePath to Build-Pack.ps1 when a local
     publish exists, regardless of stage. Previously this only fired
     for stages that included Launcher, so Pack-only deploys regressed
     the manifest to a version with no launcher metadata.

  2. New post-deploy verification step fetches the published manifest
     and asserts: pack version matches lockfile, launcherVersion +
     launcherUrl present, all self-hosted URLs use the configured
     manifest host. Throws on any mismatch.
2026-05-20 21:35:39 +01:00

291 lines
15 KiB
PowerShell

#requires -Version 5
<#
.SYNOPSIS
One-shot deploy: build launcher + server, regenerate manifest, mirror the
deploy share, scp the server binary.
.DESCRIPTION
Reads `deploy.config.ps1` (sibling file, gitignored) for local paths +
SSH details. Stages run in order; -Stage limits which stages run.
The script does NOT auto-restart the production daemon. After a server
binary deploy it prompts you to do that yourself.
.PARAMETER Stage
All | Launcher | Server | Pack. Defaults to All.
Launcher = build launcher + regenerate manifest + push to deploy share
Server = build server + scp binary (atomic swap)
Pack = regenerate manifest + mirror pack/overrides/* to share
All = everything, in order
.PARAMETER SkipBuild
Skip dotnet publish steps. Use when you've already built and just want
to push artifacts.
.PARAMETER DryRun
Print each action without executing. No files copied, no SSH, no build.
#>
[CmdletBinding()]
param(
[ValidateSet('All','Launcher','Server','Pack')]
[string]$Stage = 'All',
[switch]$SkipBuild,
[switch]$DryRun,
# Skip the version-bump check. Use only for cosmetic/internal-only changes
# where you're SURE clients don't need to re-sync. The default is to refuse
# if the local pack/launcher version matches what's already deployed.
[switch]$Force
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
# ─── Resolve repo + load config ────────────────────────────────────────────
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$repoRoot = Resolve-Path (Join-Path $here '..')
$cfgPath = Join-Path $here 'deploy.config.ps1'
if (-not (Test-Path $cfgPath)) {
throw "Missing $cfgPath. Copy deploy.config.template.ps1 -> deploy.config.ps1 and fill in real values."
}
. $cfgPath
# Sanity-check required vars actually got set (template ships with CHANGE_ME).
foreach ($v in 'DeployShare','ServerSshHost','ServerSshKey','ServerBinaryRemote') {
$val = Get-Variable -Name $v -ValueOnly -ErrorAction Stop
if ($val -match 'CHANGE_ME' -or [string]::IsNullOrWhiteSpace($val)) {
throw "deploy.config.ps1 has placeholder/empty `$$v. Fill it in."
}
}
$shouldRunLauncher = $Stage -in @('All','Launcher')
$shouldRunServer = $Stage -in @('All','Server')
$shouldRunPack = $Stage -in @('All','Launcher','Pack') # manifest needs launcher exe meta
# ─── Pre-flight: version-bump check ────────────────────────────────────────
# The launcher caches the pack by version: a client that already synced
# pack v0.9.2 will short-circuit at "already on 0.9.2" if you re-deploy
# under the same version, even if the file list / SHAs changed. Same idea
# applies to launcherVersion (drives the in-launcher upgrade banner).
# We fetch the currently-deployed manifest and refuse to deploy if the
# matching version field hasn't been bumped. Use -Force to override (e.g.
# for cosmetic-only changes where re-sync isn't needed).
if (($shouldRunPack -or $shouldRunLauncher) -and -not $DryRun -and -not $Force) {
if (-not $ManifestPublicUrl -or $ManifestPublicUrl -match 'CHANGE_ME') {
throw "deploy.config.ps1 is missing `$ManifestPublicUrl. Set it to the deployed manifest URL (e.g. https://example.com/pack/manifest.json)."
}
$deployed = $null
try {
$deployed = Invoke-RestMethod -Uri $ManifestPublicUrl -TimeoutSec 8
} catch {
Write-Host "Pre-flight: couldn't fetch deployed manifest at $ManifestPublicUrl -- version check skipped (probably first deploy)." -ForegroundColor DarkGray
}
if ($deployed) {
$errs = @()
# Pack version check applies only when the user is explicitly deploying
# pack content (Stage = All or Pack). A launcher-only deploy intentionally
# leaves pack content alone, so the pack version SHOULD stay constant.
$strictPackCheck = $Stage -in @('All','Pack')
if ($strictPackCheck) {
$lock = Get-Content (Join-Path $repoRoot 'pack\pack.lock.json') -Raw | ConvertFrom-Json
if ($deployed.version -eq $lock.version) {
$errs += "Pack version is unchanged ($($lock.version)). Clients cached at that version will SKIP the sync -- they won't pick up your pack changes. Bump 'version' in pack/pack.lock.json before deploying."
}
}
if ($shouldRunLauncher) {
$csprojPath = Join-Path $repoRoot 'launcher\ModpackLauncher.csproj'
[xml]$csproj = Get-Content $csprojPath
$localLauncherVersion = ($csproj.Project.PropertyGroup.Version | Where-Object { $_ }) | Select-Object -First 1
# Manifest stores 4-part FileVersion (e.g. "0.4.4.0"); csproj <Version> is 3-part ("0.4.4"). Compare normalised.
$deployedNorm = ($deployed.launcherVersion -replace '\.0+$','')
$localNorm = ($localLauncherVersion -replace '\.0+$','')
if ($deployedNorm -eq $localNorm -and $localNorm) {
$errs += "Launcher version is unchanged ($localLauncherVersion). Existing 0.4.x installs won't see an upgrade prompt -- bump <Version> in launcher/ModpackLauncher.csproj before deploying."
}
}
if ($errs.Count -gt 0) {
Write-Host ""
Write-Host "VERSION-BUMP CHECK FAILED:" -ForegroundColor Red
foreach ($e in $errs) { Write-Host " - $e" -ForegroundColor Red }
Write-Host ""
Write-Host "If you're re-deploying without any user-visible changes, pass -Force to skip this check." -ForegroundColor DarkGray
exit 1
}
}
}
# ─── Pre-flight: was the daemon already running? ───────────────────────────
# We don't auto-stop it (kicks active players to fix a problem that isn't
# actually broken -- the atomic swap is safe with the daemon running). But
# knowing the state up front lets us tailor the final "next steps" message
# so a deploy success doesn't silently leave you on the old code.
$daemonWasRunning = $false
if ($shouldRunServer) {
Write-Host "Pre-flight: checking daemon state on $ServerSshHost..." -ForegroundColor DarkGray
# Single-quoted PS string so PowerShell doesn't try to interpret the
# bash-side metacharacters. The remote shell sees the literal pgrep
# command; the trailing $ anchors so we don't match run.sh wrappers.
$remoteCmd = 'pgrep -f /brass-sigil-server$ 2>/dev/null'
$pgrepOut = & ssh -i $ServerSshKey -o ConnectTimeout=5 -o BatchMode=yes $ServerSshHost $remoteCmd 2>$null
$daemonWasRunning = -not [string]::IsNullOrWhiteSpace($pgrepOut)
if ($daemonWasRunning) {
Write-Host " Daemon is RUNNING. Atomic swap is safe -- but you'll need to" -ForegroundColor Yellow
Write-Host " stop+start it after deploy for the new code to take effect." -ForegroundColor Yellow
} else {
Write-Host " Daemon is stopped. New binary will run as soon as you start it." -ForegroundColor DarkGray
}
}
# ─── Helpers ───────────────────────────────────────────────────────────────
$stepNum = 0
function Step($desc, [scriptblock]$body) {
$script:stepNum++
$start = Get-Date
Write-Host ""
Write-Host ("[{0}] {1}" -f $script:stepNum, $desc) -ForegroundColor Cyan
if ($DryRun) {
Write-Host " (dry-run, skipping)" -ForegroundColor DarkGray
return
}
& $body
$elapsed = (Get-Date) - $start
Write-Host (" done in {0:N1}s" -f $elapsed.TotalSeconds) -ForegroundColor DarkGray
}
# ─── Stage 1: build launcher ───────────────────────────────────────────────
$launcherExe = Join-Path $repoRoot (Join-Path $LauncherPublishDir $LauncherExeName)
if ($shouldRunLauncher -and -not $SkipBuild) {
Step "Build launcher (dotnet publish launcher\)" {
Push-Location (Join-Path $repoRoot 'launcher')
try { dotnet publish -c Release -nologo | Out-Host }
finally { Pop-Location }
if (-not (Test-Path $launcherExe)) { throw "Launcher publish didn't produce $launcherExe" }
}
}
# ─── Stage 2: build server ─────────────────────────────────────────────────
$serverExe = Join-Path $repoRoot (Join-Path $ServerPublishDir $ServerExeName)
if ($shouldRunServer -and -not $SkipBuild) {
Step "Build server (dotnet publish server\ -r linux-x64)" {
Push-Location (Join-Path $repoRoot 'server')
try { dotnet publish -c Release -r linux-x64 --self-contained true -nologo | Out-Host }
finally { Pop-Location }
if (-not (Test-Path $serverExe)) { throw "Server publish didn't produce $serverExe" }
}
}
# ─── Stage 3: regenerate manifest ──────────────────────────────────────────
$manifestPath = Join-Path $here 'manifest.json'
if ($shouldRunPack) {
Step "Regenerate manifest (Build-Pack.ps1)" {
$args = @{ OutputPath = $manifestPath }
# Always embed launcher metadata if a local build exists -- otherwise a
# Stage=Pack deploy silently strips launcherVersion/launcherUrl from the
# published manifest, and old launchers stop seeing upgrade banners.
# Tied to Stage=Launcher only would mean every pack-only deploy regresses.
if (Test-Path $launcherExe) {
$args.LauncherExePath = $launcherExe
} else {
Write-Host " WARNING: launcher exe not found at $launcherExe; manifest will have no launcher metadata." -ForegroundColor Yellow
Write-Host " If you want launcherVersion/launcherUrl populated, run -Stage Launcher (or All) first." -ForegroundColor Yellow
}
& (Join-Path $here 'Build-Pack.ps1') @args | Out-Host
}
}
# ─── Stage 4: mirror pack overrides to share ──────────────────────────────
$overridesLocal = Join-Path $repoRoot 'pack\overrides'
$shareFiles = Join-Path $DeployShare 'files'
if ($shouldRunPack -and (Test-Path $overridesLocal)) {
Step "Mirror pack/overrides/ -> $shareFiles" {
# /MIR makes destination match source (deletes orphan files in $shareFiles).
# /XJ skips junctions, /R:1 /W:1 keeps retry behaviour sane on flaky shares.
robocopy $overridesLocal $shareFiles /MIR /XJ /R:1 /W:1 /NFL /NDL /NJH /NJS /NP | Out-Host
# Robocopy returns 0-7 for success-with-info; 8+ is real failure.
if ($LASTEXITCODE -ge 8) { throw "robocopy failed with exit $LASTEXITCODE" }
}
}
# ─── Stage 5: publish launcher exe (only when Stage includes Launcher) ─────
if ($shouldRunLauncher) {
Step "Copy launcher.exe -> $DeployShare" {
Copy-Item $launcherExe (Join-Path $DeployShare $LauncherDeployedAs) -Force
}
}
# ─── Stage 6: publish manifest (any time pack content changed) ─────────────
# Manifest is a pack artifact, not a launcher artifact -- a Pack-only deploy
# (e.g. tweak jar or pack version bump) still needs the new manifest to land
# on the share so clients see the updated SHA-1 list and pack version.
if ($shouldRunPack) {
Step "Copy manifest.json -> $DeployShare" {
Copy-Item $manifestPath (Join-Path $DeployShare 'manifest.json') -Force
}
}
# ─── Stage 6: scp + atomic swap server binary ──────────────────────────────
if ($shouldRunServer) {
Step "scp server binary -> $ServerSshHost`:$ServerBinaryRemote (atomic swap)" {
$remoteNew = "$ServerBinaryRemote.new"
& scp -i $ServerSshKey -o ConnectTimeout=15 $serverExe "$ServerSshHost`:$remoteNew"
if ($LASTEXITCODE -ne 0) { throw "scp failed with exit $LASTEXITCODE" }
$cmd = "chmod +x '$remoteNew' && mv '$remoteNew' '$ServerBinaryRemote' && md5sum '$ServerBinaryRemote'"
& ssh -i $ServerSshKey -o ConnectTimeout=10 $ServerSshHost $cmd
if ($LASTEXITCODE -ne 0) { throw "ssh swap failed with exit $LASTEXITCODE" }
}
Write-Host ""
if ($daemonWasRunning) {
Write-Host "Server binary swapped on disk, but the daemon was running before this" -ForegroundColor Yellow
Write-Host "deploy and is still on the OLD code in memory (Linux preserves the running" -ForegroundColor Yellow
Write-Host "inode through rename). Stop + start the daemon to pick up the new build." -ForegroundColor Yellow
} else {
Write-Host "Server binary swapped on disk. Daemon was stopped -- start it whenever" -ForegroundColor Green
Write-Host "you're ready and it'll run the new build." -ForegroundColor Green
}
}
# ─── Stage 7: verify deployed manifest ────────────────────────────────────
# Fetch what's actually live and assert the fields we expect. Catches:
# - share copy silently failed (clients see old manifest)
# - manifest was written without launcher metadata
# - URLs in the manifest still point at stale hostnames
# Pre-flight already version-checks the OLD manifest; this checks the NEW one.
if ($shouldRunPack -and -not $DryRun -and $ManifestPublicUrl) {
Step "Verify deployed manifest at $ManifestPublicUrl" {
Start-Sleep -Seconds 1 # give the share a moment to flush
$live = $null
try { $live = Invoke-RestMethod -Uri $ManifestPublicUrl -TimeoutSec 8 }
catch { throw "Failed to fetch deployed manifest: $($_.Exception.Message)" }
$expectedHost = ([uri]$ManifestPublicUrl).Host
$problems = @()
$lock = Get-Content (Join-Path $repoRoot 'pack\pack.lock.json') -Raw | ConvertFrom-Json
if ($live.version -ne $lock.version) {
$problems += "version mismatch: live=$($live.version), local=$($lock.version)"
}
if (-not $live.launcherVersion) { $problems += "launcherVersion missing (old launchers won't see upgrade banner)" }
if (-not $live.launcherUrl) { $problems += "launcherUrl missing" }
if ($live.launcherUrl -and ([uri]$live.launcherUrl).Host -ne $expectedHost) {
$problems += "launcherUrl host is $([uri]$live.launcherUrl).Host, expected $expectedHost"
}
# Sample a self-hosted file (skip CDN-hosted mods)
$local = $live.files | Where-Object { $_.url -notmatch 'modrinth|forgecdn|curseforge' } | Select-Object -First 1
if ($local -and ([uri]$local.url).Host -ne $expectedHost) {
$problems += "self-hosted file URL host is $([uri]$local.url).Host, expected $expectedHost (path: $($local.path))"
}
if ($problems.Count -gt 0) {
Write-Host ""
Write-Host "DEPLOYED MANIFEST FAILED VERIFICATION:" -ForegroundColor Red
foreach ($p in $problems) { Write-Host " - $p" -ForegroundColor Red }
throw "Deployed manifest is missing required fields. Fix and redeploy."
}
Write-Host (" version={0} launcherVersion={1} ok" -f $live.version, $live.launcherVersion) -ForegroundColor DarkGray
}
}
Write-Host ""
Write-Host "Deploy finished." -ForegroundColor Green