From 377a94bc686cee73294e510c187e34175a2fb643 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 3 Mar 2023 16:59:46 +0100 Subject: [PATCH] Consider a different $RUNNER_TOOL_CACHE than the default as a self-hosted runner * Fixes https://github.com/ruby/setup-ruby/issues/242 --- common.js | 30 ++++++++++++++++++------------ dist/index.js | 33 ++++++++++++++++++++------------- ruby-builder.js | 2 +- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/common.js b/common.js index e1de569..5ec89a1 100644 --- a/common.js +++ b/common.js @@ -162,9 +162,12 @@ const GitHubHostedPlatforms = [ 'windows-2022-x64', ] -// Actually a self-hosted runner which does not correspond to a GitHub-hosted runner image +// 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 export function isSelfHostedRunner() { - return !GitHubHostedPlatforms.includes(getOSNameVersionArch()) + return !GitHubHostedPlatforms.includes(getOSNameVersionArch()) || + getRunnerToolCache() !== getDefaultToolCachePath() } let virtualEnvironmentName = undefined @@ -213,28 +216,31 @@ export function shouldUseToolCache(engine, version) { return (engine === 'ruby' && !isHeadVersion(version)) || isSelfHostedRunner() } -function getPlatformToolCache(platform) { - if (isSelfHostedRunner()) { - const runnerToolCache = process.env['RUNNER_TOOL_CACHE'] - if (!runnerToolCache) { - throw new Error('$RUNNER_TOOL_CACHE must be set on self-hosted runners') - } - return runnerToolCache +export function getRunnerToolCache() { + const runnerToolCache = process.env['RUNNER_TOOL_CACHE'] + if (!runnerToolCache) { + throw new Error('$RUNNER_TOOL_CACHE must be set') } - // Hardcode paths rather than using $RUNNER_TOOL_CACHE because the prebuilt Rubies cannot be moved anyway + 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 +function getDefaultToolCachePath() { + const platform = getVirtualEnvironmentName() if (platform.startsWith('ubuntu-')) { return '/opt/hostedtoolcache' } else if (platform.startsWith('macos-')) { return '/Users/runner/hostedtoolcache' } else if (platform.startsWith('windows-')) { - return 'C:/hostedtoolcache/windows' + return 'C:\\hostedtoolcache\\windows' } else { throw new Error('Unknown platform') } } export function getToolCacheRubyPrefix(platform, engine, version) { - const toolCache = getPlatformToolCache(platform) + const toolCache = getRunnerToolCache() const name = { ruby: 'Ruby', jruby: 'JRuby', diff --git a/dist/index.js b/dist/index.js index 21810ea..977d2e6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -291,6 +291,7 @@ __nccwpck_require__.d(__webpack_exports__, { "drive": () => (/* binding */ drive), "floatVersion": () => (/* binding */ floatVersion), "getOSNameVersionArch": () => (/* binding */ getOSNameVersionArch), + "getRunnerToolCache": () => (/* binding */ getRunnerToolCache), "getToolCacheRubyPrefix": () => (/* binding */ getToolCacheRubyPrefix), "getVirtualEnvironmentName": () => (/* binding */ getVirtualEnvironmentName), "hasBundlerDefaultGem": () => (/* binding */ hasBundlerDefaultGem), @@ -514,9 +515,12 @@ const GitHubHostedPlatforms = [ 'windows-2022-x64', ] -// Actually a self-hosted runner which does not correspond to a GitHub-hosted runner image +// 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 function isSelfHostedRunner() { - return !GitHubHostedPlatforms.includes(getOSNameVersionArch()) + return !GitHubHostedPlatforms.includes(getOSNameVersionArch()) || + getRunnerToolCache() !== getDefaultToolCachePath() } let virtualEnvironmentName = undefined @@ -565,28 +569,31 @@ function shouldUseToolCache(engine, version) { return (engine === 'ruby' && !isHeadVersion(version)) || isSelfHostedRunner() } -function getPlatformToolCache(platform) { - if (isSelfHostedRunner()) { - const runnerToolCache = process.env['RUNNER_TOOL_CACHE'] - if (!runnerToolCache) { - throw new Error('$RUNNER_TOOL_CACHE must be set on self-hosted runners') - } - return runnerToolCache +function getRunnerToolCache() { + const runnerToolCache = process.env['RUNNER_TOOL_CACHE'] + if (!runnerToolCache) { + throw new Error('$RUNNER_TOOL_CACHE must be set') } - // Hardcode paths rather than using $RUNNER_TOOL_CACHE because the prebuilt Rubies cannot be moved anyway + 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 +function getDefaultToolCachePath() { + const platform = getVirtualEnvironmentName() if (platform.startsWith('ubuntu-')) { return '/opt/hostedtoolcache' } else if (platform.startsWith('macos-')) { return '/Users/runner/hostedtoolcache' } else if (platform.startsWith('windows-')) { - return 'C:/hostedtoolcache/windows' + return 'C:\\hostedtoolcache\\windows' } else { throw new Error('Unknown platform') } } function getToolCacheRubyPrefix(platform, engine, version) { - const toolCache = getPlatformToolCache(platform) + const toolCache = getRunnerToolCache() const name = { ruby: 'Ruby', jruby: 'JRuby', @@ -68253,7 +68260,7 @@ async function install(platform, engine, version) { if (common.isSelfHostedRunner()) { const rubyBuildDefinition = engine === 'ruby' ? version : `${engine}-${version}` core.error( - `The current runner (${common.getOSNameVersionArch()}) was detected as self-hosted and not matching a GitHub-hosted runner image.\n` + + `The current runner (${common.getOSNameVersionArch()}, RUNNER_TOOL_CACHE=${common.getRunnerToolCache()}) was detected as self-hosted and not matching a GitHub-hosted runner image.\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 4b124a3..917ca06 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()}) was detected as self-hosted and not matching a GitHub-hosted runner image.\n` + + `The current runner (${common.getOSNameVersionArch()}, RUNNER_TOOL_CACHE=${common.getRunnerToolCache()}) was detected as self-hosted and not matching a GitHub-hosted runner image.\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` +