aisop/tools/Deploy-Windows.ps1

58 lines
3.7 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

param(
[string]$Target = 'Administrator@192.168.0.184',
[string]$InstallRoot = 'C:\sophelper',
[string]$ReleaseId = '',
[switch]$SkipPublish,
[switch]$SkipGpuCheck
)
# 通过 SSH/SCP 把当前 win-x64 发布包部署到远端 Windows 工位机。
# 约定同 Deploy-Linux.ps1:发布复用 artifacts/avalonia/<RID>/,每个版本放入 <InstallRoot>\releases\<ReleaseId>,
# configs/models/workspace 保留在 <InstallRoot> 下,通过目录联接跨版本复用。
$ErrorActionPreference = 'Stop'
if ($Target -notmatch '^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+$') { throw 'Target must be user@host' }
if ($InstallRoot -notmatch '^[A-Za-z]:\\[A-Za-z0-9_.\\-]+$') { throw 'InstallRoot must be an absolute Windows path without spaces' }
$repo = Split-Path $PSScriptRoot -Parent
$output = Join-Path $repo 'artifacts\avalonia\win-x64'
$remoteRootFwd = $InstallRoot.Replace('\', '/')
$sha = (& git -C $repo rev-parse --short HEAD | Out-String).Trim()
# 未跟踪的部署脚本本身不算“脏”,只看已跟踪文件是否有未提交改动。
$dirty = @(& git -C $repo status --porcelain --untracked-files=no).Count -gt 0
if (!$ReleaseId) {
$ReleaseId = 'avalonia-' + (Get-Date -Format 'yyyyMMdd-HHmmss') + '-' + $sha
if ($dirty) { $ReleaseId += '-dirty' }
}
Write-Host "Release: $ReleaseId (commit $sha$(if($dirty){' + 未提交修改'}))"
if (!$SkipPublish) {
& dotnet publish (Join-Path $repo 'src\SopHelper.Avalonia') -c Release -r win-x64 --self-contained true -p:PublishSingleFile=false -p:NativePlatform=Windows -o $output --nologo
if ($LASTEXITCODE) { throw 'Publish failed' }
}
# 本机先自检,避免把跑不起来的包传到工位机。
$exe = Join-Path $output 'SopHelper.Avalonia.exe'
if (!(Test-Path -LiteralPath $exe)) { throw "Missing published executable: $exe" }
$logDir = Join-Path (Split-Path $repo -Parent) 'scratch\test-results'
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
$runtimeCheck = Start-Process -FilePath $exe -ArgumentList '--check-runtime' -NoNewWindow -PassThru -Wait `
-RedirectStandardOutput (Join-Path $logDir 'deploy-windows-local-check-runtime.stdout.log') `
-RedirectStandardError (Join-Path $logDir 'deploy-windows-local-check-runtime.stderr.log')
if ($runtimeCheck.ExitCode) { throw '本机 --check-runtime 自检失败,详见 scratch/test-results/deploy-windows-local-check-runtime.*.log' }
Write-Host '本机 --check-runtime 通过。'
# 打包:仅打包当前程序运行所需文件,排除旧训练环境。
$packageItems = Get-ChildItem -LiteralPath $output -Force | Where-Object { $_.Name -ne 'python' } | Select-Object -ExpandProperty Name
if (!$packageItems) { throw "Nothing to package in $output" }
$archive = Join-Path $repo ('artifacts\' + $ReleaseId + '.tar.gz')
if (Test-Path -LiteralPath $archive) { Remove-Item -LiteralPath $archive -Force }
& tar -czf $archive -C $output $packageItems
if ($LASTEXITCODE) { throw 'Packaging failed' }
Write-Host ('打包完成: ' + $archive + ' (' + [math]::Round((Get-Item $archive).Length/1MB,1) + ' MB)')
& ssh -o BatchMode=yes $Target "New-Item -Force -ItemType Directory $InstallRoot, $InstallRoot\releases | Out-Null"
if ($LASTEXITCODE) { throw 'SSH failed' }
& scp -o BatchMode=yes $archive (Join-Path $PSScriptRoot 'windows\install-release.ps1') "${Target}:$remoteRootFwd/"
if ($LASTEXITCODE) { throw 'Transfer failed' }
& ssh -o BatchMode=yes $Target "powershell -NoProfile -ExecutionPolicy Bypass -File $InstallRoot\install-release.ps1 -ReleaseId $ReleaseId -InstallRoot $InstallRoot -SkipGpuCheck $([int][bool]$SkipGpuCheck)"
if ($LASTEXITCODE) { throw "安装或自检失败,详见 $InstallRoot\releases\$ReleaseId\workspace\logs" }
Write-Host "已部署 $ReleaseId 到 $Target;开始菜单和桌面均有 SopHelper 快捷方式。"