feat: add support for saving / restoring rtx cache (#64)

* feat: add support for saving / restoring rtx cache

* fix: add support for .rtx.toml to file hash
This commit is contained in:
Nick Hehr 2023-04-10 20:36:00 -04:00 committed by GitHub
parent 6596ddda8c
commit 801b2f548d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 116720 additions and 176 deletions

36
src/cache-save.ts Normal file
View file

@ -0,0 +1,36 @@
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import * as fs from 'fs'
import {rtxDir} from './utils'
export async function run(): Promise<void> {
try {
await cacheRTXTools()
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
}
async function cacheRTXTools(): Promise<void> {
const state = core.getState('CACHE_KEY')
const primaryKey = core.getState('PRIMARY_KEY')
const cachePath = rtxDir()
if (!fs.existsSync(cachePath)) {
throw new Error(`Cache folder path does not exist on disk: ${cachePath}`)
}
if (primaryKey === state) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
)
return
}
const cacheId = await cache.saveCache([cachePath], primaryKey)
if (cacheId === -1) return
core.info(`Cache saved with the primary key: ${primaryKey}`)
}
run()

View file

@ -1,17 +1,40 @@
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as glob from '@actions/glob'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import {rtxDir} from './utils'
async function run(): Promise<void> {
await setToolVersions()
await restoreRTXCache()
await setupRTX()
await exec.exec('rtx', ['--version'])
await setToolVersions()
await exec.exec('rtx', ['install'])
await setPaths()
}
async function restoreRTXCache(): Promise<void> {
const cachePath = rtxDir()
const fileHash = await glob.hashFiles(`**/.tool-versions\n**/.rtx.toml`)
const primaryKey = `rtx-tools-${getOS()}-${os.arch()}-${fileHash}`
core.saveState('PRIMARY_KEY', primaryKey)
const cacheKey = await cache.restoreCache([cachePath], primaryKey)
core.setOutput('cache-hit', Boolean(cacheKey))
if (!cacheKey) {
core.info(`rtx cache not found for ${getOS()}-${os.arch()} tool versions`)
return
}
core.saveState('CACHE_KEY', cacheKey)
core.info(`rtx cache restored from key: ${cacheKey}`)
}
async function setupRTX(): Promise<void> {
const rtxBinDir = path.join(rtxDir(), 'bin')
const url = `https://rtx.pub/rtx-latest-${getOS()}-${os.arch()}`
@ -53,16 +76,6 @@ async function getBinPaths(): Promise<string[]> {
return output.stdout.split('\n')
}
function rtxDir(): string {
if (process.env.RTX_DATA_HOME) {
return process.env.RTX_DATA_HOME
}
if (process.env.XDG_DATA_HOME) {
return path.join(process.env.XDG_DATA_HOME, 'rtx')
}
return path.join(os.homedir(), '.local/share/rtx')
}
if (require.main === module) {
try {
run()

12
src/utils.ts Normal file
View file

@ -0,0 +1,12 @@
import * as os from 'os'
import * as path from 'path'
export function rtxDir(): string {
if (process.env.RTX_DATA_HOME) {
return process.env.RTX_DATA_HOME
}
if (process.env.XDG_DATA_HOME) {
return path.join(process.env.XDG_DATA_HOME, 'rtx')
}
return path.join(os.homedir(), '.local/share/rtx')
}