From e7ed264386bf4b7ed4e689efcb23ab47beebfcdc Mon Sep 17 00:00:00 2001 From: Jim Verheijde Date: Sat, 6 Jan 2024 17:20:09 +0100 Subject: [PATCH] Make local cache optional - 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 --- action.yml | 4 ++ src/downloader.ts | 96 ++++++++++++++++++++++++++--------------------- src/main.ts | 3 ++ 3 files changed, 60 insertions(+), 43 deletions(-) diff --git a/action.yml b/action.yml index 4bd43a7a..1f8fff4c 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/downloader.ts b/src/downloader.ts index 9c3ee190..0557d101 100644 --- a/src/downloader.ts +++ b/src/downloader.ts @@ -15,6 +15,7 @@ import {getLinks} from './links/get-links' export async function download( version: SemVer, method: Method, + useLocalCache: boolean, useGitHubCache: boolean ): Promise { // First try to find tool with desired version in tool cache (local to machine) @@ -22,66 +23,75 @@ export async function download( 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) { diff --git a/src/main.ts b/src/main.ts index a55650db..09725dea 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,6 +24,8 @@ async function run(): Promise { 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( @@ -83,6 +85,7 @@ async function run(): Promise { const executablePath: string = await download( version, methodParsed, + useLocalCache, useGitHubCache )