inogen/deployment/build-package.ps1

169 lines
9.1 KiB
PowerShell

[CmdletBinding()]
param(
[string]$BackendSource = "D:\Work\Inogen_new\Inogen backend",
[string]$OutputRoot = (Join-Path $PSScriptRoot "..\artifacts"),
[string]$SigningPrivateKey = $env:INOGEN_RELEASE_SIGNING_KEY,
[string]$ApiBaseUrl,
[switch]$SkipNpmInstall,
[switch]$IncludeInternalTransfer
)
$ErrorActionPreference = "Stop"
$frontendSource = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
$backendSourcePath = (Resolve-Path -LiteralPath $BackendSource).Path
$outputRootPath = [IO.Path]::GetFullPath($OutputRoot)
$releaseName = "Inogen-{0}" -f (Get-Date -Format "yyyyMMdd-HHmmss")
if ($IncludeInternalTransfer) { $releaseName += "-Internal" }
$releasePath = Join-Path $outputRootPath $releaseName
$backendOutput = Join-Path $releasePath "backend"
$frontendOutput = Join-Path $releasePath "frontend"
$transferOutput = Join-Path $releasePath "tools\Inogen.DataTransfer"
if ([string]::IsNullOrWhiteSpace($SigningPrivateKey)) {
throw "Release signing key is not configured. Generate one once with: dotnet run --project '<backend>\tools\Inogen.ReleaseProtector' -- generate-key '<secure-directory>\inogen-release-private.pem'. Then set INOGEN_RELEASE_SIGNING_KEY to that absolute file path. Keep the private key outside the package and source tree."
}
if (-not [IO.Path]::IsPathRooted($SigningPrivateKey) -or
-not (Test-Path -LiteralPath $SigningPrivateKey -PathType Leaf)) {
throw "SigningPrivateKey must be the absolute path of an existing private PEM file."
}
$signingKeyPath = (Resolve-Path -LiteralPath $SigningPrivateKey).Path
if ([string]::IsNullOrWhiteSpace($ApiBaseUrl)) {
throw "Set -ApiBaseUrl to the HTTPS API address ending in /api/v1 for this deployment."
}
$apiUri = $null
if (-not [Uri]::TryCreate($ApiBaseUrl.Trim(), [UriKind]::Absolute, [ref]$apiUri) -or
$apiUri.Scheme -ne [Uri]::UriSchemeHttps -or
$apiUri.AbsolutePath.TrimEnd('/') -ne '/api/v1' -or
-not [string]::IsNullOrEmpty($apiUri.Query) -or
-not [string]::IsNullOrEmpty($apiUri.Fragment) -or
-not [string]::IsNullOrEmpty($apiUri.UserInfo)) {
throw "ApiBaseUrl must be an absolute HTTPS URL ending in /api/v1 without query, fragment, or credentials."
}
New-Item -ItemType Directory -Force -Path $backendOutput, $frontendOutput | Out-Null
if ($IncludeInternalTransfer) { New-Item -ItemType Directory -Force -Path $transferOutput | Out-Null }
function Assert-NativeCommandSucceeded([string]$operation) {
if ($LASTEXITCODE -ne 0) { throw "$operation failed with exit code $LASTEXITCODE." }
}
function Protect-Assemblies([string]$targetPath, [string[]]$moduleNames) {
$obfuscatedPath = Join-Path $targetPath "obfuscated"
New-Item -ItemType Directory -Force -Path $obfuscatedPath | Out-Null
$moduleElements = $moduleNames | ForEach-Object {
$modulePath = [System.Security.SecurityElement]::Escape((Join-Path $targetPath $_))
$rules = @(" <SkipMethod type=`"*`" name=`".ctor`" />")
if ($_ -eq "Inogen.Infrastructure.dll") {
$rules += " <SkipNamespace name=`"Inogen.Infrastructure.Persistence`" />"
$rules += " <SkipNamespace name=`"Inogen.Infrastructure.Persistence.Migrations`" />"
$rules += " <SkipNamespace name=`"Inogen.Infrastructure.Identity`" />"
}
" <Module file=`"$modulePath`">$([Environment]::NewLine)$($rules -join [Environment]::NewLine)$([Environment]::NewLine) </Module>"
}
$configPath = Join-Path $targetPath "obfuscar.xml"
$config = @(
"<?xml version=`"1.0`" encoding=`"utf-8`"?>",
"<Obfuscator>",
" <Var name=`"InPath`" value=`"$([System.Security.SecurityElement]::Escape($targetPath))`" />",
" <Var name=`"OutPath`" value=`"$([System.Security.SecurityElement]::Escape($obfuscatedPath))`" />",
" <Var name=`"KeepPublicApi`" value=`"true`" />",
" <Var name=`"HidePrivateApi`" value=`"true`" />",
" <Var name=`"HideStrings`" value=`"true`" />",
" <Var name=`"ReuseNames`" value=`"false`" />",
" <Var name=`"UseUnicodeNames`" value=`"true`" />",
" <Var name=`"SkipGenerated`" value=`"true`" />",
($moduleElements -join [Environment]::NewLine),
"</Obfuscator>"
) -join [Environment]::NewLine
[IO.File]::WriteAllText($configPath, $config, [Text.UTF8Encoding]::new($false))
Push-Location $backendSourcePath
try {
dotnet tool run obfuscar.console -- $configPath
Assert-NativeCommandSucceeded "Assembly obfuscation"
}
finally { Pop-Location }
foreach ($module in $moduleNames) {
Copy-Item -LiteralPath (Join-Path $obfuscatedPath $module) -Destination (Join-Path $targetPath $module) -Force
}
Remove-Item -LiteralPath $obfuscatedPath -Recurse -Force
Remove-Item -LiteralPath $configPath -Force
}
function Write-SignedIntegrityManifest([string]$targetPath, [string[]]$fileNames) {
$protectorProject = Join-Path $backendSourcePath "tools\Inogen.ReleaseProtector\Inogen.ReleaseProtector.csproj"
dotnet run --project $protectorProject --configuration Release -- sign $signingKeyPath $targetPath @fileNames
Assert-NativeCommandSucceeded "Release integrity signing"
}
Push-Location $frontendSource
try {
if (-not $SkipNpmInstall) {
npm ci
Assert-NativeCommandSucceeded "npm ci"
}
npm run typecheck
Assert-NativeCommandSucceeded "Frontend typecheck"
npm run build
Assert-NativeCommandSucceeded "Frontend build"
Copy-Item -Path (Join-Path $frontendSource "build\client\*") -Destination $frontendOutput -Recurse -Force
$runtimeConfigPath = Join-Path $frontendOutput "config\runtime-config.js"
$apiBaseUrlJson = ConvertTo-Json -InputObject $apiUri.AbsoluteUri.TrimEnd('/') -Compress
[IO.File]::WriteAllText($runtimeConfigPath,
"window.INOGEN_CONFIG = { apiBaseUrl: $apiBaseUrlJson };`n",
[Text.UTF8Encoding]::new($false))
}
finally { Pop-Location }
dotnet publish (Join-Path $backendSourcePath "src\Inogen.Api\Inogen.Api.csproj") `
--configuration Release --output $backendOutput --no-self-contained
Assert-NativeCommandSucceeded "Backend publish"
$developmentSettings = Join-Path $backendOutput "appsettings.Development.json"
if (Test-Path -LiteralPath $developmentSettings) { Remove-Item -LiteralPath $developmentSettings -Force }
if ($IncludeInternalTransfer) {
dotnet publish (Join-Path $backendSourcePath "tools\Inogen.DataTransfer\Inogen.DataTransfer.csproj") `
--configuration Release --output $transferOutput --no-self-contained
Assert-NativeCommandSucceeded "Internal transfer tool publish"
}
Push-Location $backendSourcePath
try {
dotnet tool restore
Assert-NativeCommandSucceeded "Obfuscator restore"
}
finally { Pop-Location }
$backendModules = @()
$integrityModules = @("Inogen.Api.dll", "Inogen.Application.dll", "Inogen.Domain.dll", "Inogen.Infrastructure.dll")
if ($backendModules.Count -gt 0) { Protect-Assemblies $backendOutput $backendModules }
$publishedPaths = @($backendOutput)
if ($IncludeInternalTransfer) { $publishedPaths += $transferOutput }
Get-ChildItem -LiteralPath $publishedPaths -Filter "*.pdb" -File | Remove-Item -Force
Get-ChildItem -LiteralPath $publishedPaths -Filter "Mapping.txt" -File | Remove-Item -Force
Write-SignedIntegrityManifest $backendOutput $integrityModules
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "appsettings.Production.template.json") `
-Destination (Join-Path $backendOutput "appsettings.Production.json") -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "INSTALL.pdf") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "initialize-admin.ps1") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "migrate-target.ps1") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "set-iis-runtime-connection.ps1") -Destination $releasePath -Force
if ($IncludeInternalTransfer) {
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "INSTALL-INTERNAL.pdf") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "select-internal-transfer.ps1") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "prepare-transfer-connections.ps1") -Destination $releasePath -Force
}
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "configure-windows-tls.ps1") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "verify-public-security.ps1") -Destination $releasePath -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "verify-runtime-sql-permissions.ps1") -Destination $releasePath -Force
$assetPath = Join-Path $backendOutput "data\deployment\assets"
New-Item -ItemType Directory -Force -Path $assetPath, (Join-Path $backendOutput "data\files"), `
(Join-Path $backendOutput "data\activation") | Out-Null
foreach ($asset in @("product-logo.svg", "abniro.png", "besparan.png", "khwarazmi.png", "faravash1.png", "faravash2.png", "kimia.png")) {
Copy-Item -LiteralPath (Join-Path $frontendSource "public\$asset") -Destination $assetPath -Force
}
$zipPath = "$releasePath.zip"
Compress-Archive -LiteralPath $releasePath -DestinationPath $zipPath -CompressionLevel Optimal
Write-Host "Release folder: $releasePath"
Write-Host "Release archive: $zipPath"