Skip to content

Commit

Permalink
[Windows] Use common approach to set machine vars (actions#8830)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpolikarpov-akvelon authored Nov 17, 2023
1 parent 045a0de commit 07e8da7
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 75 deletions.
14 changes: 10 additions & 4 deletions images/windows/scripts/build/Configure-SystemEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
## Desc: Configures system environment variables
################################################################################

setx ImageVersion $env:IMAGE_VERSION /m
setx ImageOS $env:IMAGE_OS /m
setx AGENT_TOOLSDIRECTORY $env:AGENT_TOOLSDIRECTORY /m
setx ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE $env:ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE /m
$variables = @{
"ImageVersion" = $env:IMAGE_VERSION
"ImageOS" = $env:IMAGE_OS
"AGENT_TOOLSDIRECTORY" = $env:AGENT_TOOLSDIRECTORY
"ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE" = $env:ACTIONS_RUNNER_ACTION_ARCHIVE_CACHE
}

$variables.GetEnumerator() | ForEach-Object {
[Environment]::SetEnvironmentVariable($_.Key, $_.Value, "Machine")
}
13 changes: 5 additions & 8 deletions images/windows/scripts/build/Configure-Toolset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Function Set-DefaultVariables

if (-not ([string]::IsNullOrEmpty($EnvVars.defaultVariable)))
{
setx $toolEnvVars.defaultVariable $ToolVersionPath /M | Out-Null
[Environment]::SetEnvironmentVariable($toolEnvVars.defaultVariable, $ToolVersionPath, "Machine")
}
}

Expand All @@ -48,20 +48,17 @@ $tools = Get-ToolsetContent | Select-Object -ExpandProperty toolcache `
| Where-Object { $toolsToConfigure -contains $_.name }

Write-Host "Configure toolset tools environment..."
foreach ($tool in $tools)
{
foreach ($tool in $tools) {
$toolEnvVars = $toolsEnvironmentVariables[$tool.name]

if (-not ([string]::IsNullOrEmpty($toolEnvVars.variableTemplate)))
{
foreach ($version in $tool.versions)
{
if (-not ([string]::IsNullOrEmpty($toolEnvVars.variableTemplate))) {
foreach ($version in $tool.versions) {
Write-Host "Set $($tool.name) $version environment variable..."

$foundVersionArchPath = Get-ToolsetToolFullPath -Name $tool.name -Version $version -Arch $tool.arch
$envName = $toolEnvVars.variableTemplate -f $version.Split(".")

setx $envName $foundVersionArchPath /M | Out-Null
[Environment]::SetEnvironmentVariable($envName, $foundVersionArchPath, "Machine")
}
}

Expand Down
12 changes: 6 additions & 6 deletions images/windows/scripts/build/Install-AndroidSDK.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ $ndkDefaultVersion = ($androidNDKs | Where-Object { $_ -match "ndk;$ndkDefaultMa
$ndkRoot = "$sdkRoot\ndk\$ndkDefaultVersion"

# Create env variables
setx ANDROID_HOME $sdkRoot /M
setx ANDROID_SDK_ROOT $sdkRoot /M
[Environment]::SetEnvironmentVariable("ANDROID_HOME", $sdkRoot, "Machine")
[Environment]::SetEnvironmentVariable("ANDROID_SDK_ROOT", $sdkRoot, "Machine")
# ANDROID_NDK, ANDROID_NDK_HOME, and ANDROID_NDK_ROOT variables should be set as many customer builds depend on them https://github.com/actions/runner-images/issues/5879
setx ANDROID_NDK $ndkRoot /M
setx ANDROID_NDK_HOME $ndkRoot /M
setx ANDROID_NDK_ROOT $ndkRoot /M
[Environment]::SetEnvironmentVariable("ANDROID_NDK", $ndkRoot, "Machine")
[Environment]::SetEnvironmentVariable("ANDROID_NDK_HOME", $ndkRoot, "Machine")
[Environment]::SetEnvironmentVariable("ANDROID_NDK_ROOT", $ndkRoot, "Machine")

$ndkLatestPath = "$sdkRoot\ndk\$ndkLatestVersion"
if (Test-Path $ndkLatestPath) {
setx ANDROID_NDK_LATEST_HOME $ndkLatestPath /M
[Environment]::SetEnvironmentVariable("ANDROID_NDK_LATEST_HOME", $ndkLatestPath, "Machine")
} else {
Write-Host "Latest NDK $ndkLatestVersion is not installed at path $ndkLatestPath"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-Chrome.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Write-Host "Expand Chrome WebDriver archive (without using directory names)..."
Extract-7Zip -Path $ChromeDriverArchPath -DestinationPath $ChromeDriverPath -ExtractMethod "e"

Write-Host "Setting the environment variables..."
setx ChromeWebDriver "$ChromeDriverPath" /M
[Environment]::SetEnvironmentVariable("ChromeWebDriver", $ChromeDriverPath, "Machine")

$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'
$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path'
Expand Down
6 changes: 3 additions & 3 deletions images/windows/scripts/build/Install-DotnetSDK.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
################################################################################

# Set environment variables
Set-SystemVariable -SystemVariable DOTNET_MULTILEVEL_LOOKUP -Value "0"
Set-SystemVariable -SystemVariable DOTNET_NOLOGO -Value "1"
Set-SystemVariable -SystemVariable DOTNET_SKIP_FIRST_TIME_EXPERIENCE -Value "1"
[System.Environment]::SetEnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP", "0", "Machine")
[System.Environment]::SetEnvironmentVariable("DOTNET_NOLOGO", "1", "Machine")
[System.Environment]::SetEnvironmentVariable("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "1", "Machine")

[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12"

Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-EdgeDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $EdgeDriverSignatureThumbprint = ("7C94971221A799907BB45665663BBFD587BAC9F8", "7
Test-FileSignature -FilePath "$EdgeDriverPath\msedgedriver.exe" -ExpectedThumbprint $EdgeDriverSignatureThumbprint

Write-Host "Setting the environment variables..."
setx EdgeWebDriver "$EdgeDriverPath" /M
[Environment]::SetEnvironmentVariable("EdgeWebDriver", $EdgeDriverPath, "Machine")

$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'
$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path'
Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-Firefox.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ Test-FileSignature -FilePath "$GeckoDriverPath/geckodriver.exe" -ExpectedThumbpr

Write-Host "Setting the environment variables..."
Add-MachinePathItem -PathItem $GeckoDriverPath
setx GeckoWebDriver "$GeckoDriverPath" /M
[Environment]::SetEnvironmentVariable("GeckoWebDriver", $GeckoDriverPath, "Machine")

Invoke-PesterTests -TestFile "Browsers" -TestName "Firefox"
6 changes: 3 additions & 3 deletions images/windows/scripts/build/Install-Haskell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ New-Item -Path "$ghcupPrefix\ghcup" -ItemType 'directory' -ErrorAction SilentlyC
New-Item -Path "$ghcupPrefix\ghcup\bin" -ItemType 'directory' -ErrorAction SilentlyContinue | Out-Null
Start-DownloadWithRetry -Url $ghcupDownloadURL -Name "ghcup.exe" -DownloadPath "$ghcupPrefix\ghcup\bin"

Set-SystemVariable "GHCUP_INSTALL_BASE_PREFIX" $ghcupPrefix
Set-SystemVariable "GHCUP_MSYS2" $msysPath
Set-SystemVariable "CABAL_DIR" $cabalDir
[System.Environment]::SetEnvironmentVariable("GHCUP_INSTALL_BASE_PREFIX", $ghcupPrefix, "Machine")
[System.Environment]::SetEnvironmentVariable("GHCUP_MSYS2", $msysPath, "Machine")
[System.Environment]::SetEnvironmentVariable("CABAL_DIR", $cabalDir, "Machine")
Add-MachinePathItem "$ghcupPrefix\ghcup\bin"
Add-MachinePathItem "$cabalDir\bin"

Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-IEWebDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Write-Host "Get the IEDriver version..."
(Get-Item "$ieDriverPath\IEDriverServer.exe").VersionInfo.FileVersion | Out-File -FilePath "$ieDriverPath\versioninfo.txt"

Write-Host "Setting the IEWebDriver environment variables"
setx IEWebDriver $ieDriverPath /M
[Environment]::SetEnvironmentVariable("IEWebDriver", $ieDriverPath, "Machine")

Invoke-PesterTests -TestFile "Browsers" -TestName "Internet Explorer"
12 changes: 6 additions & 6 deletions images/windows/scripts/build/Install-JavaTools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function Set-JavaPath {
}

Write-Host "Set 'JAVA_HOME_${Version}_X64' environmental variable as $javaPath"
setx JAVA_HOME_${Version}_X64 $javaPath /M
[Environment]::SetEnvironmentVariable("JAVA_HOME_${Version}_X64", $javaPath, "Machine")

if ($Default) {
# Clean up any other Java folders from PATH to make sure that they won't conflict with each other
Expand All @@ -42,7 +42,7 @@ function Set-JavaPath {
Set-MachinePath -NewPath $newPath

Write-Host "Set JAVA_HOME environmental variable as $javaPath"
setx JAVA_HOME $javaPath /M
[Environment]::SetEnvironmentVariable("JAVA_HOME", $javaPath, "Machine")
}
}

Expand Down Expand Up @@ -129,9 +129,9 @@ $maven_opts = '-Xms256m'
$m2_repo = 'C:\ProgramData\m2'
New-Item -Path $m2_repo -ItemType Directory -Force | Out-Null

setx M2 $m2 /M
setx M2_REPO $m2_repo /M
setx MAVEN_OPTS $maven_opts /M
[Environment]::SetEnvironmentVariable("M2", $m2, "Machine")
[Environment]::SetEnvironmentVariable("M2_REPO", $m2_repo, "Machine")
[Environment]::SetEnvironmentVariable("MAVEN_OPTS", $maven_opts, "Machine")

# Download cobertura jars
$uri = 'https://repo1.maven.org/maven2/net/sourceforge/cobertura/cobertura/2.1.1/cobertura-2.1.1-bin.zip'
Expand All @@ -143,6 +143,6 @@ $fileHash = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash
Use-ChecksumComparison $fileHash $sha256sum
Extract-7Zip -Path $archivePath -DestinationPath "C:\"

setx COBERTURA_HOME $coberturaPath /M
[Environment]::SetEnvironmentVariable("COBERTURA_HOME", $coberturaPath, "Machine")

Invoke-PesterTests -TestFile "Java"
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-Miniconda.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $InstallerUrl = "https://repo.anaconda.com/miniconda/${InstallerName}"
$ArgumentList = ("/S", "/AddToPath=0", "/RegisterPython=0", "/D=$CondaDestination")

Install-Binary -Url $InstallerUrl -Name $InstallerName -ArgumentList $ArgumentList
Set-SystemVariable -SystemVariable "CONDA" -Value $CondaDestination
[System.Environment]::SetEnvironmentVariable("CONDA", $CondaDestination, "Machine")

#region Supply chain security
$localFileHash = (Get-FileHash -Path (Join-Path ${env:TEMP} $installerName) -Algorithm SHA256).Hash
Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-NodeJS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Choco-Install -PackageName nodejs -ArgumentList "--version=$versionToInstall"
Add-MachinePathItem $PrefixPath
$env:Path = Get-MachinePath

setx npm_config_prefix $PrefixPath /M
[Environment]::SetEnvironmentVariable("npm_config_prefix", $PrefixPath, "Machine")
$env:npm_config_prefix = $PrefixPath

npm config set cache $CachePath --global
Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-PHP.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Choco-Install -PackageName composer -ArgumentList "--ia", "/DEV=$installDir /PHP
((Get-Content -path $installDir\php.ini -Raw) -replace ';extension=curl','extension=curl' -replace ';extension=mbstring','extension=mbstring' -replace ';extension_dir = "ext"','extension_dir = "ext"' -replace ';extension=openssl','extension=openssl') | Set-Content -Path $installDir\php.ini

# Set the PHPROOT environment variable.
setx PHPROOT $installDir /M
[Environment]::SetEnvironmentVariable("PHPROOT", $installDir, "Machine")

# Invoke Pester Tests
Invoke-PesterTests -TestFile "PHP"
4 changes: 2 additions & 2 deletions images/windows/scripts/build/Install-Pipx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ $env:PIPX_HOME = "${env:ProgramFiles(x86)}\pipx"
pip install pipx

Add-MachinePathItem "${env:PIPX_BIN_DIR}"
Set-SystemVariable -SystemVariable PIPX_BIN_DIR -Value $env:PIPX_BIN_DIR
Set-SystemVariable -SystemVariable PIPX_HOME -Value $env:PIPX_HOME
[System.Environment]::SetEnvironmentVariable("PIPX_BIN_DIR", $env:PIPX_BIN_DIR, "Machine")
[System.Environment]::SetEnvironmentVariable("PIPX_HOME", $env:PIPX_HOME, "Machine")

Invoke-PesterTests -TestFile "Tools" -TestName "Pipx"

Expand Down
29 changes: 14 additions & 15 deletions images/windows/scripts/build/Install-PostgreSQL.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ $pgUser = "postgres"
$pgPwd = "root"

# Prepare environment variable for validation
Set-SystemVariable -SystemVariable PGUSER -Value $pgUser
Set-SystemVariable -SystemVariable PGPASSWORD -Value $pgPwd
[System.Environment]::SetEnvironmentVariable("PGUSER", $pgUser, "Machine")
[System.Environment]::SetEnvironmentVariable("PGPASSWORD", $pgPwd, "Machine")

# Define latest available version to install based on version specified in the toolset
$toolsetVersion = (Get-ToolsetContent).postgresql.version
$getPostgreReleases = Invoke-WebRequest -Uri "https://git.postgresql.org/gitweb/?p=postgresql.git;a=tags" -UseBasicParsing
$getPostgreReleases = Invoke-WebRequest -Uri "https://git.postgresql.org/gitweb/?p=postgresql.git;a=tags" -UseBasicParsing
# Getting all links matched to the pattern (e.g.a=log;h=refs/tags/REL_14)
$TargetReleases = $getPostgreReleases.Links.href | Where-Object {$_ -match "a=log;h=refs/tags/REL_$toolsetVersion"}
$TargetReleases = $getPostgreReleases.Links.href | Where-Object { $_ -match "a=log;h=refs/tags/REL_$toolsetVersion" }
[Int32]$OutNumber = $null
$MinorVersions = @()
foreach ($release in $TargetReleases) {
$version = $release.split('/')[-1]
# Checking if the latest symbol of the release version is actually a number. If yes, add to $MinorVersions array
if ([Int32]::TryParse($($version.Split('_')[-1]),[ref]$OutNumber)){
$MinorVersions += $OutNumber
}
$version = $release.split('/')[-1]
# Checking if the latest symbol of the release version is actually a number. If yes, add to $MinorVersions array
if ([Int32]::TryParse($($version.Split('_')[-1]), [ref]$OutNumber)) {
$MinorVersions += $OutNumber
}
}
# Sorting and getting the last one
$TargetMinorVersions = ($MinorVersions | Sort-Object)[-1]
Expand Down Expand Up @@ -47,7 +47,7 @@ do {
# Return the previous value of ErrorAction and invoke Install-Binary function
$ErrorActionPreference = $ErrorActionOldValue
$InstallerName = $InstallerUrl.Split('/')[-1]
$ArgumentList = ("--install_runtimes 0","--superpassword root","--enable_acledit 1","--unattendedmodeui none","--mode unattended")
$ArgumentList = ("--install_runtimes 0", "--superpassword root", "--enable_acledit 1", "--unattendedmodeui none", "--mode unattended")
Install-Binary -Url $InstallerUrl -Name $InstallerName -ArgumentList $ArgumentList -ExpectedSignature (Get-ToolsetContent).postgresql.signature

# Get Path to pg_ctl.exe
Expand All @@ -63,16 +63,15 @@ $pgReadyPath = Join-Path $pgBin "pg_isready.exe"
$pgReady = Start-Process -FilePath $pgReadyPath -Wait -PassThru
$exitCode = $pgReady.ExitCode

if ($exitCode -ne 0)
{
if ($exitCode -ne 0) {
Write-Host -Object "PostgreSQL is not ready. Exitcode: $exitCode"
exit $exitCode
}

# Added PostgreSQL environment variable
Set-SystemVariable -SystemVariable PGBIN -Value $pgBin
Set-SystemVariable -SystemVariable PGROOT -Value $pgRoot
Set-SystemVariable -SystemVariable PGDATA -Value $pgData
[System.Environment]::SetEnvironmentVariable("PGBIN", $pgBin, "Machine")
[System.Environment]::SetEnvironmentVariable("PGROOT", $pgRoot, "Machine")
[System.Environment]::SetEnvironmentVariable("PGDATA", $pgData, "Machine")

# Stop and disable PostgreSQL service
$pgService = Get-Service -Name postgresql*
Expand Down
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-Selenium.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ New-Item -Path $seleniumDirectory -Name "$seleniumBinaryName-$seleniumFullVersio

# Add SELENIUM_JAR_PATH environment variable
$seleniumBinPath = Join-Path $seleniumDirectory $seleniumFileName
setx "SELENIUM_JAR_PATH" "$($seleniumBinPath)" /M
[Environment]::SetEnvironmentVariable("SELENIUM_JAR_PATH", $seleniumBinPath, "Machine")

Invoke-PesterTests -TestFile "Browsers" -TestName "Selenium"
2 changes: 1 addition & 1 deletion images/windows/scripts/build/Install-Vcpkg.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ Invoke-Expression "$InstallDir\$VcpkgExecPath integrate install"
# Add vcpkg to system environment
Add-MachinePathItem $InstallDir
$env:Path = Get-MachinePath
setx VCPKG_INSTALLATION_ROOT $InstallDir /M
[Environment]::SetEnvironmentVariable("VCPKG_INSTALLATION_ROOT", $InstallDir, "Machine")

Invoke-PesterTests -TestFile "Tools" -TestName "Vcpkg"
1 change: 0 additions & 1 deletion images/windows/scripts/helpers/ImageHelpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Export-ModuleMember -Function @(
'Add-DefaultItem'
'Get-SystemVariable'
'Get-DefaultVariable'
'Set-SystemVariable'
'Set-DefaultVariable'
'Install-Binary'
'Install-VisualStudio'
Expand Down
12 changes: 1 addition & 11 deletions images/windows/scripts/helpers/PathHelpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ function Get-DefaultVariable {
[System.GC]::Collect()
}

function Set-SystemVariable {
param(
[string]$SystemVariable,
[string]$Value
)

[System.Environment]::SetEnvironmentVariable($SystemVariable, $Value, "Machine")
Get-SystemVariable $SystemVariable
}

function Set-DefaultVariable {
param(
[string]$DefaultVariable,
Expand Down Expand Up @@ -93,7 +83,7 @@ function Set-MachinePath {
[string]$NewPath
)

Set-SystemVariable PATH $NewPath
[System.Environment]::SetEnvironmentVariable("PATH", $NewPath, "Machine")
}

function Set-DefaultPath {
Expand Down
7 changes: 0 additions & 7 deletions images/windows/scripts/helpers/test/PathHelpers.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,3 @@ Describe "Add-MachinePathItem Tests"{
Add-MachinePathItem -PathItem 'C:\baz' | Should Be 'C:\baz;C:\foo;C:\bar'
}
}

Describe 'Set-SystemVariable Tests' {
Mock Set-ItemProperty {return}
It 'Set-SystemVariable should return new path' {
Set-SystemVariable -SystemVariable "NewPathVar" -Value "C:\baz" | Should Be "C:\baz"
}
}

0 comments on commit 07e8da7

Please sign in to comment.