Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Consider a different $RUNNER_TOOL_CACHE than the default as a self-hosted runner and add self-hosted input #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ inputs:
description: |
Arbitrary string that will be added to the cache key of the bundler cache. Set or change it if you need
to invalidate the cache.
self-hosted:
description: |
Consider the runner as a self-hosted runner, which means not using prebuilt Ruby binaries which only work
on GitHub-hosted runners or self-hosted runners with a very similar image to the ones used by GitHub runners.
The default is to detect this automatically based on the OS, OS version and $RUNNER_TOOL_CACHE.
outputs:
ruby-prefix:
description: 'The prefix of the installed ruby'
Expand Down
51 changes: 39 additions & 12 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const windows = (os.platform() === 'win32')
// Extract to SSD on Windows, see https://github.com/ruby/setup-ruby/pull/14
export const drive = (windows ? (process.env['GITHUB_WORKSPACE'] || 'C')[0] : undefined)

export const inputs = {
selfHosted: undefined
}

export function partition(string, separator) {
const i = string.indexOf(separator)
if (i === -1) {
Expand Down Expand Up @@ -162,9 +166,29 @@ 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())
if (inputs.selfHosted === undefined) {
throw new Error('inputs.selfHosted should have been already set')
}

return inputs.selfHosted === 'true' ||
!GitHubHostedPlatforms.includes(getOSNameVersionArch()) ||
getRunnerToolCache() !== getDefaultToolCachePath()
}

export function selfHostedRunnerReason() {
if (inputs.selfHosted === 'true') {
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'
}
}

let virtualEnvironmentName = undefined
Expand Down Expand Up @@ -213,28 +237,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',
Expand Down
60 changes: 46 additions & 14 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const inputDefaults = {
'bundler-cache': 'false',
'working-directory': '.',
'cache-version': bundler.DEFAULT_CACHE_VERSION,
'self-hosted': 'false',
}

// entry point when this action is run on its own
Expand All @@ -39,6 +40,7 @@ export async function setupRuby(options = {}) {
inputs[key] = core.getInput(key) || inputDefaults[key]
}
}
common.inputs.selfHosted = inputs['self-hosted']

process.chdir(inputs['working-directory'])

Expand Down
4 changes: 2 additions & 2 deletions ruby-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ 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` +
`In such a case, you should install Ruby in the $RUNNER_TOOL_CACHE yourself, for example using https://github.com/rbenv/ruby-build:\n` +
`The current runner (${common.getOSNameVersionArch()}, RUNNER_TOOL_CACHE=${common.getRunnerToolCache()}) 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` +
`Once that completes successfully, mark it as complete with:\n` +
Expand Down