Skip to content

Commit

Permalink
Make local cache optional
Browse files Browse the repository at this point in the history
- Added 'use-local-cache' option so we can control the use of the local cache
- Also move the file to GitHub cache instead of copying to save space
  • Loading branch information
Jimver committed Jan 6, 2024
1 parent a59fc63 commit e7ed264
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ inputs:
description: 'Use GitHub cache to cache downloaded installer on GitHub servers'
required: false
default: 'true'
use-local-cache:
description: 'Use local cache to cache downloaded installer on the local runner'
required: false
default: 'true'
runs:
using: 'node16'
main: dist/index.js
Expand Down
96 changes: 53 additions & 43 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,83 @@ import {getLinks} from './links/get-links'
export async function download(
version: SemVer,
method: Method,
useLocalCache: boolean,
useGitHubCache: boolean
): Promise<string> {
// First try to find tool with desired version in tool cache (local to machine)
const toolName = 'cuda_installer'
const osType = await getOs()
const osRelease = await getRelease()
const toolId = `${toolName}-${osType}-${osRelease}`
const toolPath = tc.find(toolId, `${version}`)
// Path that contains the executable file
let executablePath: string
if (toolPath) {
// Tool is already in cache
core.debug(`Found in local machine cache ${toolPath}`)
executablePath = toolPath
} else {
let executableDirectory: string | undefined
const cacheKey = `${toolId}-${version}`
const cacheDirectory = cacheKey
if (useLocalCache) {
const toolPath = tc.find(toolId, `${version}`)
if (toolPath) {
// Tool is already in cache
core.debug(`Found in local machine cache ${toolPath}`)
executableDirectory = toolPath
}
}
if (executableDirectory === undefined && useGitHubCache) {
// Second option, get tool from GitHub cache if enabled
const cacheKey = `${toolId}-${version}`
const cachePath = cacheKey
let cacheResult: string | undefined
if (useGitHubCache) {
cacheResult = await cache.restoreCache([cachePath], cacheKey)
}
cacheResult = await cache.restoreCache([cacheDirectory], cacheKey)
if (cacheResult !== undefined) {
core.debug(`Found in GitHub cache ${cachePath}`)
executablePath = cachePath
} else {
// Final option, download tool from NVIDIA servers
core.debug(`Not found in local/GitHub cache, downloading...`)
// Get download URL
const url: URL = await getDownloadURL(method, version)
// Get intsaller filename extension depending on OS
const fileExtension: string = getFileExtension(osType)
const destFileName = `${toolId}_${version}.${fileExtension}`
// Download executable
const downloadPath: string = await tc.downloadTool(
url.toString(),
destFileName
)
// Copy file to GitHub cachePath
core.debug(`Copying ${destFileName} to ${cachePath}`)
await io.mkdirP(cachePath)
await io.cp(destFileName, cachePath)
core.debug(`Found in GitHub cache ${cacheDirectory}`)
executableDirectory = cacheDirectory
}
}
if (executableDirectory === undefined) {
// Final option, download tool from NVIDIA servers
core.debug(`Not found in local/GitHub cache, downloading...`)
// Get download URL
const url: URL = await getDownloadURL(method, version)
// Get intsaller filename extension depending on OS
const fileExtension: string = getFileExtension(osType)
const destFileName = `${toolId}_${version}.${fileExtension}`
// Download executable
const downloadPath: string = await tc.downloadTool(
url.toString(),
destFileName
)
if (useLocalCache) {
// Cache download to local machine cache
const localCachePath = await tc.cacheFile(
const localCacheDirectory = await tc.cacheFile(
downloadPath,
destFileName,
`${toolName}-${osType}`,
`${version}`
)
core.debug(`Cached download to local machine cache at ${localCachePath}`)
// Cache download to GitHub cache if enabled
if (useGitHubCache) {
const cacheId = await cache.saveCache([cachePath], cacheKey)
if (cacheId !== -1) {
core.debug(`Cached download to GitHub cache with cache id ${cacheId}`)
} else {
core.debug(`Did not cache, cache possibly already exists`)
}
core.debug(
`Cached download to local machine cache at ${localCacheDirectory}`
)
executableDirectory = localCacheDirectory
}
if (useGitHubCache) {
// Move file to GitHub cache directory
core.debug(`Copying ${destFileName} to ${cacheDirectory}`)
await io.mkdirP(cacheDirectory)
await io.mv(destFileName, cacheDirectory)
// Save cache directory to GitHub cache
const cacheId = await cache.saveCache([cacheDirectory], cacheKey)
if (cacheId !== -1) {
core.debug(`Cached download to GitHub cache with cache id ${cacheId}`)
} else {
core.debug(`Did not cache, cache possibly already exists`)
}
executablePath = localCachePath
core.debug(`Tool was moved to cache directory ${cacheDirectory}`)
executableDirectory = cacheDirectory
}
}
core.debug(`Executable path ${executableDirectory}`)
// String with full executable path
let fullExecutablePath: string
// Get list of files in tool cache
const filesInCache = await (
await glob.create(`${executablePath}/**.*`)
await glob.create(`${executableDirectory}/**.*`)
).glob()
core.debug(`Files in tool cache:`)
for (const f of filesInCache) {
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ async function run(): Promise<void> {
core.debug(`Desired local linux args: ${linuxLocalArgs}`)
const useGitHubCache: boolean = core.getBooleanInput('use-github-cache')
core.debug(`Desired GitHub cache usage: ${useGitHubCache}`)
const useLocalCache: boolean = core.getBooleanInput('use-local-cache')
core.debug(`Desired local cache usage: ${useLocalCache}`)

// Parse subPackages array
const subPackagesArray: string[] = await parsePackages(
Expand Down Expand Up @@ -83,6 +85,7 @@ async function run(): Promise<void> {
const executablePath: string = await download(
version,
methodParsed,
useLocalCache,
useGitHubCache
)

Expand Down

0 comments on commit e7ed264

Please sign in to comment.