Skip to content

Commit ce4345b

Browse files
mokagioAliSoftware
andauthored
Introspect Windows CI image and skip setup accordingly (#192)
Co-authored-by: Olivier Halligon <[email protected]>
1 parent 83f6453 commit ce4345b

File tree

5 files changed

+112
-33
lines changed

5 files changed

+112
-33
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ _None._
3838

3939
### New Features
4040

41-
_None._
41+
- The `prepare_windows_host_for_app_distribution.ps1` command for Windows builds will inspect the environment it's running on and skip time consuming steps when running on our custom CI image. [#192]
4242

4343
### Bug Fixes
4444

4545
_None._
4646

4747
### Internal Changes
4848

49-
Improved header comment documentation of `pr_changed_files`. [#191]
49+
- Improved header comment documentation of `pr_changed_files`. [#191]
5050

5151
## 5.6.0
5252

bin/install_python.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ if ($DryRun) {
6666
# Some Node.js packages require Python during npm install for building native extensions
6767
Write-Output "Installing Python 3 via Chocolatey..."
6868
choco install python3 --yes --no-progress
69-
If ($LastExitCode -ne 0) {
69+
If ($LastExitCode -ne 0) {
7070
Write-Output "[!] Failed to install Python via Chocolatey"
71-
Exit $LastExitCode
71+
Exit $LastExitCode
7272
}
7373

7474
# Refresh environment to make Python available in PATH
7575
Write-Output "Refreshing environment variables..."
7676
& "$PSScriptRoot\path_aware_refreshenv.ps1"
77-
If ($LastExitCode -ne 0) {
77+
If ($LastExitCode -ne 0) {
7878
Write-Output "[!] Failed to refresh environment after Python installation"
79-
Exit $LastExitCode
79+
Exit $LastExitCode
8080
}
8181

8282
# Verify Python installation

bin/prepare_windows_host_for_app_distribution.ps1

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ param (
2727
# Stop script execution when a non-terminating error occurs
2828
$ErrorActionPreference = "Stop"
2929

30+
$amiVersion = [Environment]::GetEnvironmentVariable('AMI_VERSION', 'Machine')
31+
Write-Output "AMI_VERSION is: $amiVersion"
32+
if (![string]::IsNullOrWhiteSpace($amiVersion) -and [version]$amiVersion -ge [version]'0.2') {
33+
Write-Output "Tooling is already pre-installed in AMI versions >= 0.2. Only installing code signing certificate."
34+
& "setup_windows_code_signing.ps1"
35+
Exit 0
36+
}
37+
3038
# Validate parameter combinations
3139
if ($InstallNativeCompilationTools -and $SkipWindows10SDKInstallation) {
3240
Write-Output "[!] Invalid parameter combination: -InstallNativeCompilationTools cannot be used with -SkipWindows10SDKInstallation."
@@ -88,14 +96,6 @@ Write-Output "--- :windows: Setting up Package Manager"
8896
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
8997
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
9098

91-
if ($InstallPython) {
92-
Write-Output "Run with InstallPython = true. Installing Python for Node.js native module compilation..."
93-
& "$PSScriptRoot\install_python.ps1"
94-
If ($LastExitCode -ne 0) { Exit $LastExitCode }
95-
} else {
96-
Write-Output "Python installation not requested. Skipping Python installation."
97-
}
98-
9999
# This should avoid issues with symlinks not being supported in Windows.
100100
#
101101
# See how this build failed
@@ -115,29 +115,24 @@ if ($developerMode.State -eq 'Enabled') {
115115
}
116116
}
117117

118+
if ($InstallPython) {
119+
Write-Output "Run with InstallPython = true. Installing Python for Node.js native module compilation..."
120+
& "$PSScriptRoot\install_python.ps1"
121+
If ($LastExitCode -ne 0) { Exit $LastExitCode }
122+
} else {
123+
Write-Output "Python installation not requested. Skipping Python installation."
124+
}
125+
118126
if ($env:SKIP_CERTIFICATE_DOWNLOAD -eq "true") {
119127
Write-Output "--- :lock_with_ink_pen: Skipping Code Signing Certificate download"
120128
} else {
121-
Write-Output "--- :lock_with_ink_pen: Download Code Signing Certificate"
122-
$certificateBinPath = "certificate.bin"
123-
$EncodedText = aws secretsmanager get-secret-value --secret-id windows-code-signing-certificate `
124-
| jq -r '.SecretString' `
125-
| Out-File $certificateBinPath
126-
$certificatePfxPath = "certificate.pfx"
127-
certutil -decode $certificateBinPath $certificatePfxPath
128-
Write-Output "Code signing certificate downloaded at: $((Get-Item $certificatePfxPath).FullName)"
129-
130-
If ($LastExitCode -ne 0) {
131-
Write-Output "[!] Failed to download code signing certificate."
132-
Exit $LastExitCode
133-
}
129+
& "setup_windows_code_signing.ps1"
134130
}
135131

136-
Write-Output "--- :windows: Checking whether to install Windows 10 SDK..."
137-
138132
# When using Electron Forge and electron2appx, building Appx requires the Windows 10 SDK
139133
#
140134
# See https://github.com/hermit99/electron-windows-store/tree/v2.1.2?tab=readme-ov-file#usage
135+
Write-Output "--- :windows: Checking whether to install Windows 10 SDK..."
141136

142137
if ($SkipWindows10SDKInstallation) {
143138
Write-Output "Run with SkipWindows10SDKInstallation = true. Skipping Windows 10 SDK installation check."
@@ -147,7 +142,7 @@ if ($SkipWindows10SDKInstallation) {
147142
$windowsSDKVersionFile = ".windows-10-sdk-version"
148143
if (Test-Path $windowsSDKVersionFile) {
149144
Write-Output "Found $windowsSDKVersionFile file, installing Windows 10 SDK..."
150-
145+
151146
if ($InstallNativeCompilationTools) {
152147
& "$PSScriptRoot\install_windows_10_sdk.ps1" -InstallNativeCompilationTools
153148
} else {

bin/setup_windows_code_signing.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Installs the code signing certificate for Windows app distribution.
2+
#
3+
# The certificate is stored in our AWS SecretsManager storage (`windows-code-signing-certificate` secret ID).
4+
# It is decoded and stored in the `certificate.pfx` file.
5+
6+
# Stop script execution when a non-terminating error occurs
7+
$ErrorActionPreference = "Stop"
8+
9+
Write-Output "--- :lock_with_ink_pen: Download Code Signing Certificate"
10+
$certificateBinPath = "certificate.bin"
11+
$EncodedText = aws secretsmanager get-secret-value --secret-id windows-code-signing-certificate `
12+
| jq -r '.SecretString' `
13+
| Out-File $certificateBinPath
14+
$certificatePfxPath = "certificate.pfx"
15+
certutil -decode $certificateBinPath $certificatePfxPath
16+
17+
If ($LastExitCode -ne 0) {
18+
Write-Output "[!] Failed to download code signing certificate."
19+
Exit $LastExitCode
20+
} else {
21+
Write-Output "Code signing certificate downloaded at: $((Get-Item $certificatePfxPath).FullName)"
22+
}

tests/test_prepare_windows_host_for_app_distribution.ps1

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# 3. No .windows-10-sdk-version file with -InstallNativeCompilationTools (should skip installation - no version file available)
77
# 4. No .windows-10-sdk-version file without -InstallNativeCompilationTools (should skip installation)
88
#
9-
# Note: These tests focus on parameter validation and decision logic only, without performing
10-
# expensive installations or downloads.
9+
# Note: These tests focus on parameter validation and decision logic only,
10+
# without performing expensive installations or downloads.
1111

1212
param (
1313
[int]$ExpectedExitCode = 0
@@ -141,7 +141,69 @@ if ($output -match [regex]::Escape($expectedNoFileSkipMessage)) {
141141
Exit 1
142142
}
143143

144-
# Clean up
144+
#
145+
# FIXME: Disabled because I haven't figured out how to set the env var in a way that the scripts can read but doesn't alter the machine.
146+
# Tracked in https://linear.app/a8c/issue/AINFRA-1467
147+
#
148+
# # Test 5: If CI AMI version is == 0.2, only installs certificates
149+
# Write-Output "`n--- Test 5: If AMI version env == 0.2, only installs certificates"
150+
# Remove-TestFiles
151+
152+
# # Notice that setting via $env is process bound, which is perfect for unit tests.
153+
# $env:AMI_VERSION = '0.2'
154+
155+
# # Notice running the command via . so that it runs in the same process and accesses AMI_VERSION
156+
# $output = . "$PSScriptRoot\..\bin\prepare_windows_host_for_app_distribution.ps1"
157+
# $exitCode = $LASTEXITCODE
158+
159+
# if ($exitCode -ne $ExpectedExitCode) {
160+
# Write-Output "$emojiRedCross Test 5: Expected exit code $ExpectedExitCode, got $exitCode"
161+
# Write-Output "Output was:"
162+
# Write-Output "$output"
163+
# Exit 1
164+
# } else {
165+
# Write-Output "$emojiGreenCheck Test 5: Exit code matches expected value ($ExpectedExitCode)"
166+
# }
167+
168+
# $expectedOnlyCertsMessage = "Tooling is already pre-installed in AMI versions >= 0.2. Only installing code signing certificate."
169+
# if ($output -match [regex]::Escape($expectedOnlyCertsMessage)) {
170+
# Write-Output "$emojiGreenCheck Test 5: Found expected message regarding AMI version and what to install."
171+
# } else {
172+
# Write-Output "$emojiRedCross Test 5: Expected to find AMI version acknowledgement, but got:"
173+
# Write-Output "$output"
174+
# Exit 1
175+
# }
176+
177+
# # Test 6: If CI AMI version is > 0.2, only installs certificates
178+
# Write-Output "`n--- Test 6: If AMI version env > 0.2, only installs certificates"
179+
# Remove-TestFiles
180+
181+
# # Notice that setting via $env is process bound, which is perfect for unit tests.
182+
# $env:AMI_VERSION = '1.0'
183+
184+
# # Notice running the command via . so that it runs in the same process and accesses AMI_VERSION
185+
# $output = . "$PSScriptRoot\..\bin\prepare_windows_host_for_app_distribution.ps1"
186+
# $exitCode = $LASTEXITCODE
187+
188+
# if ($exitCode -ne $ExpectedExitCode) {
189+
# Write-Output "$emojiRedCross Test 6: Expected exit code $ExpectedExitCode, got $exitCode"
190+
# Write-Output "Output was:"
191+
# Write-Output "$output"
192+
# Exit 1
193+
# } else {
194+
# Write-Output "$emojiGreenCheck Test 6: Exit code matches expected value ($ExpectedExitCode)"
195+
# }
196+
197+
# $expectedOnlyCertsMessage = "Tooling is already pre-installed in AMI versions >= 0.2. Only installing code signing certificate."
198+
# if ($output -match [regex]::Escape($expectedOnlyCertsMessage)) {
199+
# Write-Output "$emojiGreenCheck Test 6: Found expected message regarding AMI version and what to install."
200+
# } else {
201+
# Write-Output "$emojiRedCross Test 6: Expected to find AMI version acknowledgement, but got:"
202+
# Write-Output "$output"
203+
# Exit 1
204+
# }
205+
206+
# Final clean up
145207
Remove-TestFiles
146208

147209
Write-Output "`n$emojiGreenCheck All tests completed successfully."

0 commit comments

Comments
 (0)