| 1 | #requires -Version 7.0 |
| 2 | # CI orchestration only. Builds the checked-out version and never publishes it. |
| 3 | [CmdletBinding()] |
| 4 | param([switch]$Release) |
| 5 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 6 | if (-not $IsWindows) { throw 'This CI entry point requires a disposable Windows runner.' } |
| 7 | if ($env:CAGE_BUILD_REF -notmatch '^[0-9A-Fa-f]{40}$') { throw 'CAGE_BUILD_REF must be a complete commit SHA.' } |
| 8 | $repository = Split-Path $PSScriptRoot -Parent |
| 9 | Push-Location $repository |
| 10 | $pfx = $null |
| 11 | $certificate = $null |
| 12 | $certificates = @() |
| 13 | $qemuSource = $null |
| 14 | $savedPath = $env:PATH |
| 15 | $savedCertificate = $env:CAGE_WINDOWS_CERT_SHA1 |
| 16 | $savedStorageVolume = $env:CAGE_TEST_STORAGE_VOLUME |
| 17 | $storageTestRoot = $null |
| 18 | try { |
| 19 | $actual = (& git rev-parse HEAD).Trim() |
| 20 | if ($LASTEXITCODE -ne 0 -or $actual -ne $env:CAGE_BUILD_REF) { throw 'Checkout differs from the requested commit.' } |
| 21 | if ($Release) { |
| 22 | if ($env:GITHUB_EVENT_NAME -ne 'workflow_dispatch') { throw 'Release candidates require an explicitly dispatched release workflow.' } |
| 23 | if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -eq 'esigner') { |
| 24 | foreach ($name in @('CAGE_ESIGNER_USERNAME', 'CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_CREDENTIAL_ID', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_ESIGNER_TOTP_SECRET')) { |
| 25 | if (-not [Environment]::GetEnvironmentVariable($name)) { throw "Missing cage-release setting: $name" } |
| 26 | } |
| 27 | Assert-WindowsSigningConfiguration |
| 28 | Invoke-BuildTool 'node' @('scripts/esigner.mjs', 'install') |
| 29 | } elseif ($env:CAGE_WINDOWS_SIGNING_PROVIDER -and $env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'certificate-store') { |
| 30 | throw 'Unknown Windows signing provider.' |
| 31 | } elseif (-not $env:CAGE_WINDOWS_PFX_BASE64 -or -not $env:CAGE_WINDOWS_PFX_PASSWORD) { |
| 32 | throw 'Configure CAGE_WINDOWS_PFX_BASE64 and CAGE_WINDOWS_PFX_PASSWORD in the cage-release environment.' |
| 33 | } |
| 34 | if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'esigner') { |
| 35 | $pfx = Join-Path $env:RUNNER_TEMP ('cage-signing-' + [guid]::NewGuid().ToString('N') + '.pfx') |
| 36 | [IO.File]::WriteAllBytes($pfx, [Convert]::FromBase64String($env:CAGE_WINDOWS_PFX_BASE64)) |
| 37 | $password = ConvertTo-SecureString $env:CAGE_WINDOWS_PFX_PASSWORD -AsPlainText -Force |
| 38 | $certificates = @(Import-PfxCertificate -FilePath $pfx -CertStoreLocation Cert:\CurrentUser\My -Password $password) |
| 39 | Remove-Item -LiteralPath $pfx -Force |
| 40 | $signers = @($certificates | Where-Object { |
| 41 | $_.HasPrivateKey -and @($_.EnhancedKeyUsageList | Where-Object { $_.ObjectId -eq '1.3.6.1.5.5.7.3.3' }).Count -gt 0 |
| 42 | }) |
| 43 | if ($signers.Count -ne 1) { throw 'PFX must contain exactly one code-signing certificate with its private key.' } |
| 44 | $certificate = $signers[0] |
| 45 | $env:CAGE_WINDOWS_CERT_SHA1 = $certificate.Thumbprint |
| 46 | Remove-Item Env:CAGE_WINDOWS_PFX_BASE64, Env:CAGE_WINDOWS_PFX_PASSWORD |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | & (Join-Path $PSScriptRoot 'install-nsis.ps1') -Destination $env:RUNNER_TEMP |
| 51 | $env:PATH = (Join-Path $env:RUNNER_TEMP 'nsis-3.13') + [IO.Path]::PathSeparator + $env:PATH |
| 52 | Invoke-BuildTool 'node' @('scripts/check-version.mjs') |
| 53 | Invoke-BuildTool 'node' @('--test', 'internal/cage/browser-clipboard.test.mjs', 'scripts/windows-native-bridge.test.mjs') |
| 54 | Invoke-BuildTool (Get-BuildCommand @('pwsh.exe', 'pwsh')) @('-NoProfile', '-NonInteractive', '-File', 'scripts/windows-workbench-smoke.test.ps1') |
| 55 | $windowsPowerShell = Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell/v1.0/powershell.exe' |
| 56 | # NSIS is x86 and resolves $SYSDIR to SysWOW64. Match its real helper host. |
| 57 | $installationPowerShell = Join-Path $env:SystemRoot 'SysWOW64/WindowsPowerShell/v1.0/powershell.exe' |
| 58 | & (Join-Path $PSScriptRoot 'test-windows-installation-files.ps1') -HelperShell $installationPowerShell -InstallerSmoke |
| 59 | if ($Release) { |
| 60 | & (Join-Path $PSScriptRoot 'prepare-qemu-release.ps1') |
| 61 | $qemuSource = Join-Path $repository '_output/windows-qemu-sources/full-source.tar.gz' |
| 62 | } |
| 63 | & (Join-Path $PSScriptRoot 'build-windows.ps1') -Release:$Release |
| 64 | Invoke-BuildTool $windowsPowerShell @('-NoProfile', '-NonInteractive', '-File', 'scripts/guest-os/windows-host-enrollment.tests.ps1') |
| 65 | foreach ($test in @('windows-iso-setup.tests.ps1', 'windows-host-image.tests.ps1', 'new-windows-test-vm.tests.ps1')) { |
| 66 | Invoke-BuildTool $windowsPowerShell @('-NoProfile', '-NonInteractive', '-File', "scripts/guest-os/$test") |
| 67 | } |
| 68 | Invoke-BuildTool 'go' @('test', './internal/guestos', '-run', '^TestWindows(HostEnrollment|UserToolACL)', '-count=1') |
| 69 | if (-not $Release) { |
| 70 | $windowsPowerShell = Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell/v1.0/powershell.exe' |
| 71 | foreach ($test in @('install-guest-powershell.tests.ps1', 'windows-evaluation.tests.ps1')) { |
| 72 | Invoke-BuildTool $windowsPowerShell @('-NoProfile', '-NonInteractive', '-File', "scripts/guest-os/$test") |
| 73 | } |
| 74 | Invoke-BuildTool $windowsPowerShell @('-NoProfile', '-NonInteractive', '-File', 'scripts/guest-os/windows-powershell-media.smoke.ps1', |
| 75 | '-GuestToolsDirectory', "$repository/_output/windows-amd64/app/resources/guest-os") |
| 76 | } |
| 77 | Invoke-BuildTool 'dotnet' @('run', '--project', 'desktop/windows.tests/Cage.Windows.Tests.csproj') |
| 78 | # RUNNER_TEMP and the checkout share D: on hosted runners. Use a |
| 79 | # unique system-drive directory, outside user data, for the other side. |
| 80 | if (-not $env:CAGE_TEST_STORAGE_VOLUME) { |
| 81 | $systemDrive = [IO.Path]::GetPathRoot($env:SystemRoot) |
| 82 | $testDrive = [IO.Path]::GetPathRoot((Get-WindowsWorkbenchTestBase)) |
| 83 | if ($systemDrive -ne $testDrive) { |
| 84 | $storageTestRoot = (New-Item -ItemType Directory -Path (Join-Path $systemDrive ('cage-storage-volume-' + [guid]::NewGuid().ToString('N')))).FullName |
| 85 | $env:CAGE_TEST_STORAGE_VOLUME = $storageTestRoot |
| 86 | Write-Host "Storage migration tests use separate drives: $testDrive -> $systemDrive" |
| 87 | } else { |
| 88 | Write-Warning 'No second test volume is configured; the cross-volume storage test will be skipped.' |
| 89 | } |
| 90 | } |
| 91 | Invoke-InIsolatedTestTemp { |
| 92 | Invoke-BuildTool 'go' @('test', './internal/workbench', './internal/appupdate', './internal/host', './internal/managedproxy', './internal/env', './internal/paths', './internal/mounts') |
| 93 | Invoke-BuildTool 'go' @('test', './internal/cage', '-run', 'Test(Storage|Gate|ProxyRevision|ConfigChange|JobStop|CONNECT|SOCKS|DNS|RejectHostLAN|Credentials|ProxyHostname|SafetyReport|Renew)') |
| 94 | Invoke-BuildTool 'go' @('test', './internal/vm', '-run', 'Test(NetworkConfined|WindowsQEMUIdentity)') |
| 95 | Invoke-BuildTool 'go' @('test', 'github.com/lima-vm/lima/v2/pkg/driver/qemu', '-run', 'Test(WindowsFirmwareArgument|WindowsPackagedFirmwareBeatsOldUserCopy|WHPXQEMU64Features|WindowsCPUResolution)$') |
| 96 | Invoke-BuildTool 'go' @('test', 'github.com/lima-vm/lima/v2/pkg/hostagent', '-run', '^TestResourceProbeCommand$') |
| 97 | Invoke-BuildTool 'go' @('test', 'github.com/lima-vm/lima/v2/pkg/networks/usernet', '-run', 'TestNetstackWindowsNativeSocketPaths') |
| 98 | Invoke-BuildTool 'go' @('vet', './cmd/cage', './internal/cage', './internal/workbench', './internal/appupdate', './internal/host') |
| 99 | } |
| 100 | $packageArguments = @{ Release = $Release; SkipBuild = $true } |
| 101 | if ($Release) { $packageArguments.QemuSourceArchive = $qemuSource } |
| 102 | & (Join-Path $PSScriptRoot 'package-windows.ps1') @packageArguments |
| 103 | $version = Get-CageVersion $repository |
| 104 | $suffix = if ($Release) { '' } else { '-unsigned' } |
| 105 | & (Join-Path $PSScriptRoot 'test-windows-install.ps1') -Installer "$repository/_output/releases/$version/windows/Cage-$version-windows-amd64-setup$suffix.exe" |
| 106 | if ($Release) { |
| 107 | Invoke-BuildTool 'node' @('scripts/release-version.mjs', 'metadata', 'windows', "_output/releases/$version/windows/build-metadata.json") |
| 108 | } |
| 109 | } finally { |
| 110 | if ($pfx) { Remove-Item -LiteralPath $pfx -Force -ErrorAction SilentlyContinue } |
| 111 | foreach ($imported in $certificates) { |
| 112 | if ($imported.HasPrivateKey) { |
| 113 | Remove-Item "Cert:\CurrentUser\My\$($imported.Thumbprint)" -DeleteKey -Force -ErrorAction SilentlyContinue |
| 114 | } else { |
| 115 | Remove-Item "Cert:\CurrentUser\My\$($imported.Thumbprint)" -Force -ErrorAction SilentlyContinue |
| 116 | } |
| 117 | } |
| 118 | # Verified QEMU sources remain in the build cache; packaging copies them separately. |
| 119 | $env:PATH = $savedPath |
| 120 | $env:CAGE_WINDOWS_CERT_SHA1 = $savedCertificate |
| 121 | $env:CAGE_TEST_STORAGE_VOLUME = $savedStorageVolume |
| 122 | if ($storageTestRoot) { Remove-Item -LiteralPath $storageTestRoot -Recurse -Force -ErrorAction SilentlyContinue } |
| 123 | Remove-Item Env:CAGE_ESIGNER_PASSWORD, Env:CAGE_ESIGNER_TOTP_SECRET -ErrorAction SilentlyContinue |
| 124 | Pop-Location |
| 125 | } |