Files
MoShiro/ci/release-windows.ps1
T

119 lines
4.7 KiB
PowerShell

param(
[Parameter(Mandatory)]
[ValidateSet('amd64', 'arm64')]
[string]$Architecture
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
if (-not $env:CI_COMMIT_TAG -or $env:CI_COMMIT_TAG -notmatch '^v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$') {
throw 'Release must run from a vMAJOR.MINOR.PATCH tag.'
}
$version = $Matches[1]
$root = Split-Path -Parent $PSScriptRoot
$backend = Join-Path $root 'backend'
$projectVersion = [regex]::Match((Get-Content (Join-Path $backend 'Directory.Build.props') -Raw), '<Version>([^<]+)</Version>').Groups[1].Value
if ($version -ne $projectVersion) {
throw "Tag version $version does not match project version $projectVersion."
}
if (-not $env:GITEA_TOKEN) {
throw 'GITEA_TOKEN is required.'
}
$rid = if ($Architecture -eq 'amd64') { 'win-x64' } else { 'win-arm64' }
$vcArchitecture = if ($Architecture -eq 'amd64') { 'x64' } else { 'arm64' }
$vsWhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'
$vsPath = & $vsWhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
if (-not $vsPath) {
throw 'Visual Studio C++ Build Tools were not found.'
}
$devCmd = Join-Path $vsPath 'Common7\Tools\VsDevCmd.bat'
$environmentLines = & cmd.exe /d /s /c "call `"$devCmd`" -no_logo -arch=$vcArchitecture -host_arch=x64 && set"
foreach ($line in $environmentLines) {
$separator = $line.IndexOf('=')
if ($separator -gt 0) {
[Environment]::SetEnvironmentVariable($line.Substring(0, $separator), $line.Substring($separator + 1), 'Process')
}
}
$buildRoot = Join-Path $root ".release\build\windows-$Architecture-$PID"
$publishRoot = Join-Path $root ".release\windows-$Architecture-$PID"
$packageRoot = Join-Path $root '.release\packages'
$null = New-Item -ItemType Directory -Force -Path $publishRoot, $packageRoot
function Publish-Aot([string]$Project, [string]$Output) {
$arguments = @(
'publish', $Project,
'-c', 'Release',
'-r', $rid,
'--self-contained', 'true',
'-p:PublishAot=true',
'-p:StripSymbols=true',
'-p:DebugType=none',
'--artifacts-path', $buildRoot,
'-o', $Output
)
Push-Location $backend
try {
& dotnet @arguments
if ($LASTEXITCODE -ne 0) {
throw "NativeAOT publish failed for $Project ($rid)."
}
}
finally {
Pop-Location
}
}
$agentOut = Join-Path $publishRoot 'agent'
$probeOut = Join-Path $publishRoot 'probe'
$enrollOut = Join-Path $publishRoot 'fleet-enroll'
$null = New-Item -ItemType Directory -Force -Path $agentOut, $probeOut, $enrollOut
Publish-Aot 'agent/src/Monitor.Agent' $agentOut
Publish-Aot 'probe/src/Monitor.Probe' $probeOut
Publish-Aot 'fleet/tools/fleet-enroll' $enrollOut
$giteaBase = if ($env:GITEA_BASE) { $env:GITEA_BASE.TrimEnd('/') } else { 'https://git.oeo.one' }
$netrc = Join-Path $env:TEMP ("monitor-gitea-$([guid]::NewGuid().ToString('N')).netrc")
$giteaHost = ([Uri]$giteaBase).Host
Set-Content -LiteralPath $netrc -Value "machine $giteaHost login justme password $($env:GITEA_TOKEN)" -Encoding ascii -NoNewline
function Publish-Package([string]$Role, [string]$BinaryDirectory) {
$stage = Join-Path $publishRoot "stage-$Role"
$null = New-Item -ItemType Directory -Force -Path $stage
Copy-Item -LiteralPath (Join-Path $BinaryDirectory "monitor-$Role.exe") -Destination $stage
Copy-Item -LiteralPath (Join-Path $root 'LICENSE') -Destination $stage
Copy-Item -LiteralPath (Join-Path $root "backend\$Role\README.md") -Destination $stage
if ($Role -eq 'agent') {
Copy-Item -LiteralPath (Join-Path $enrollOut 'fleet-enroll.exe') -Destination $stage
}
$archiveName = "monitor-$Role-v$version-windows-$Architecture.zip"
$archive = Join-Path $packageRoot $archiveName
Compress-Archive -Path (Join-Path $stage '*') -DestinationPath $archive -CompressionLevel Optimal -Force
$hash = (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash.ToLowerInvariant()
$checksum = "$hash $archiveName`n"
$checksumPath = "$archive.sha256"
[IO.File]::WriteAllText($checksumPath, $checksum, [Text.UTF8Encoding]::new($false))
foreach ($file in @($archive, $checksumPath)) {
$name = Split-Path -Leaf $file
$url = "$giteaBase/api/packages/justme/generic/monitor-node/$version/$name"
& curl.exe --fail-with-body --retry 3 --netrc-file $netrc --upload-file $file $url
if ($LASTEXITCODE -ne 0) {
throw "Gitea package upload failed for $name."
}
Write-Output "uploaded Gitea package $name"
}
}
try {
Publish-Package 'agent' $agentOut
Publish-Package 'probe' $probeOut
}
finally {
Remove-Item -LiteralPath $netrc -Force -ErrorAction SilentlyContinue
}