am-freescan/deploy/windows11/update.ps1

89 lines
5.2 KiB
PowerShell
Raw Normal View History

#requires -Version 7.2
[CmdletBinding()]
param(
[switch]$ConfirmStopped,
[switch]$CheckOnly,
[string]$RemoteUrl = 'https://p20001.svc.cmvr-iot.icu/SmartBench/am-freescan.git'
)
$ErrorActionPreference='Stop'
. (Join-Path $PSScriptRoot 'deployment-common.ps1')
$cfg=Read-DeployConfig
$lock=Get-DeployLock
$maintenance=$false
$activation=$false
$backendStopped=$false
$token=[guid]::NewGuid().ToString('N')
$id='release-'+(Get-Date -Format 'yyyyMMdd-HHmmss')+'-'+$token.Substring(0,8)
$stage=Join-Path $deployRepo ".local/release-builds/$id"
try {
Assert-CleanDeployTree
foreach($program in @('git','java','mvn','node','pnpm','python','docker','pwsh')) { Get-Command $program -ErrorAction Stop | Out-Null }
if ((& java -version 2>&1 | Out-String) -notmatch 'version "17\.') { throw 'JDK17 is required' }
if ((& mvn -version 2>&1 | Out-String) -notmatch 'Apache Maven 3\.9\.') { throw 'Maven3.9.x is required' }
if ($RemoteUrl -ne 'https://p20001.svc.cmvr-iot.icu/SmartBench/am-freescan.git') { throw 'Unexpected Git repository; review the script before changing its destination' }
Run-DeployCompose $cfg @('config','--quiet')
Run-DeployCommand 'python' @($deployPython,'--config',$deployRuntime,'--check-instance')
if ($CheckOnly) { Write-Output 'Preflight passed. No pull, build, restart or data import was performed.'; return }
if (-not $ConfirmStopped) { throw 'Confirm acquisition and physical motion have stopped, then pass -ConfirmStopped' }
Assert-DeployIdle
$env:GIT_TERMINAL_PROMPT='0'
Run-DeployCommand 'git' @('-C',$deployRepo,'fetch','--no-tags',$RemoteUrl,'main')
$commit=(& git -C $deployRepo rev-parse FETCH_HEAD).Trim()
if ($LASTEXITCODE -ne 0 -or $commit -notmatch '^[0-9a-f]{40}$') { throw 'Unable to identify the fetched commit' }
Run-DeployCommand 'git' @('-C',$deployRepo,'merge','--ff-only',$commit)
Assert-CleanDeployTree
New-Item -ItemType Directory -Path (Join-Path $stage 'web') -Force | Out-Null
Write-Output "Building $commit. The current runtime package is preserved until both builds succeed."
Push-Location (Join-Path $deployRepo 'am-backend')
try { Run-DeployCommand 'mvn' @('-pl','yudao-server','-am','package','-DskipTests','-DskipITs') } finally { Pop-Location }
Copy-Item -LiteralPath (Join-Path $deployRepo 'am-backend/yudao-server/target/yudao-server.jar') -Destination (Join-Path $stage 'yudao-server.jar')
Push-Location (Join-Path $deployRepo 'am-web')
try {
Run-DeployCommand 'pnpm' @('install','--frozen-lockfile')
Run-DeployCommand 'pnpm' @('build:standalone','--outDir',(Join-Path $stage 'web'))
} finally { Pop-Location }
if (-not (Test-Path -LiteralPath (Join-Path $stage 'web/index.html'))) { throw 'Frontend build has no index.html' }
Assert-CleanDeployTree
Assert-DeployIdle
New-DeployMaintenance $token 'code-update';$maintenance=$true
Run-DeployCompose $cfg @('stop','web')
Run-DeployCommand 'pwsh' @('-NoProfile','-File',$deployManager,'-Action','Stop','-ConfirmStopped')
$backendStopped=$true
Assert-DeployIdle
# Preserve only the previous program and frontend. No business data copy/import.
$previous=Join-Path $deployRepo ".local/release-history/$id"
New-Item -ItemType Directory -Path $previous -Force | Out-Null
$runtimeJar=Join-Path $deployPrivate 'backend/yudao-server.jar'
Copy-Item -LiteralPath $runtimeJar -Destination (Join-Path $previous 'yudao-server.jar')
foreach($name in @('deployed-commit.txt','backend-sha256.txt','deployment.json')) {
$source=Join-Path $deployPrivate $name
if(Test-Path -LiteralPath $source){Copy-Item -LiteralPath $source -Destination (Join-Path $previous $name)}
}
$web=Join-Path $cfg.dataRoot 'web'
$activation=$true
if(Test-Path -LiteralPath $web){Move-Item -LiteralPath $web -Destination (Join-Path $cfg.dataRoot "web-before-$id")}
Copy-Item -LiteralPath (Join-Path $stage 'yudao-server.jar') -Destination $runtimeJar -Force
Copy-Item -LiteralPath (Join-Path $stage 'web') -Destination $web -Recurse
Run-DeployCommand 'python' @($deployPython,'--config',$deployRuntime,'--upgrade')
Run-DeployCommand 'python' @($deployPython,'--config',$deployRuntime,'--verify')
Start-DeployInstance $token
Save-DeployRecord $commit 'code-update' $id
Complete-DeployMaintenance $token;$maintenance=$false
Write-Output "Deployment succeeded: $commit. Business data and private device/QUIC configuration were preserved."
}
catch {
if($maintenance){
if(-not $activation){
try {
if($backendStopped){Start-DeployInstance $token}else{Run-DeployCompose $cfg @('up','-d','web')}
Complete-DeployMaintenance $token;$maintenance=$false
} catch { Write-Warning 'Old application could not be restarted; maintenance marker retained.' }
} else {
try { Run-DeployCompose $cfg @('stop','web') } catch { Write-Warning 'Could not stop the frontend; inspect its container.' }
Write-Warning 'Activation failed. Maintenance remains active; inspect logs and the saved previous program. Do not rerun Init/Bootstrap or reset the database.'
}
}
throw
}
finally { $lock.Dispose() }