From a456ede464d5b9962e9a37ef33935e2fc4d80e82 Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Mon, 11 May 2020 19:46:36 +0300 Subject: [PATCH] Install Go to hostedtoolcache directory on Windows image (#849) * Move installed go versions to toolcache directory --- images/win/scripts/Installers/Install-Go.ps1 | 26 +++++++++++-------- images/win/scripts/Installers/Validate-Go.ps1 | 6 ++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/images/win/scripts/Installers/Install-Go.ps1 b/images/win/scripts/Installers/Install-Go.ps1 index 6d98ef80..c5d4c144 100644 --- a/images/win/scripts/Installers/Install-Go.ps1 +++ b/images/win/scripts/Installers/Install-Go.ps1 @@ -29,36 +29,40 @@ function Install-GoVersion # Extract the zip archive. It contains a single directory named "go". Write-Host "Extracting Go $latestVersion..." - Expand-Archive -Path $goArchPath -DestinationPath "C:\" -Force + $toolDirectory = Join-Path $env:AGENT_TOOLSDIRECTORY "go\$latestVersion" + 7z.exe x $goArchPath -o"$toolDirectory" -y | Out-Null + + # Rename the extracted "go" directory to "x64" for full path "C:\hostedtoolcache\windows\Go\1.14.2\x64\..." + Rename-Item -path "$toolDirectory\go" -newName "x64" + $fullArchPath = "$toolDirectory\x64" # Delete unnecessary files to conserve space Write-Host "Cleaning directories of Go $latestVersion..." - if (Test-Path "C:\go\doc") + if (Test-Path "$fullArchPath\doc") { - Remove-Item -Recurse -Force "C:\go\doc" + Remove-Item -Recurse -Force "$fullArchPath\doc" } - if (Test-Path "C:\go\blog") + if (Test-Path "$fullArchPath\blog") { - Remove-Item -Recurse -Force "C:\go\blog" + Remove-Item -Recurse -Force "$fullArchPath\blog" } - # Rename the extracted "go" directory to include the Go version number (to support side-by-side versions of Go). - $newDirName = "Go$latestVersion" - Rename-Item -path "C:\go" -newName $newDirName + # Create symlink in old location + New-Item -Path "C:\go$latestVersion" -ItemType SymbolicLink -Value $fullArchPath # Make this the default version of Go? if ($addToDefaultPath) { Write-Host "Adding Go $latestVersion to the path..." # Add the Go binaries to the path. - Add-MachinePathItem "C:\$newDirName\bin" | Out-Null + Add-MachinePathItem "$fullArchPath\bin" | Out-Null # Set the GOROOT environment variable. - setx GOROOT "C:\$newDirName" /M | Out-Null + setx GOROOT "$fullArchPath" /M | Out-Null } # Done Write-Host "Done installing Go $latestVersion." - return "C:\$newDirName" + return $fullArchPath } # Install Go diff --git a/images/win/scripts/Installers/Validate-Go.ps1 b/images/win/scripts/Installers/Validate-Go.ps1 index 059ea8fa..4c88cc1d 100644 --- a/images/win/scripts/Installers/Validate-Go.ps1 +++ b/images/win/scripts/Installers/Validate-Go.ps1 @@ -11,9 +11,9 @@ function Get-GoVersion [String]$goVersion ) Write-Host "Check if $goVersion is presented in the system" - $DestinationPath = "$($env:SystemDrive)\" - $goDirectory = Get-ChildItem -Path $DestinationPath -Filter "Go$goVersion*" | Select-Object -First 1 - $goPath = Join-Path $env:SystemDrive $goDirectory + $destinationPath = "$($env:AGENT_TOOLSDIRECTORY)\go" + $goDirectory = Get-ChildItem -Path $destinationPath -Filter "$goVersion*" | Select-Object -First 1 + $goPath = Join-Path $destinationPath "$goDirectory\x64" $env:Path = "$goPath\bin;" + $env:Path $version = $(go version)