Skip to content

Commit 81009e8

Browse files
update environment variables after installing Swift on Windows
1 parent 99835f7 commit 81009e8

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

.github/workflows/scripts/windows/swift/install-swift.ps1

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,50 @@ function Install-Swift {
175175
exit 1
176176
}
177177
Remove-FileWithRetry -Path installer.exe
178-
}
178+
179+
Write-Host -NoNewLine 'Adding Swift to PATH ... '
180+
# Older toolchains modify the Machine PATH to include \Library\Developer\Toolchains
181+
foreach ($Item in [Environment]::GetEnvironmentVariables('Machine')['Path'].Split(';')) {
182+
if ($Item.Contains('Swift') -or $Item.Contains('Library\Developer\Toolchains')) {
183+
GitHubActions-AddPath -Item $Item
184+
}
185+
}
186+
# Newer toolchains modify the User PATH to include a path to a Swift directory
187+
foreach ($Item in [Environment]::GetEnvironmentVariables('User')['Path'].Split(';')) {
188+
if ($Item.Contains('Swift')) {
189+
GitHubActions-AddPath -Item $Item
190+
}
191+
}
192+
# Find and export the SDKROOT environment variable
193+
$MachineSDKROOT = [Environment]::GetEnvironmentVariables('Machine')['SDKROOT']
194+
$UserSDKROOT = [Environment]::GetEnvironmentVariables('User')['SDKROOT']
195+
if ($MachineSDKROOT) {
196+
GitHubActions-ExportVariable -Name "SDKROOT" -Value $MachineSDKROOT
197+
} elseif ($UserSDKROOT) {
198+
GitHubActions-ExportVariable -Name "SDKROOT" -Value $UserSDKROOT
199+
}
200+
Write-Host 'SUCCESS'
201+
}
202+
203+
function GitHubActions-AddPath {
204+
param (
205+
[string]$Item
206+
)
207+
$FilePath = $env:GITHUB_PATH
208+
if ($FilePath) {
209+
Add-Content -Path "$FilePath" -Value "$Item`r`n"
210+
}
211+
$env:Path = $Item + ";" + $env:Path
212+
}
213+
214+
function GitHubActions-ExportVariable {
215+
param (
216+
[string]$Name,
217+
[string]$Value
218+
)
219+
$FilePath = $env:GITHUB_ENV
220+
if ($FilePath) {
221+
Add-Content -Path "$FilePath" -Value "$Name=$Value`r`n"
222+
}
223+
Set-Content env:\$Name $Value
224+
}

.github/workflows/swift_package_test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,4 @@ jobs:
736736
- name: Build / Test
737737
timeout-minutes: 60
738738
if: ${{ !inputs.enable_windows_docker }}
739-
run: |
740-
Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1
741-
RefreshEnv
742-
powershell.exe -NoLogo -File $env:TEMP\test-script\run.ps1; exit $LastExitCode
739+
run: powershell.exe -NoLogo -File $env:TEMP\test-script\run.ps1; exit $LastExitCode

0 commit comments

Comments
 (0)