Harden hosted deploy script

This commit is contained in:
2026-07-12 12:17:35 -05:00
parent 6559295a64
commit 2fce849666
7 changed files with 32 additions and 13 deletions
+22 -7
View File
@@ -1,16 +1,21 @@
param(
[string]$HostName = "christunscripted",
[string]$RemoteRoot = "/sites/libre-study",
[string]$RemoteBibleDataRoot = "/sites/libre-bible-data/packages/json",
[string]$RemoteRoot = "/home/jbird/libre-study",
[string]$RemoteBibleDataRoot = "/home/jbird/libre-bible-data/packages/json",
[int]$Port = 9174
)
$ErrorActionPreference = "Stop"
function ConvertTo-WslPath([string]$Path) {
$resolved = (Resolve-Path $Path).Path.Replace("\", "/")
return (wsl wslpath -a "$resolved").Trim()
}
$repoRoot = Split-Path -Parent $PSScriptRoot
$dataRepo = Resolve-Path (Join-Path $repoRoot "..\libre-bible-data")
$repoRootWsl = (wsl wslpath -a "$repoRoot").Trim()
$dataRepoWsl = (wsl wslpath -a "$dataRepo").Trim()
$repoRootWsl = ConvertTo-WslPath $repoRoot
$dataRepoWsl = ConvertTo-WslPath $dataRepo
$commit = (git -C $repoRoot rev-parse HEAD).Trim()
$deployedAt = (Get-Date).ToUniversalTime().ToString("o")
@@ -33,12 +38,20 @@ $deployInfo = @{
deployedAt = $deployedAt
source = "libre-study"
} | ConvertTo-Json -Compress
$deployInfoPath = Join-Path $env:TEMP "libre-study-deploy-info.json"
Set-Content -Path $deployInfoPath -Value $deployInfo -NoNewline
$deployInfoPathWsl = ConvertTo-WslPath $deployInfoPath
$escapedDeployInfo = $deployInfo.Replace("'", "'\''")
wsl ssh $HostName "mkdir -p '$RemoteRoot/app' '$RemoteRoot/server' '$RemoteRoot/state' '$RemoteBibleDataRoot' && printf '%s' '$escapedDeployInfo' > '$RemoteRoot/deploy-info.json'"
wsl ssh $HostName "mkdir -p '$RemoteRoot/app' '$RemoteRoot/server' '$RemoteRoot/state' '$RemoteBibleDataRoot'"
if ($LASTEXITCODE -ne 0) { throw "Remote directory preparation failed." }
wsl rsync -az "$deployInfoPathWsl" "${HostName}:$RemoteRoot/deploy-info.json"
if ($LASTEXITCODE -ne 0) { throw "Deploy info rsync failed." }
wsl rsync -az --delete "$repoRootWsl/dist-hosted/" "${HostName}:$RemoteRoot/app/"
if ($LASTEXITCODE -ne 0) { throw "App rsync failed." }
wsl rsync -az --delete "$repoRootWsl/server/" "${HostName}:$RemoteRoot/server/"
if ($LASTEXITCODE -ne 0) { throw "Server rsync failed." }
wsl rsync -az --delete "$dataRepoWsl/packages/json/" "${HostName}:$RemoteBibleDataRoot/"
if ($LASTEXITCODE -ne 0) { throw "LibreBible rsync failed." }
$pm2Command = @"
cd '$RemoteRoot' &&
@@ -51,4 +64,6 @@ pm2 save
"@
wsl ssh $HostName $pm2Command
Write-Output "Deployed Libre Study web commit $commit to $HostName:$RemoteRoot on port $Port."
if ($LASTEXITCODE -ne 0) { throw "PM2 start/restart failed." }
Remove-Item $deployInfoPath -Force -ErrorAction SilentlyContinue
Write-Output "Deployed Libre Study web commit $commit to ${HostName}:$RemoteRoot on port $Port."