Install latest patch versions of Golang for Windows (#502)

* add golang for windows

* minor changes

* fix comments

* resolve comments

* add 1.14 version

* add debug lines

* fix arrays

* better way to remove ecmpty strings

* add path

* minor fix

Co-authored-by: Dmitry Shibanov <v-dmshib@microsoft.com>
This commit is contained in:
Dmitry Shibanov
2020-03-12 11:14:08 +03:00
committed by GitHub
parent 02204718b4
commit a3ecb363d7
4 changed files with 53 additions and 32 deletions

View File

@@ -28,7 +28,9 @@
"capture_name_prefix": "packer", "capture_name_prefix": "packer",
"image_version": "dev", "image_version": "dev",
"image_os": "win16", "image_os": "win16",
"github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}" "github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}",
"go_versions": "1.9, 1.10, 1.11, 1.12, 1.13, 1.14",
"go_default": "1.14"
}, },
"sensitive-variables": ["install_password", "ssh_password", "client_secret", "github_feed_token"], "sensitive-variables": ["install_password", "ssh_password", "client_secret", "github_feed_token"],
"builders": [ "builders": [
@@ -342,8 +344,8 @@
{ {
"type": "powershell", "type": "powershell",
"environment_vars": [ "environment_vars": [
"GO_VERSIONS=1.9.7,1.10.8,1.11.12,1.12.7,1.13", "GO_VERSIONS={{user `go_versions`}}",
"GO_DEFAULT=1.12.7" "GO_DEFAULT={{user `go_default`}}"
], ],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Install-Go.ps1" "{{ template_dir }}/scripts/Installers/Install-Go.ps1"
@@ -651,8 +653,8 @@
{ {
"type": "powershell", "type": "powershell",
"environment_vars": [ "environment_vars": [
"GO_VERSIONS=1.9.7,1.10.8,1.11.12,1.12.7,1.13", "GO_VERSIONS={{user `go_versions`}}",
"GO_DEFAULT=1.12.7" "GO_DEFAULT={{ user `go_default`}}"
], ],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Validate-Go.ps1" "{{ template_dir }}/scripts/Installers/Validate-Go.ps1"

View File

@@ -28,7 +28,9 @@
"capture_name_prefix": "packer", "capture_name_prefix": "packer",
"image_version": "dev", "image_version": "dev",
"image_os": "win19", "image_os": "win19",
"github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}" "github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}",
"go_versions": "1.9, 1.10, 1.11, 1.12, 1.13, 1.14",
"go_default": "1.14"
}, },
"sensitive-variables": ["install_password", "ssh_password", "client_secret", "github_feed_token"], "sensitive-variables": ["install_password", "ssh_password", "client_secret", "github_feed_token"],
"builders": [ "builders": [
@@ -317,8 +319,8 @@
{ {
"type": "powershell", "type": "powershell",
"environment_vars": [ "environment_vars": [
"GO_VERSIONS=1.10.8,1.11.12,1.12.7,1.13", "GO_VERSIONS={{user `go_versions`}}",
"GO_DEFAULT=1.12.7" "GO_DEFAULT={{user `go_default`}}"
], ],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Install-Go.ps1" "{{ template_dir }}/scripts/Installers/Install-Go.ps1"
@@ -638,8 +640,8 @@
{ {
"type": "powershell", "type": "powershell",
"environment_vars": [ "environment_vars": [
"GO_VERSIONS=1.10.8,1.11.12,1.12.7,1.13", "GO_VERSIONS={{user `go_versions`}}",
"GO_DEFAULT=1.12.7" "GO_DEFAULT={{user `go_default`}}"
], ],
"scripts":[ "scripts":[
"{{ template_dir }}/scripts/Installers/Validate-Go.ps1" "{{ template_dir }}/scripts/Installers/Validate-Go.ps1"

View File

@@ -5,6 +5,7 @@
Import-Module -Name ImageHelpers -Force Import-Module -Name ImageHelpers -Force
$refsJson = Invoke-RestMethod "https://api.github.com/repos/golang/go/git/refs/tags"
function Install-GoVersion function Install-GoVersion
{ {
Param Param
@@ -13,17 +14,20 @@ function Install-GoVersion
[Switch]$addToDefaultPath [Switch]$addToDefaultPath
) )
$latestVersionObject = $refsJson | Where-Object { $_.ref -Match "refs/tags/go$goVersion[./d]*" } | Select-Object -Last 1
$latestVersion = $latestVersionObject.ref.replace('refs/tags/go','')
# Download the Go zip archive. # Download the Go zip archive.
Write-Host "Downloading Go $goVersion..." Write-Host "Downloading Go $latestVersion..."
$ProgressPreference = 'SilentlyContinue' $ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -UseBasicParsing -Uri "https://dl.google.com/go/go$goVersion.windows-amd64.zip" -OutFile go$goVersion.windows-amd64.zip Invoke-WebRequest -UseBasicParsing -Uri "https://dl.google.com/go/go$latestVersion.windows-amd64.zip" -OutFile go$latestVersion.windows-amd64.zip
# Extract the zip archive. It contains a single directory named "go". # Extract the zip archive. It contains a single directory named "go".
Write-Host "Extracting Go $goVersion..." Write-Host "Extracting Go $latestVersion..."
Expand-Archive -Path go$goVersion.windows-amd64.zip -DestinationPath "C:\" -Force Expand-Archive -Path go$latestVersion.windows-amd64.zip -DestinationPath "C:\" -Force
# Delete unnecessary files to conserve space # Delete unnecessary files to conserve space
Write-Host "Cleaning directories of Go $goVersion..." Write-Host "Cleaning directories of Go $latestVersion..."
if (Test-Path "C:\go\doc") if (Test-Path "C:\go\doc")
{ {
Remove-Item -Recurse -Force "C:\go\doc" Remove-Item -Recurse -Force "C:\go\doc"
@@ -34,17 +38,17 @@ function Install-GoVersion
} }
# Rename the extracted "go" directory to include the Go version number (to support side-by-side versions of Go). # Rename the extracted "go" directory to include the Go version number (to support side-by-side versions of Go).
$newDirName = "Go$goVersion" $newDirName = "Go$latestVersion"
Rename-Item -path "C:\go" -newName $newDirName Rename-Item -path "C:\go" -newName $newDirName
# Delete the Go zip archive. # Delete the Go zip archive.
Write-Host "Deleting downloaded archive of Go $goVersion..." Write-Host "Deleting downloaded archive of Go $latestVersion..."
Remove-Item go$goVersion.windows-a`md64.zip Remove-Item go$latestVersion.windows-amd64.zip
# Make this the default version of Go? # Make this the default version of Go?
if ($addToDefaultPath) if ($addToDefaultPath)
{ {
Write-Host "Adding Go $goVersion to the path..." Write-Host "Adding Go $latestVersion to the path..."
# Add the Go binaries to the path. # Add the Go binaries to the path.
Add-MachinePathItem "C:\$newDirName\bin" | Out-Null Add-MachinePathItem "C:\$newDirName\bin" | Out-Null
# Set the GOROOT environment variable. # Set the GOROOT environment variable.
@@ -52,12 +56,12 @@ function Install-GoVersion
} }
# Done # Done
Write-Host "Done installing Go $goVersion." Write-Host "Done installing Go $latestVersion."
return "C:\$newDirName" return "C:\$newDirName"
} }
# Install Go # Install Go
$goVersionsToInstall = $env:GO_VERSIONS.split(",") $goVersionsToInstall = $env:GO_VERSIONS.split(", ", [System.StringSplitOptions]::RemoveEmptyEntries)
foreach($go in $goVersionsToInstall) { foreach($go in $goVersionsToInstall) {
Write-Host "Installing Go ${go}" Write-Host "Installing Go ${go}"
@@ -66,6 +70,6 @@ foreach($go in $goVersionsToInstall) {
} else { } else {
$installDirectory = Install-GoVersion -goVersion $go $installDirectory = Install-GoVersion -goVersion $go
} }
$envName = "GOROOT_{0}_{1}_X64" -f $go.split(".") $envName = "GOROOT_{0}_{1}_X64" -f $go.split(".")
setx $envName "$installDirectory" /M setx $envName "$installDirectory" /M
} }

View File

@@ -8,18 +8,31 @@ function Get-GoVersion
{ {
Param Param
( (
[String]$goRootPath [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
$env:Path = "$goRootPath\bin;" + $env:Path $env:Path = "$goPath\bin;" + $env:Path
if( $(go version) -match 'go version go(?<version>.*) win.*' ) $version = $(go version)
$matchVersion = $version -match $goVersion
$semanticEquality = $version -match 'go version go(?<version>.*) win.*'
if($semanticEquality -And $matchVersion)
{ {
$goVersion = $Matches.version $goFullVersion = $Matches.version
return $goVersion Write-Host "$goFullVersion has been found"
}
Write-Host "Unable to determine Go version at " + $goRootPath return $goFullVersion
return "" }
else
{
Write-Host "Unable to determine Go version at " + $goPath
exit 1
}
} }
# Verify that go.exe is on the path # Verify that go.exe is on the path
@@ -53,10 +66,10 @@ _Environment:_
$SoftwareName = "Go (x64)" $SoftwareName = "Go (x64)"
$Description = New-Object System.Text.StringBuilder $Description = New-Object System.Text.StringBuilder
$goVersionsToInstall = $env:GO_VERSIONS.split(",") $goVersionsToInstall = $env:GO_VERSIONS.split(", ", [System.StringSplitOptions]::RemoveEmptyEntries)
foreach($go in $goVersionsToInstall) { foreach($go in $goVersionsToInstall) {
$goVersion = Get-GoVersion -goRootPath "C:\Go${go}" $goVersion = Get-GoVersion -goVersion $go
$goVersionTag = "GOROOT_{0}_{1}_X64" -f $go.split(".") $goVersionTag = "GOROOT_{0}_{1}_X64" -f $go.split(".")
if ($goVersion -eq $go) { if ($goVersion -eq $go) {
if($go -eq $env:GO_DEFAULT) { if($go -eq $env:GO_DEFAULT) {