Skip to content

Commit

Permalink
Cache .rye and remove os version dependency
Browse files Browse the repository at this point in the history
To speed up runs on hosted runners this will also cache the .rye directory. Also remove the os version from the cache key to be more robust. This especially fixes issues with macos14.
  • Loading branch information
eifinger committed Mar 1, 2024
1 parent 9d83a60 commit 358888c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 113 deletions.
76 changes: 39 additions & 37 deletions dist/save-cache/index.js

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

2 changes: 1 addition & 1 deletion dist/save-cache/index.js.map

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions dist/setup/index.js

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

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

45 changes: 22 additions & 23 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ import * as glob from '@actions/glob'
import * as core from '@actions/core'
import {cp} from '@actions/io/'
import {exists} from '@actions/io/lib/io-util'
import {getArch} from './utils'

import {IS_MAC, getLinuxInfo, getMacOSInfo} from './utils'

export const STATE_CACHE_PRIMARY_KEY = 'cache-primary-key'
export const STATE_CACHE_KEY = 'cache-key'
export const STATE_CACHE_MATCHED_KEY = 'cache-matched-key'
const CACHE_VERSION = '1'
const CACHE_DEPENDENCY_PATH = 'requirements**.lock'
const workingDirInput = core.getInput('working-directory')
const workingDir = workingDirInput ? `/${workingDirInput}` : ''
const cachePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`
export const workingDirInput = core.getInput('working-directory')
export const workingDir = workingDirInput ? `/${workingDirInput}` : ''
export const venvPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.venv`
export const ryeHomePath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/.rye`
const CACHE_VERSION = '2'
const cacheLocalStoragePath =
`${core.getInput('cache-local-storage-path')}` || ''
const cacheDependencyPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/${CACHE_DEPENDENCY_PATH}`
const cacheDependencyPath = `${process.env['GITHUB_WORKSPACE']}${workingDir}/requirements**.lock`

export async function restoreCache(
cachePrefix: string,
version: string
): Promise<void> {
const {primaryKey, restoreKey} = await computeKeys(cachePrefix, version)
if (primaryKey.endsWith('-')) {
const cacheKey = await computeKeys(cachePrefix, version)
if (cacheKey.endsWith('-')) {
throw new Error(
`No file in ${process.cwd()} matched to [${cacheDependencyPath}], make sure you have checked out the target repository`
)
Expand All @@ -32,34 +31,30 @@ export async function restoreCache(
let matchedKey: string | undefined
try {
matchedKey = cacheLocalStoragePath
? await restoreCacheLocal(primaryKey)
: await cache.restoreCache([cachePath], primaryKey, [restoreKey])
? await restoreCacheLocal(cacheKey)
: await cache.restoreCache([venvPath, ryeHomePath], cacheKey)
} catch (err) {
const message = (err as Error).message
core.warning(message)
core.setOutput('cache-hit', false)
return
}

core.saveState(STATE_CACHE_PRIMARY_KEY, primaryKey)
core.saveState(STATE_CACHE_KEY, cacheKey)

handleMatchResult(matchedKey, primaryKey)
handleMatchResult(matchedKey, cacheKey)
}

async function computeKeys(
cachePrefix: string,
version: string
): Promise<{primaryKey: string; restoreKey: string}> {
core.debug(`Computing cache key for ${cacheDependencyPath}`)
const dependencyPathHash = await glob.hashFiles(cacheDependencyPath)
): Promise<string> {
const cacheDependencyPathHash = await glob.hashFiles(cacheDependencyPath)
const workingDirHash = workingDir
? `-${crypto.createHash('sha256').update(workingDir).digest('hex')}`
: ''
const osInfo = IS_MAC ? await getMacOSInfo() : await getLinuxInfo()
const prefix = cachePrefix ? `${cachePrefix}-` : ''
const primaryKey = `${prefix}setup-rye-${CACHE_VERSION}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}${workingDirHash}-${dependencyPathHash}`
const restoreKey = `${prefix}setup-rye-${CACHE_VERSION}-${osInfo.osVersion}-${osInfo.osName}-rye-${version}${workingDirHash}`
return {primaryKey, restoreKey}
return `${prefix}setup-rye-${CACHE_VERSION}-${process.env['RUNNER_OS']}-${getArch()}-rye-${version}${workingDirHash}-${cacheDependencyPathHash}`
}

function handleMatchResult(
Expand All @@ -83,7 +78,11 @@ async function restoreCacheLocal(
core.info(`Local cache is not found: ${storedCache}`)
return
}
await cp(storedCache, cachePath, {
await cp(`${storedCache}/.venv`, venvPath, {
copySourceDirectory: false,
recursive: true
})
await cp(`${storedCache}/.rye`, ryeHomePath, {
copySourceDirectory: false,
recursive: true
})
Expand Down
Loading

0 comments on commit 358888c

Please sign in to comment.