Skip to content

Commit ba8e178

Browse files
authored
Add PowerShell command to setup Windows environment (#100)
1 parent 29064ab commit ba8e178

File tree

5 files changed

+157
-9
lines changed

5 files changed

+157
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ buildkite-plugin-lint:
1212

1313
shellcheck:
1414
@echo ~~~ 🕵️ ShellCheck
15-
$(docker_run) koalaman/shellcheck hooks/** bin/** --exclude=SC1071
15+
$(docker_run) koalaman/shellcheck $(shell find hooks bin -type f -not -name "*.ps1") --exclude=SC1071
1616

1717
rubocop:
1818
@echo ~~~ 🕵️ Rubocop

bin/add_ssh_key_to_agent

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
NEW_SSH_KEY=$1
44
NEW_SSH_KEY_NAME=$2
55

6+
echo "--- :lock_with_ink_pen: Adding custom SSH key to agent ($NEW_SSH_KEY_NAME)"
7+
68
# Create key in ~/.ssh dir
79
NEW_SSH_KEY_PATH="$HOME"/.ssh/"$NEW_SSH_KEY_NAME"
810
echo -e "$NEW_SSH_KEY" > "$NEW_SSH_KEY_PATH"
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Stop script execution when a non-terminating error occurs
2+
$ErrorActionPreference = "Stop"
3+
4+
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
5+
Write-Host "--- :bug: Running as Administrator"
6+
} else {
7+
Write-Host "--- :bug: Running as not Administrator"
8+
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
9+
$roles = $principal.Identity.Groups | ForEach-Object {
10+
$_.Translate([Security.Principal.NTAccount]).Value
11+
}
12+
Write-Host "Your roles are:"
13+
$roles | ForEach-Object { Write-Host " - $_" }
14+
}
15+
16+
Write-Host "--- :windows: Setting up Windows for Node and Electorn builds"
17+
18+
Write-Host "Enable long path behavior"
19+
# See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file#maximum-path-length-limitation
20+
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1
21+
22+
# Disable Windows Defender before starting – otherwise our performance is terrible
23+
Write-Host "Disable Windows Defender..."
24+
$avPreference = @(
25+
@{DisableArchiveScanning = $true}
26+
@{DisableAutoExclusions = $true}
27+
@{DisableBehaviorMonitoring = $true}
28+
@{DisableBlockAtFirstSeen = $true}
29+
@{DisableCatchupFullScan = $true}
30+
@{DisableCatchupQuickScan = $true}
31+
@{DisableIntrusionPreventionSystem = $true}
32+
@{DisableIOAVProtection = $true}
33+
@{DisablePrivacyMode = $true}
34+
@{DisableScanningNetworkFiles = $true}
35+
@{DisableScriptScanning = $true}
36+
@{MAPSReporting = 0}
37+
@{PUAProtection = 0}
38+
@{SignatureDisableUpdateOnStartupWithoutEngine = $true}
39+
@{SubmitSamplesConsent = 2}
40+
@{ScanAvgCPULoadFactor = 5; ExclusionPath = @("D:\", "C:\")}
41+
@{DisableRealtimeMonitoring = $true}
42+
@{ScanScheduleDay = 8}
43+
)
44+
45+
$avPreference += @(
46+
@{EnableControlledFolderAccess = "Disable"}
47+
@{EnableNetworkProtection = "Disabled"}
48+
)
49+
50+
$avPreference | Foreach-Object {
51+
$avParams = $_
52+
Set-MpPreference @avParams
53+
}
54+
55+
# https://github.com/actions/runner-images/issues/4277
56+
# https://docs.microsoft.com/en-us/microsoft-365/security/defender-endpoint/microsoft-defender-antivirus-compatibility?view=o365-worldwide
57+
$atpRegPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows Advanced Threat Protection'
58+
if (Test-Path $atpRegPath) {
59+
Write-Host "Set Microsoft Defender Antivirus to passive mode"
60+
Set-ItemProperty -Path $atpRegPath -Name 'ForceDefenderPassiveMode' -Value '1' -Type 'DWORD'
61+
}
62+
63+
Write-Host "--- :lock_with_ink_pen: Downloading Code Signing Certificate"
64+
$EncodedText = aws secretsmanager get-secret-value --secret-id windows-code-signing-certificate | jq -r '.SecretString' | Out-File 'certificate.bin'
65+
certutil -decode certificate.bin certificate.pfx
66+
If ($LastExitCode -ne 0) { Exit $LastExitCode }
67+
68+
# From https://stackoverflow.com/a/46760714
69+
Write-Host "--- :windows: Setting up Package Manager"
70+
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
71+
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
72+
73+
# This should avoid issues with symlinks not being supported in Windows.
74+
#
75+
# See how this build failed
76+
# https://buildkite.com/automattic/beeper-desktop/builds/2895#01919738-7c6e-4b82-8d1d-1c1800481740
77+
Write-Host "--- :windows: :linux: Enable developer mode to use symlinks"
78+
79+
$developerMode = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
80+
81+
if ($developerMode.State -eq 'Enabled') {
82+
Write-Host "Developer Mode is already enabled."
83+
} else {
84+
Write-Host "Enabling Developer Mode..."
85+
try {
86+
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart
87+
} catch {
88+
Write-Host "Failed to enable Developer Mode. Continuing without it..."
89+
}
90+
}
91+
92+
Write-Host "--- :node: Installing NVM"
93+
choco install nvm.portable -y
94+
If ($LastExitCode -ne 0) { Exit $LastExitCode }
95+
96+
Write-Host "--- :hammer: Custom PATH refresh post NVM installation to avoid losing previous PATH changes"
97+
Write-Host "PATH before refreshenv is $env:PATH"
98+
# It looks like out of the box, calling refreshenv at this point erases various PATH modifications made by the rest of our automation.
99+
#
100+
# See https://buildkite.com/automattic/beeper-desktop/builds/2893#01919717-d0d0-441d-a85d-0fe3223467d2/195
101+
#
102+
# To avoid the issue, we save the PATH pre-refreshenv and then manually add all the components that were removed.
103+
$originalPath = "$env:PATH"
104+
refreshenv
105+
$mergedPath = "$env:PATH;$originalPath" -split ";" | Select-Object -Unique -Skip 1
106+
$env:PATH = ($mergedPath -join ";")
107+
Write-Host "PATH after refreshenv is $env:PATH"
108+
109+
$nvmRCPath = '.nvmrc'
110+
if (-not (Test-Path $nvmRCPath)) {
111+
Write-Host "No .nvmrc found. Skipping Node set up."
112+
Exit 0
113+
}
114+
115+
Write-Host "--- :node: Installing Node"
116+
$nvmVersion=(Get-Content -Path $nvmRCPath -Total 1)
117+
Write-Host "Switching to nvm version defined in .nvmrc: $nvmVersion"
118+
119+
nvm install $nvmVersion
120+
nvm use $nvmVersion
121+
If ($LastExitCode -ne 0) { Exit $LastExitCode }
122+
123+
Write-Host "--- :hammer: Custom PATH refresh post NVM installation to avoid losing previous PATH changes"
124+
Write-Host "PATH before refreshenv is $env:PATH"
125+
# It looks like out of the box, calling refreshenv at this point erases various PATH modifications made by the rest of our automation.
126+
#
127+
# See https://buildkite.com/automattic/beeper-desktop/builds/2893#01919717-d0d0-441d-a85d-0fe3223467d2/195
128+
#
129+
# To avoid the issue, we save the PATH pre-refreshenv and then manually add all the components that were removed.
130+
$originalPath = "$env:PATH"
131+
refreshenv
132+
$mergedPath = "$env:PATH;$originalPath" -split ";" | Select-Object -Unique -Skip 1
133+
$env:PATH = ($mergedPath -join ";")
134+
Write-Host "PATH after refreshenv is $env:PATH"

hooks/environment

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@
77

88
set -e
99

10+
echo "~~~ :file_cabinet: Loading Automattic plugin commands in the environment"
11+
1012
HOOKS_ROOT=$( dirname "${BASH_SOURCE[0]}" )
1113
PLUGIN_ROOT=$( dirname "$HOOKS_ROOT" )
12-
PLUGIN_BIN="$PLUGIN_ROOT/bin"
1314

14-
export PATH="$PATH:$PLUGIN_BIN"
15+
OS=$(uname -s)
16+
echo "FYI: Running on OS $OS"
1517

16-
echo "~~~ :file_cabinet: Loaded Caching Plugin"
18+
# Notice that we don't need any OS-specific treatment of the path format for UNIX vs Windows.
19+
# When Windows runs this script via bash, it handles the path separation conversion from / to \ internally.
20+
PLUGIN_BIN="${PLUGIN_ROOT}/bin"
21+
22+
echo "Original PATH: $PATH"
23+
export PATH="$PATH:$PLUGIN_BIN"
24+
echo "PATH updated to: $PATH"

tests/test-that-all-files-are-executable.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
# it seems that running `[ -x ]` under `bats` in Docker on a Mac returns invalid results, and this was more reliable.
1212
#
1313
# See: https://github.com/Automattic/a8c-ci-toolkit-buildkite-plugin/pull/42
14-
context 'All Commands Should Be Executable' do
15-
Dir.children('bin').map { |f| File.new(File.join('bin', f)) }.each do |file|
16-
it file.path do
17-
expect(file.stat.executable?).to be true
14+
context 'All Unix Commands Should Be Executable' do
15+
Dir
16+
.children('bin')
17+
# Ignore Windows PowerShell scripts
18+
.reject { |f| f.end_with?('.ps1') }
19+
.map { |f| File.new(File.join('bin', f)) }.each do |file|
20+
it file.path do
21+
expect(file.stat.executable?).to be true
22+
end
1823
end
19-
end
2024
end

0 commit comments

Comments
 (0)