From 6fd6c5ccbbadf01a07708383b8291b764fac2ff7 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 5 Mar 2023 14:31:10 +0100 Subject: [PATCH 1/3] Use the default tool cache path on self-hosted runners matching a GH-hosted runner image * See https://github.com/ruby/setup-ruby/issues/475 * Semantically reverts 377a94bc686cee73294e510c187e34175a2fb643 --- common.js | 24 ++++++++++++++---------- dist/index.js | 27 ++++++++++++++++----------- ruby-builder.js | 2 +- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/common.js b/common.js index 8fe2529f0..2a84ae448 100644 --- a/common.js +++ b/common.js @@ -166,17 +166,14 @@ const GitHubHostedPlatforms = [ 'windows-2022-x64', ] -// Actually a self-hosted runner for which either -// * the OS and OS version does not correspond to a GitHub-hosted runner image, -// * or the hosted tool cache is different from the default tool cache path +// Actually a self-hosted runner for which the OS and OS version does not correspond to a GitHub-hosted runner image, export function isSelfHostedRunner() { if (inputs.selfHosted === undefined) { throw new Error('inputs.selfHosted should have been already set') } return inputs.selfHosted === 'true' || - !GitHubHostedPlatforms.includes(getOSNameVersionArch()) || - getRunnerToolCache() !== getDefaultToolCachePath() + !GitHubHostedPlatforms.includes(getOSNameVersionArch()) } export function selfHostedRunnerReason() { @@ -184,8 +181,6 @@ export function selfHostedRunnerReason() { return 'the self-hosted input was set' } else if (!GitHubHostedPlatforms.includes(getOSNameVersionArch())) { return 'the platform does not match a GitHub-hosted runner image (or that image is deprecated and no longer supported)' - } else if (getRunnerToolCache() !== getDefaultToolCachePath()) { - return 'the $RUNNER_TOOL_CACHE is different than the default tool cache path (they must be the same to reuse prebuilt Ruby binaries)' } else { return 'unknown reason' } @@ -237,6 +232,16 @@ export function shouldUseToolCache(engine, version) { return (engine === 'ruby' && !isHeadVersion(version)) || isSelfHostedRunner() } +export function getToolCachePath() { + if (isSelfHostedRunner()) { + return getRunnerToolCache() + } else { + // Rubies prebuilt by this action embed this path rather than using $RUNNER_TOOL_CACHE + // so use that path is not isSelfHostedRunner() + return getDefaultToolCachePath() + } +} + export function getRunnerToolCache() { const runnerToolCache = process.env['RUNNER_TOOL_CACHE'] if (!runnerToolCache) { @@ -245,8 +250,7 @@ export function getRunnerToolCache() { return runnerToolCache } -// Rubies prebuilt by this action embed this path rather than using $RUNNER_TOOL_CACHE, -// so they can only be used if the two paths are the same +// Rubies prebuilt by this action embed this path rather than using $RUNNER_TOOL_CACHE function getDefaultToolCachePath() { const platform = getVirtualEnvironmentName() if (platform.startsWith('ubuntu-')) { @@ -261,7 +265,7 @@ function getDefaultToolCachePath() { } export function getToolCacheRubyPrefix(platform, engine, version) { - const toolCache = getRunnerToolCache() + const toolCache = getToolCachePath() const name = { ruby: 'Ruby', jruby: 'JRuby', diff --git a/dist/index.js b/dist/index.js index 1312e715b..d5af80dbd 100644 --- a/dist/index.js +++ b/dist/index.js @@ -292,6 +292,7 @@ __nccwpck_require__.d(__webpack_exports__, { "floatVersion": () => (/* binding */ floatVersion), "getOSNameVersionArch": () => (/* binding */ getOSNameVersionArch), "getRunnerToolCache": () => (/* binding */ getRunnerToolCache), + "getToolCachePath": () => (/* binding */ getToolCachePath), "getToolCacheRubyPrefix": () => (/* binding */ getToolCacheRubyPrefix), "getVirtualEnvironmentName": () => (/* binding */ getVirtualEnvironmentName), "hasBundlerDefaultGem": () => (/* binding */ hasBundlerDefaultGem), @@ -521,17 +522,14 @@ const GitHubHostedPlatforms = [ 'windows-2022-x64', ] -// Actually a self-hosted runner for which either -// * the OS and OS version does not correspond to a GitHub-hosted runner image, -// * or the hosted tool cache is different from the default tool cache path +// Actually a self-hosted runner for which the OS and OS version does not correspond to a GitHub-hosted runner image, function isSelfHostedRunner() { if (inputs.selfHosted === undefined) { throw new Error('inputs.selfHosted should have been already set') } return inputs.selfHosted === 'true' || - !GitHubHostedPlatforms.includes(getOSNameVersionArch()) || - getRunnerToolCache() !== getDefaultToolCachePath() + !GitHubHostedPlatforms.includes(getOSNameVersionArch()) } function selfHostedRunnerReason() { @@ -539,8 +537,6 @@ function selfHostedRunnerReason() { return 'the self-hosted input was set' } else if (!GitHubHostedPlatforms.includes(getOSNameVersionArch())) { return 'the platform does not match a GitHub-hosted runner image (or that image is deprecated and no longer supported)' - } else if (getRunnerToolCache() !== getDefaultToolCachePath()) { - return 'the $RUNNER_TOOL_CACHE is different than the default tool cache path (they must be the same to reuse prebuilt Ruby binaries)' } else { return 'unknown reason' } @@ -592,6 +588,16 @@ function shouldUseToolCache(engine, version) { return (engine === 'ruby' && !isHeadVersion(version)) || isSelfHostedRunner() } +function getToolCachePath() { + if (isSelfHostedRunner()) { + return getRunnerToolCache() + } else { + // Rubies prebuilt by this action embed this path rather than using $RUNNER_TOOL_CACHE + // so use that path is not isSelfHostedRunner() + return getDefaultToolCachePath() + } +} + function getRunnerToolCache() { const runnerToolCache = process.env['RUNNER_TOOL_CACHE'] if (!runnerToolCache) { @@ -600,8 +606,7 @@ function getRunnerToolCache() { return runnerToolCache } -// Rubies prebuilt by this action embed this path rather than using $RUNNER_TOOL_CACHE, -// so they can only be used if the two paths are the same +// Rubies prebuilt by this action embed this path rather than using $RUNNER_TOOL_CACHE function getDefaultToolCachePath() { const platform = getVirtualEnvironmentName() if (platform.startsWith('ubuntu-')) { @@ -616,7 +621,7 @@ function getDefaultToolCachePath() { } function getToolCacheRubyPrefix(platform, engine, version) { - const toolCache = getRunnerToolCache() + const toolCache = getToolCachePath() const name = { ruby: 'Ruby', jruby: 'JRuby', @@ -68283,7 +68288,7 @@ async function install(platform, engine, version) { if (common.isSelfHostedRunner()) { const rubyBuildDefinition = engine === 'ruby' ? version : `${engine}-${version}` core.error( - `The current runner (${common.getOSNameVersionArch()}, RUNNER_TOOL_CACHE=${common.getRunnerToolCache()}) was detected as self-hosted because ${common.selfHostedRunnerReason()}.\n` + + `The current runner (${common.getOSNameVersionArch()}) was detected as self-hosted because ${common.selfHostedRunnerReason()}.\n` + `In such a case, you should install Ruby in the $RUNNER_TOOL_CACHE yourself, for example using https://github.com/rbenv/ruby-build\n` + `You can take inspiration from this workflow for more details: https://github.com/ruby/ruby-builder/blob/master/.github/workflows/build.yml\n` + `$ ruby-build ${rubyBuildDefinition} ${toolCacheRubyPrefix}\n` + diff --git a/ruby-builder.js b/ruby-builder.js index 60388fde0..3281fe71d 100644 --- a/ruby-builder.js +++ b/ruby-builder.js @@ -28,7 +28,7 @@ export async function install(platform, engine, version) { if (common.isSelfHostedRunner()) { const rubyBuildDefinition = engine === 'ruby' ? version : `${engine}-${version}` core.error( - `The current runner (${common.getOSNameVersionArch()}, RUNNER_TOOL_CACHE=${common.getRunnerToolCache()}) was detected as self-hosted because ${common.selfHostedRunnerReason()}.\n` + + `The current runner (${common.getOSNameVersionArch()}) was detected as self-hosted because ${common.selfHostedRunnerReason()}.\n` + `In such a case, you should install Ruby in the $RUNNER_TOOL_CACHE yourself, for example using https://github.com/rbenv/ruby-build\n` + `You can take inspiration from this workflow for more details: https://github.com/ruby/ruby-builder/blob/master/.github/workflows/build.yml\n` + `$ ruby-build ${rubyBuildDefinition} ${toolCacheRubyPrefix}\n` + From bfbb10f5cd01c869e0c12491f683559281a9127c Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 5 Mar 2023 14:37:22 +0100 Subject: [PATCH 2/3] Fix tc.find() calls to use the proper name on self-hosted --- common.js | 11 +++++++---- dist/index.js | 16 ++++++++++------ ruby-builder.js | 2 +- windows.js | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/common.js b/common.js index 2a84ae448..dc71bc03d 100644 --- a/common.js +++ b/common.js @@ -264,15 +264,18 @@ function getDefaultToolCachePath() { } } -export function getToolCacheRubyPrefix(platform, engine, version) { - const toolCache = getToolCachePath() - const name = { +export function engineToToolCacheName(engine) { + return { ruby: 'Ruby', jruby: 'JRuby', truffleruby: 'TruffleRuby', "truffleruby+graalvm": 'TruffleRubyGraalVM' }[engine] - return path.join(toolCache, name, version, os.arch()) +} + +export function getToolCacheRubyPrefix(platform, engine, version) { + const toolCache = getToolCachePath() + return path.join(toolCache, engineToToolCacheName(engine), version, os.arch()) } export function toolCacheCompleteFile(toolCacheRubyPrefix) { diff --git a/dist/index.js b/dist/index.js index d5af80dbd..c2a54b51a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -289,6 +289,7 @@ __nccwpck_require__.r(__webpack_exports__); __nccwpck_require__.d(__webpack_exports__, { "createToolCacheCompleteFile": () => (/* binding */ createToolCacheCompleteFile), "drive": () => (/* binding */ drive), + "engineToToolCacheName": () => (/* binding */ engineToToolCacheName), "floatVersion": () => (/* binding */ floatVersion), "getOSNameVersionArch": () => (/* binding */ getOSNameVersionArch), "getRunnerToolCache": () => (/* binding */ getRunnerToolCache), @@ -620,15 +621,18 @@ function getDefaultToolCachePath() { } } -function getToolCacheRubyPrefix(platform, engine, version) { - const toolCache = getToolCachePath() - const name = { +function engineToToolCacheName(engine) { + return { ruby: 'Ruby', jruby: 'JRuby', truffleruby: 'TruffleRuby', "truffleruby+graalvm": 'TruffleRubyGraalVM' }[engine] - return path.join(toolCache, name, version, os.arch()) +} + +function getToolCacheRubyPrefix(platform, engine, version) { + const toolCache = getToolCachePath() + return path.join(toolCache, engineToToolCacheName(engine), version, os.arch()) } function toolCacheCompleteFile(toolCacheRubyPrefix) { @@ -68280,7 +68284,7 @@ function getAvailableVersions(platform, engine) { async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find('Ruby', version) + inToolCache = tc.find(common.engineToToolCacheName(engine), version) if (inToolCache) { rubyPrefix = inToolCache } else { @@ -68494,7 +68498,7 @@ async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find('Ruby', version) + inToolCache = tc.find(common.engineToToolCacheName(engine), version) if (inToolCache) { rubyPrefix = inToolCache } else { diff --git a/ruby-builder.js b/ruby-builder.js index 3281fe71d..8dd71c588 100644 --- a/ruby-builder.js +++ b/ruby-builder.js @@ -20,7 +20,7 @@ export function getAvailableVersions(platform, engine) { export async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find('Ruby', version) + inToolCache = tc.find(common.engineToToolCacheName(engine), version) if (inToolCache) { rubyPrefix = inToolCache } else { diff --git a/windows.js b/windows.js index 13a382639..d827071c7 100644 --- a/windows.js +++ b/windows.js @@ -49,7 +49,7 @@ export async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find('Ruby', version) + inToolCache = tc.find(common.engineToToolCacheName(engine), version) if (inToolCache) { rubyPrefix = inToolCache } else { From af3f62747fa238d18546ae65649658bb4e22f55f Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 5 Mar 2023 14:42:52 +0100 Subject: [PATCH 3/3] Use tc.find() with the effective tool cache path * So it finds already-download prebuilt Ruby in the GH-hosted tool cache path on self-hosted, where $RUNNER_TOOL_CACHE is often different than on GH-hosted. * See #475. * This relies on tc.find() looking up the env var every time: https://github.com/actions/toolkit/blob/1f4b3fac06/packages/tool-cache/src/tool-cache.ts#L522 --- common.js | 14 +++++++++++++- dist/index.js | 18 +++++++++++++++--- ruby-builder.js | 2 +- windows.js | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/common.js b/common.js index dc71bc03d..a28f8390b 100644 --- a/common.js +++ b/common.js @@ -5,6 +5,7 @@ const util = require('util') const stream = require('stream') const crypto = require('crypto') const core = require('@actions/core') +const tc = require('@actions/tool-cache') const { performance } = require('perf_hooks') const linuxOSInfo = require('linux-os-info') import macosRelease from 'macos-release' @@ -264,7 +265,18 @@ function getDefaultToolCachePath() { } } -export function engineToToolCacheName(engine) { +// tc.find() but using RUNNER_TOOL_CACHE=getToolCachePath() +export function toolCacheFind(engine, version) { + const originalToolCache = getToolCachePath() + process.env['RUNNER_TOOL_CACHE'] = getToolCachePath() + try { + return tc.find(engineToToolCacheName(engine), version) + } finally { + process.env['RUNNER_TOOL_CACHE'] = originalToolCache + } +} + +function engineToToolCacheName(engine) { return { ruby: 'Ruby', jruby: 'JRuby', diff --git a/dist/index.js b/dist/index.js index c2a54b51a..48ccb6648 100644 --- a/dist/index.js +++ b/dist/index.js @@ -289,7 +289,6 @@ __nccwpck_require__.r(__webpack_exports__); __nccwpck_require__.d(__webpack_exports__, { "createToolCacheCompleteFile": () => (/* binding */ createToolCacheCompleteFile), "drive": () => (/* binding */ drive), - "engineToToolCacheName": () => (/* binding */ engineToToolCacheName), "floatVersion": () => (/* binding */ floatVersion), "getOSNameVersionArch": () => (/* binding */ getOSNameVersionArch), "getRunnerToolCache": () => (/* binding */ getRunnerToolCache), @@ -313,6 +312,7 @@ __nccwpck_require__.d(__webpack_exports__, { "targetRubyVersion": () => (/* binding */ targetRubyVersion), "time": () => (/* binding */ time), "toolCacheCompleteFile": () => (/* binding */ toolCacheCompleteFile), + "toolCacheFind": () => (/* binding */ toolCacheFind), "win2nix": () => (/* binding */ win2nix), "windows": () => (/* binding */ windows) }); @@ -362,6 +362,7 @@ const util = __nccwpck_require__(3837) const stream = __nccwpck_require__(2781) const common_crypto = __nccwpck_require__(6113) const core = __nccwpck_require__(2186) +const tc = __nccwpck_require__(7784) const { performance } = __nccwpck_require__(4074) const linuxOSInfo = __nccwpck_require__(8487) ; @@ -621,6 +622,17 @@ function getDefaultToolCachePath() { } } +// tc.find() but using RUNNER_TOOL_CACHE=getToolCachePath() +function toolCacheFind(engine, version) { + const originalToolCache = getToolCachePath() + process.env['RUNNER_TOOL_CACHE'] = getToolCachePath() + try { + return tc.find(engineToToolCacheName(engine), version) + } finally { + process.env['RUNNER_TOOL_CACHE'] = originalToolCache + } +} + function engineToToolCacheName(engine) { return { ruby: 'Ruby', @@ -68284,7 +68296,7 @@ function getAvailableVersions(platform, engine) { async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find(common.engineToToolCacheName(engine), version) + inToolCache = common.toolCacheFind(engine, version) if (inToolCache) { rubyPrefix = inToolCache } else { @@ -68498,7 +68510,7 @@ async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find(common.engineToToolCacheName(engine), version) + inToolCache = common.toolCacheFind(engine, version) if (inToolCache) { rubyPrefix = inToolCache } else { diff --git a/ruby-builder.js b/ruby-builder.js index 8dd71c588..9f5b143c5 100644 --- a/ruby-builder.js +++ b/ruby-builder.js @@ -20,7 +20,7 @@ export function getAvailableVersions(platform, engine) { export async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find(common.engineToToolCacheName(engine), version) + inToolCache = common.toolCacheFind(engine, version) if (inToolCache) { rubyPrefix = inToolCache } else { diff --git a/windows.js b/windows.js index d827071c7..e831cb4e2 100644 --- a/windows.js +++ b/windows.js @@ -49,7 +49,7 @@ export async function install(platform, engine, version) { let rubyPrefix, inToolCache if (common.shouldUseToolCache(engine, version)) { - inToolCache = tc.find(common.engineToToolCacheName(engine), version) + inToolCache = common.toolCacheFind(engine, version) if (inToolCache) { rubyPrefix = inToolCache } else {