param( [Parameter(Mandatory)] [ValidateSet( 'linux-x64', 'linux-arm64', 'linux-musl-x64', 'linux-musl-arm64', 'osx-x64', 'osx-arm64', 'win-x64', 'win-arm64' )] [string]$RuntimeIdentifier, [Parameter(Mandatory)] [ValidateSet('agent', 'probe', 'fleet-enroll')] [string]$Component ) $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' # 版本取自 tag:写进工作区的 Directory.Build.props(仓库里的 只是占位,发版不用再提交 bump) $propsPath = Join-Path $backend 'Directory.Build.props' $props = Get-Content $propsPath -Raw Set-Content -LiteralPath $propsPath -Value ([regex]::Replace($props, '[^<]*', "$version")) -NoNewline if ($RuntimeIdentifier -in @('win-x64', 'win-arm64')) { $vcArchitecture = if ($RuntimeIdentifier -eq 'win-x64') { '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') } } } $project = switch ($Component) { 'agent' { 'agent/src/MoShiro.Agent' } 'probe' { 'probe/src/MoShiro.Probe' } 'fleet-enroll' { 'fleet/tools/fleet-enroll' } } $output = Join-Path $root ".release\publish\$RuntimeIdentifier\$Component" $buildRoot = Join-Path $root ".release\build\$RuntimeIdentifier-$Component" $null = New-Item -ItemType Directory -Force -Path $output # 构建信息:程序里显示的 tag、提交、构建时间 $env:MOSHIRO_TAG = $env:CI_COMMIT_TAG $env:MOSHIRO_COMMIT = $env:CI_COMMIT_SHA.Substring(0, 10) $env:MOSHIRO_BUILT_AT = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') Push-Location $backend try { & dotnet publish $project ` -m:1 ` -c Release ` -r $RuntimeIdentifier ` --self-contained true ` -p:PublishAot=true ` -p:UseAotAnywhere=true ` -p:StripSymbols=true ` -p:DebugType=none ` -p:DebugSymbols=false ` --artifacts-path $buildRoot ` -o $output if ($LASTEXITCODE -ne 0) { throw "NativeAOT publish failed for $project ($RuntimeIdentifier)." } } finally { Pop-Location } Write-Output "built $Component for $RuntimeIdentifier"