rework ruby installation

This commit is contained in:
Aleksandr Chebotov
2020-11-12 20:09:32 +03:00
parent ec9befb26a
commit fe3f5efbe8
11 changed files with 169 additions and 154 deletions

View File

@@ -16,10 +16,8 @@ Export-ModuleMember -Function @(
'Set-SystemVariable' 'Set-SystemVariable'
'Install-Binary' 'Install-Binary'
'Install-VisualStudio' 'Install-VisualStudio'
'Get-ToolcachePackages'
'Get-ToolsetContent' 'Get-ToolsetContent'
'Get-ToolsetToolFullPath' 'Get-ToolsetToolFullPath'
'Get-ToolsByName'
'Stop-SvcWithErrHandling' 'Stop-SvcWithErrHandling'
'Set-SvcWithErrHandling' 'Set-SvcWithErrHandling'
'Start-DownloadWithRetry' 'Start-DownloadWithRetry'

View File

@@ -286,12 +286,6 @@ function Get-VSExtensionVersion
return $packageVersion return $packageVersion
} }
function Get-ToolcachePackages
{
$toolcachePath = Join-Path $env:ROOT_FOLDER "toolcache.json"
Get-Content -Raw $toolcachePath | ConvertFrom-Json
}
function Get-ToolsetContent function Get-ToolsetContent
{ {
$toolsetJson = Get-Content -Path $env:TOOLSET_JSON_PATH -Raw $toolsetJson = Get-Content -Path $env:TOOLSET_JSON_PATH -Raw
@@ -350,24 +344,6 @@ function Get-ToolsetToolFullPath
return Join-Path $foundVersion $Arch 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 function Get-WinVersion
{ {
(Get-CimInstance -ClassName Win32_OperatingSystem).Caption (Get-CimInstance -ClassName Win32_OperatingSystem).Caption

View File

@@ -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"

View File

@@ -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

View File

@@ -62,12 +62,6 @@ function Test-DefaultVersion {
} }
$tools = Get-ToolsetContent | Select-Object -ExpandProperty toolcache $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) { foreach ($tool in $tools) {
Describe "$($tool.name) [$($tool.arch)]" { Describe "$($tool.name) [$($tool.arch)]" {
$toolExecs = Get-ToolExecutables -Name $tool.name $toolExecs = Get-ToolExecutables -Name $tool.name

View File

@@ -1,5 +0,0 @@
{
"@actions/toolcache-ruby-windows-x64": [
"2.4", "2.5", "2.6", "2.7"
]
}

View File

@@ -1,5 +0,0 @@
{
"@actions/toolcache-ruby-windows-x64": [
"2.4", "2.5", "2.6", "2.7"
]
}

View File

@@ -1,5 +1,17 @@
{ {
"toolcache": [ "toolcache": [
{
"name": "Ruby",
"arch": "x64",
"platform" : "win32",
"versions": [
"2.4",
"2.5",
"2.6",
"2.7"
],
"default": "2.5"
},
{ {
"name": "Python", "name": "Python",
"url" : "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json", "url" : "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",

View File

@@ -1,5 +1,17 @@
{ {
"toolcache": [ "toolcache": [
{
"name": "Ruby",
"arch": "x64",
"platform" : "win32",
"versions": [
"2.4",
"2.5",
"2.6",
"2.7"
],
"default": "2.5"
},
{ {
"name": "Python", "name": "Python",
"url" : "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json", "url" : "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",

View File

@@ -26,13 +26,11 @@
"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`}}",
"announcements": "{{env `ANNOUNCEMENTS`}}" "announcements": "{{env `ANNOUNCEMENTS`}}"
}, },
"sensitive-variables": [ "sensitive-variables": [
"install_password", "install_password",
"client_secret", "client_secret"
"github_feed_token"
], ],
"builders": [ "builders": [
{ {
@@ -92,11 +90,6 @@
"source": "{{ template_dir }}/scripts/Tests", "source": "{{ template_dir }}/scripts/Tests",
"destination": "{{user `image_folder`}}" "destination": "{{user `image_folder`}}"
}, },
{
"type": "file",
"source": "{{template_dir}}/toolsets/toolcache-2016.json",
"destination": "{{user `root_folder`}}/toolcache.json"
},
{ {
"type": "file", "type": "file",
"source": "{{template_dir}}/toolsets/toolset-2016.json", "source": "{{template_dir}}/toolsets/toolset-2016.json",
@@ -226,12 +219,11 @@
{ {
"type": "powershell", "type": "powershell",
"environment_vars": [ "environment_vars": [
"GITHUB_FEED_TOKEN={{ user `github_feed_token` }}",
"TOOLSET_JSON_PATH={{user `toolset_json_path`}}", "TOOLSET_JSON_PATH={{user `toolset_json_path`}}",
"ROOT_FOLDER={{user `root_folder`}}" "ROOT_FOLDER={{user `root_folder`}}"
], ],
"scripts": [ "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-PyPy.ps1",
"{{ template_dir }}/scripts/Installers/Install-Toolset.ps1", "{{ template_dir }}/scripts/Installers/Install-Toolset.ps1",
"{{ template_dir }}/scripts/Installers/Configure-Toolset.ps1", "{{ template_dir }}/scripts/Installers/Configure-Toolset.ps1",

View File

@@ -26,13 +26,11 @@
"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`}}",
"announcements": "{{env `ANNOUNCEMENTS`}}" "announcements": "{{env `ANNOUNCEMENTS`}}"
}, },
"sensitive-variables": [ "sensitive-variables": [
"install_password", "install_password",
"client_secret", "client_secret"
"github_feed_token"
], ],
"builders": [ "builders": [
{ {
@@ -92,11 +90,6 @@
"source": "{{ template_dir }}/scripts/Tests", "source": "{{ template_dir }}/scripts/Tests",
"destination": "{{user `image_folder`}}" "destination": "{{user `image_folder`}}"
}, },
{
"type": "file",
"source": "{{template_dir}}/toolsets/toolcache-2019.json",
"destination": "{{user `root_folder`}}/toolcache.json"
},
{ {
"type": "file", "type": "file",
"source": "{{template_dir}}/toolsets/toolset-2019.json", "source": "{{template_dir}}/toolsets/toolset-2019.json",
@@ -225,11 +218,10 @@
{ {
"type": "powershell", "type": "powershell",
"environment_vars": [ "environment_vars": [
"GITHUB_FEED_TOKEN={{ user `github_feed_token` }}",
"ROOT_FOLDER={{user `root_folder`}}" "ROOT_FOLDER={{user `root_folder`}}"
], ],
"scripts": [ "scripts": [
"{{ template_dir }}/scripts/Installers/Download-ToolCache.ps1" "{{ template_dir }}/scripts/Installers/Install-Ruby.ps1"
] ]
}, },
{ {