diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index 1f52d22e..3f10d70b 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -16,10 +16,8 @@ Export-ModuleMember -Function @( 'Set-SystemVariable' 'Install-Binary' 'Install-VisualStudio' - 'Get-ToolcachePackages' 'Get-ToolsetContent' 'Get-ToolsetToolFullPath' - 'Get-ToolsByName' 'Stop-SvcWithErrHandling' 'Set-SvcWithErrHandling' 'Start-DownloadWithRetry' diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index aed13b99..a28273c2 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -286,12 +286,6 @@ function Get-VSExtensionVersion return $packageVersion } -function Get-ToolcachePackages -{ - $toolcachePath = Join-Path $env:ROOT_FOLDER "toolcache.json" - Get-Content -Raw $toolcachePath | ConvertFrom-Json -} - function Get-ToolsetContent { $toolsetJson = Get-Content -Path $env:TOOLSET_JSON_PATH -Raw @@ -350,24 +344,6 @@ function Get-ToolsetToolFullPath return Join-Path $foundVersion $Arch } -function Get-ToolsByName -{ - Param - ( - [Parameter(Mandatory = $True)] - [string]$SoftwareName - ) - - (Get-ToolcachePackages).PSObject.Properties | Where-Object { $_.Name -match $SoftwareName } | ForEach-Object { - $packageNameParts = $_.Name.Split("-") - [PSCustomObject] @{ - ToolName = $packageNameParts[1] - Versions = $_.Value - Architecture = $packageNameParts[3,4] -join "-" - } - } -} - function Get-WinVersion { (Get-CimInstance -ClassName Win32_OperatingSystem).Caption diff --git a/images/win/scripts/Installers/Download-ToolCache.ps1 b/images/win/scripts/Installers/Download-ToolCache.ps1 deleted file mode 100644 index 8656fb6a..00000000 --- a/images/win/scripts/Installers/Download-ToolCache.ps1 +++ /dev/null @@ -1,92 +0,0 @@ -################################################################################ -## File: Download-ToolCache.ps1 -## Team: CI-Build -## Desc: Download tool cache -################################################################################ - -Function Install-NpmPackage { - [CmdletBinding()] - param( - [Parameter(Mandatory=$true)] - [System.String] $PackageName, - [Parameter(Mandatory=$true)] - [System.Uri] $FeedPrefix - ) - - Push-Location -Path $env:TEMP - - $FeedUri = $FeedPrefix.AbsoluteUri - - Write-Host "Installing npm $PackageName from ${FeedUri}" - npm install $PackageName --registry "${FeedUri}" - - if($LASTEXITCODE) { - Write-Host "$PackageName installation failure; Error: ${LASTEXITCODE}" - - exit 1 - } - - Pop-Location -} - -Function NPMFeed-AuthSetup { - param( - [Parameter(Mandatory=$true)] - [System.String] $AccessToken, - [Parameter(Mandatory=$true)] - [System.Uri] $FeedPrefix - ) - - $FeedHost = $FeedPrefix.Host - - Write-Host "Configure auth for github package registry" - $npmrcContent = "//${FeedHost}/:_authToken=${AccessToken}" - $npmrcContent | Out-File -FilePath "$($env:TEMP)/.npmrc" -Encoding utf8 -} - -Function Set-DefaultRubyVersion { - param( - [Parameter(Mandatory=$true)] - [System.Version] $Version, - [System.String] $Arch = "x64" - ) - $rubyPath = $Env:AGENT_TOOLSDIRECTORY + "/Ruby/${Version}*/${Arch}/bin" - $rubyDir = Get-Item -Path $rubyPath - - Write-Host "Use Ruby ${Version} as a system Ruby" - Add-MachinePathItem -PathItem $rubyDir.FullName - - $env:Path = Get-MachinePath - - # Update ruby gem to latest version - Invoke-Expression "gem update --system" -} - -$FeedPrefix = "https://npm.pkg.github.com" -$AccessToken = $env:GITHUB_FEED_TOKEN - -# HostedToolCache Path -$Dest = "C:/" -$Path = "hostedtoolcache/windows" -$ToolsDirectory = $Dest + $Path - -# Define AGENT_TOOLSDIRECTORY environment variable -$env:AGENT_TOOLSDIRECTORY = $ToolsDirectory -setx AGENT_TOOLSDIRECTORY $ToolsDirectory /M - -# Install HostedToolCache tools via NPM -$ToolVersionsFileContent = Get-Content -Path "$env:ROOT_FOLDER/toolcache.json" -Raw -$ToolVersions = ConvertFrom-Json -InputObject $ToolVersionsFileContent - -NPMFeed-AuthSetup -AccessToken $AccessToken -FeedPrefix $FeedPrefix - -$ToolVersions.PSObject.Properties | ForEach-Object { - $PackageName = $_.Name - $PackageVersions = $_.Value - $NpmPackages = $PackageVersions | ForEach-Object { "$PackageName@$_" } - foreach($NpmPackage in $NpmPackages) { - Install-NpmPackage -PackageName $NpmPackage -FeedPrefix $FeedPrefix - } -} - -Set-DefaultRubyVersion -Version "2.5" \ No newline at end of file diff --git a/images/win/scripts/Installers/Install-Ruby.ps1 b/images/win/scripts/Installers/Install-Ruby.ps1 new file mode 100644 index 00000000..e2b1763f --- /dev/null +++ b/images/win/scripts/Installers/Install-Ruby.ps1 @@ -0,0 +1,141 @@ +################################################################################ +## File: Install-Ruby.ps1 +## Desc: Install rubyinstaller2 +################################################################################ + +function Get-RubyVersions +{ + param ( + [System.String] $Arch = "x64", + [System.String] $Extension = "7z" + ) + + $uri = "https://api.github.com/repos/oneclick/rubyinstaller2/releases" + try + { + $versionLists = @{} + $assets = (Invoke-RestMethod -Uri $uri).Where{ -not $_.prerelease }.assets + $7zArchives = $assets.Where{ $_.name.EndsWith("$Arch.$Extension") } + $majorMinorGroups = $7zArchives | Group-Object { $_.name.Replace("rubyinstaller-", "").Substring(0, 3) } + foreach($majorMinorGroup in $majorMinorGroups) + { + $group = $majorMinorGroup.Group + $sortVersions = $group | Sort-Object {[Version]$_.name.Replace("rubyinstaller-", "").Replace("-$Arch.$Extension","").Replace("-",".")} + $latestVersion = $sortVersions | Select-Object -Last 1 + $versionLists[$majorMinorGroup.Name] = $latestVersion.browser_download_url + + } + return $versionLists + } + catch + { + Write-Host "Enable to send request to the '$uri'. Error: '$_'" + exit 1 + } +} + +# Most of this logic is from +# https://github.com/MSP-Greg/actions-ruby/blob/master/lib/main.js +function Install-Ruby +{ + param( + [String]$PackagePath, + [String]$Architecture = "x64" + ) + + # Expand archive with binaries + $packageName = [IO.Path]::GetFileNameWithoutExtension((Split-Path -Path $PackagePath -Leaf)) + $tempFolder = Join-Path -Path $env:TEMP -ChildPath $packageName + Extract-7Zip -Path $PackagePath -DestinationPath $env:TEMP + + # Get Ruby version from binaries + $rubyVersion = & "$tempFolder\bin\ruby.exe" -e "print RUBY_VERSION" + + if ($rubyVersion) + { + Write-Host "Installing Ruby $rubyVersion" + $rubyToolcachePath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Ruby" + $rubyVersionPath = Join-Path -Path $rubyToolcachePath -ChildPath $rubyVersion + $rubyArchPath = Join-Path -Path $rubyVersionPath -ChildPath $Architecture + + if (-not (Test-Path $rubyToolcachePath)) + { + Write-Host "Create Ruby toolcache folder" + New-Item -ItemType Directory -Path $rubyToolcachePath | Out-Null + } + + Write-Host "Creating Ruby '${rubyVersion}' folder in '${rubyVersionPath}'" + New-Item -ItemType Directory -Path $rubyVersionPath -Force | Out-Null + + Write-Host "Moving Ruby '${rubyVersion}' files to '${rubyArchPath}'" + Move-Item -Path $tempFolder -Destination $rubyArchPath | Out-Null + + Write-Host "Installing Bundler" + cmd.exe /c "$rubyArchPath\bin\gem.cmd install bundler -v ""~> 2"" --no-document" + + if ($LASTEXITCODE -ne 0) + { + Throw "Error happened during Ruby installation" + exit 1 + } + + Write-Host "Creating complete file" + New-Item -ItemType File -Path $rubyVersionPath -Name "$Architecture.complete" | Out-Null + } + else + { + Write-Host "Ruby application is not found. Failed to expand '$PackagePath' archive" + exit 1 + } +} + +function Set-DefaultRubyVersion +{ + param( + [Parameter(Mandatory=$true)] + [System.Version] $Version, + [System.String] $Arch = "x64" + ) + + $rubyPath = Join-Path $env:AGENT_TOOLSDIRECTORY "/Ruby/${Version}*/${Arch}/bin" + $rubyDir = (Get-Item -Path $rubyPath).FullName + + Write-Host "Use Ruby ${Version} as a system Ruby" + Add-MachinePathItem -PathItem $rubyDir | Out-Null + $env:Path = Get-MachinePath + + # Update ruby gem to latest version + Invoke-Expression "gem update --system --no-document" +} + +# Define AGENT_TOOLSDIRECTORY environment variable +$toolsDirectory = "C:/hostedtoolcache/windows" +$env:AGENT_TOOLSDIRECTORY = $toolsDirectory +setx AGENT_TOOLSDIRECTORY $toolsDirectory /M + +# Install Ruby +$rubyTools = (Get-ToolsetContent).toolcache | Where-Object { $_.name -eq "Ruby" } +$rubyToolVersions = $rubyTools.versions + +# Get Ruby versions from the repo +$rubyLatestMajorVersions = Get-RubyVersions + +Write-Host "Starting installation Ruby..." +foreach($rubyVersion in $rubyToolVersions) +{ + Write-Host "Starting Ruby $rubyVersion installation" + # Get url for the latest major Ruby version + $url = $rubyLatestMajorVersions[$rubyVersion] + if ($url) + { + $tempRubyPackagePath = Start-DownloadWithRetry -Url $url + Install-Ruby -PackagePath $tempRubyPackagePath + } + else + { + Write-Host "Url not found for the '$rubyVersion' version" + exit 1 + } +} + +Set-DefaultRubyVersion -Version $rubyTools.default -Arch $rubyTools.arch \ No newline at end of file diff --git a/images/win/scripts/Tests/Toolset.Tests.ps1 b/images/win/scripts/Tests/Toolset.Tests.ps1 index 0116ab22..70acfc16 100644 --- a/images/win/scripts/Tests/Toolset.Tests.ps1 +++ b/images/win/scripts/Tests/Toolset.Tests.ps1 @@ -62,12 +62,6 @@ function Test-DefaultVersion { } $tools = Get-ToolsetContent | Select-Object -ExpandProperty toolcache -# convert old tool-cache to toolset format on-flight to re-use code -(Get-ToolcachePackages).PSObject.Properties | ForEach-Object { - $packageNameParts = $_.Name.Split("-") - $tools += @{ name = $packageNameParts[1]; arch = $packageNameParts[3]; versions = $_.Value; default = "." } -} - foreach ($tool in $tools) { Describe "$($tool.name) [$($tool.arch)]" { $toolExecs = Get-ToolExecutables -Name $tool.name diff --git a/images/win/toolsets/toolcache-2016.json b/images/win/toolsets/toolcache-2016.json deleted file mode 100644 index b3f9c3c0..00000000 --- a/images/win/toolsets/toolcache-2016.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "@actions/toolcache-ruby-windows-x64": [ - "2.4", "2.5", "2.6", "2.7" - ] -} \ No newline at end of file diff --git a/images/win/toolsets/toolcache-2019.json b/images/win/toolsets/toolcache-2019.json deleted file mode 100644 index b3f9c3c0..00000000 --- a/images/win/toolsets/toolcache-2019.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "@actions/toolcache-ruby-windows-x64": [ - "2.4", "2.5", "2.6", "2.7" - ] -} \ No newline at end of file diff --git a/images/win/toolsets/toolset-2016.json b/images/win/toolsets/toolset-2016.json index f5d30694..7e730911 100644 --- a/images/win/toolsets/toolset-2016.json +++ b/images/win/toolsets/toolset-2016.json @@ -1,5 +1,17 @@ { "toolcache": [ + { + "name": "Ruby", + "arch": "x64", + "platform" : "win32", + "versions": [ + "2.4", + "2.5", + "2.6", + "2.7" + ], + "default": "2.5" + }, { "name": "Python", "url" : "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json", diff --git a/images/win/toolsets/toolset-2019.json b/images/win/toolsets/toolset-2019.json index ebb7aa82..4b1c5328 100644 --- a/images/win/toolsets/toolset-2019.json +++ b/images/win/toolsets/toolset-2019.json @@ -1,5 +1,17 @@ { "toolcache": [ + { + "name": "Ruby", + "arch": "x64", + "platform" : "win32", + "versions": [ + "2.4", + "2.5", + "2.6", + "2.7" + ], + "default": "2.5" + }, { "name": "Python", "url" : "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json", diff --git a/images/win/windows2016.json b/images/win/windows2016.json index 120e7ac9..ed3bf71d 100644 --- a/images/win/windows2016.json +++ b/images/win/windows2016.json @@ -26,13 +26,11 @@ "capture_name_prefix": "packer", "image_version": "dev", "image_os": "win16", - "github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}", "announcements": "{{env `ANNOUNCEMENTS`}}" }, "sensitive-variables": [ "install_password", - "client_secret", - "github_feed_token" + "client_secret" ], "builders": [ { @@ -92,11 +90,6 @@ "source": "{{ template_dir }}/scripts/Tests", "destination": "{{user `image_folder`}}" }, - { - "type": "file", - "source": "{{template_dir}}/toolsets/toolcache-2016.json", - "destination": "{{user `root_folder`}}/toolcache.json" - }, { "type": "file", "source": "{{template_dir}}/toolsets/toolset-2016.json", @@ -226,12 +219,11 @@ { "type": "powershell", "environment_vars": [ - "GITHUB_FEED_TOKEN={{ user `github_feed_token` }}", "TOOLSET_JSON_PATH={{user `toolset_json_path`}}", "ROOT_FOLDER={{user `root_folder`}}" ], "scripts": [ - "{{ template_dir }}/scripts/Installers/Download-ToolCache.ps1", + "{{ template_dir }}/scripts/Installers/Install-Ruby.ps1", "{{ template_dir }}/scripts/Installers/Install-PyPy.ps1", "{{ template_dir }}/scripts/Installers/Install-Toolset.ps1", "{{ template_dir }}/scripts/Installers/Configure-Toolset.ps1", diff --git a/images/win/windows2019.json b/images/win/windows2019.json index a27bf27d..756391ed 100644 --- a/images/win/windows2019.json +++ b/images/win/windows2019.json @@ -26,13 +26,11 @@ "capture_name_prefix": "packer", "image_version": "dev", "image_os": "win19", - "github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}", "announcements": "{{env `ANNOUNCEMENTS`}}" }, "sensitive-variables": [ "install_password", - "client_secret", - "github_feed_token" + "client_secret" ], "builders": [ { @@ -92,11 +90,6 @@ "source": "{{ template_dir }}/scripts/Tests", "destination": "{{user `image_folder`}}" }, - { - "type": "file", - "source": "{{template_dir}}/toolsets/toolcache-2019.json", - "destination": "{{user `root_folder`}}/toolcache.json" - }, { "type": "file", "source": "{{template_dir}}/toolsets/toolset-2019.json", @@ -225,11 +218,10 @@ { "type": "powershell", "environment_vars": [ - "GITHUB_FEED_TOKEN={{ user `github_feed_token` }}", "ROOT_FOLDER={{user `root_folder`}}" ], "scripts": [ - "{{ template_dir }}/scripts/Installers/Download-ToolCache.ps1" + "{{ template_dir }}/scripts/Installers/Install-Ruby.ps1" ] }, {