From dca64a28b3a50a3f881b452cbaa655e24b38b43c Mon Sep 17 00:00:00 2001 From: Niklas Gehlen Date: Thu, 26 Sep 2024 12:38:43 +0200 Subject: [PATCH] Suggest in error how to configure caching. (#7) * Suggest in error how to configure caching. * rebuild * handle exceptions nicely. --- dist/index/index.js | 102 ++++++++++++++++++++++++---------------- src/index.ts | 112 ++++++++++++++++++++++++++------------------ 2 files changed, 127 insertions(+), 87 deletions(-) diff --git a/dist/index/index.js b/dist/index/index.js index 9378a11..8fe3a3f 100644 --- a/dist/index/index.js +++ b/dist/index/index.js @@ -27230,54 +27230,74 @@ const Output_CacheHit = "cache-hit"; const ActionVersion = "nscloud-action-cache@v1"; void main(); async function main() { - const localCachePath = process.env[Env_CacheRoot]; - if (localCachePath == null) { - throw new Error(`Local cache path not found. - -Did you configure the Namespace cross-invocation cache? https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume + try { + const localCachePath = process.env[Env_CacheRoot]; + if (localCachePath == null) { + let hint = `Please update your \x1b[1mruns-on\x1b[0m labels. E.g.: + +\x1b[32mruns-on\x1b[34m:\x1b[0m + - \x1b[34mnscloud-ubuntu-22.04-amd64-8x16-\x1b[1mwith-cache\x1b[0m + - \x1b[34m\x1b[1mnscloud-cache-size-50gb\x1b[0m + - \x1b[34m\x1b[1mnscloud-cache-tag-my-cache-key\x1b[0m + +You can replace \x1b[1mmy-cache-key\x1b[0m with something that represents what you’re storing in the cache.`; + if (process.env.NSC_RUNNER_PROFILE_INFO) { + hint = "Please enable \x1b[1mCaching\x1b[0m in your runner profile."; + } + throw new Error(`nscloud-cache-action requires a cache volume to be configured. + +${hint} + +See also https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume Are you running in a container? Check out https://namespace.so/docs/actions/nscloud-cache-action#advanced-running-github-jobs-in-containers`); - } - core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`); - const cachePaths = await resolveCachePaths(localCachePath); - const cacheMisses = await restoreLocalCache(cachePaths); - const fullHit = cacheMisses.length === 0; - core.setOutput(Output_CacheHit, fullHit.toString()); - if (!fullHit) { - core.info(`Some cache paths missing: ${cacheMisses}.`); - const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss); - if (failOnCacheMiss) { - throw new Error(`Some cache paths missing: ${cacheMisses}.`); } - } - else { - core.info("All cache paths found and restored."); - } - try { - // Write/update cache volume metadata file - const metadata = ensureCacheMetadata(localCachePath); - metadata.updatedAt = new Date().toISOString(); - metadata.version = 1; - if (!metadata.userRequest) { - metadata.userRequest = {}; + core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`); + const cachePaths = await resolveCachePaths(localCachePath); + const cacheMisses = await restoreLocalCache(cachePaths); + const fullHit = cacheMisses.length === 0; + core.setOutput(Output_CacheHit, fullHit.toString()); + if (!fullHit) { + core.info(`Some cache paths missing: ${cacheMisses}.`); + const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss); + if (failOnCacheMiss) { + throw new Error(`Some cache paths missing: ${cacheMisses}.`); + } } - for (const p of cachePaths) { - metadata.userRequest[p.pathInCache] = { - cacheFramework: p.framework, - mountTarget: [p.mountTarget], - source: ActionVersion, - }; + else { + core.info("All cache paths found and restored."); + } + try { + // Write/update cache volume metadata file + const metadata = ensureCacheMetadata(localCachePath); + metadata.updatedAt = new Date().toISOString(); + metadata.version = 1; + if (!metadata.userRequest) { + metadata.userRequest = {}; + } + for (const p of cachePaths) { + metadata.userRequest[p.pathInCache] = { + cacheFramework: p.framework, + mountTarget: [p.mountTarget], + source: ActionVersion, + }; + } + writeCacheMetadata(localCachePath, metadata); + } + catch (e) { + core.warning("Failed to record cache metadata."); + core.info(e.message); } - writeCacheMetadata(localCachePath, metadata); + // Save the list of cache paths to actions state for the post-cache action + core.saveState(StatePathsKey, cachePaths); + const cacheUtilInfo = await getCacheSummaryUtil(localCachePath); + core.info(`Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.`); } - catch (e) { - core.warning("Failed to record cache metadata."); - core.info(e.message); + catch (error) { + // Fail the workflow run if an error occurs + if (error instanceof Error) + core.setFailed(error.message); } - // Save the list of cache paths to actions state for the post-cache action - core.saveState(StatePathsKey, cachePaths); - const cacheUtilInfo = await getCacheSummaryUtil(localCachePath); - core.info(`Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.`); } async function restoreLocalCache(cachePaths) { const cacheMisses = []; diff --git a/src/index.ts b/src/index.ts index 4d2e51d..c34719a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,64 +15,84 @@ const ActionVersion = "nscloud-action-cache@v1"; void main(); async function main() { - const localCachePath = process.env[utils.Env_CacheRoot]; - if (localCachePath == null) { - throw new Error( - `Local cache path not found. - -Did you configure the Namespace cross-invocation cache? https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume + try { + const localCachePath = process.env[utils.Env_CacheRoot]; + if (localCachePath == null) { + let hint = `Please update your \x1b[1mruns-on\x1b[0m labels. E.g.: + +\x1b[32mruns-on\x1b[34m:\x1b[0m + - \x1b[34mnscloud-ubuntu-22.04-amd64-8x16-\x1b[1mwith-cache\x1b[0m + - \x1b[34m\x1b[1mnscloud-cache-size-50gb\x1b[0m + - \x1b[34m\x1b[1mnscloud-cache-tag-my-cache-key\x1b[0m + +You can replace \x1b[1mmy-cache-key\x1b[0m with something that represents what you’re storing in the cache.`; + + if (process.env.NSC_RUNNER_PROFILE_INFO) { + hint = "Please enable \x1b[1mCaching\x1b[0m in your runner profile."; + } + + throw new Error( + `nscloud-cache-action requires a cache volume to be configured. + +${hint} + +See also https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume Are you running in a container? Check out https://namespace.so/docs/actions/nscloud-cache-action#advanced-running-github-jobs-in-containers` - ); - } - core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`); + ); + } + core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`); - const cachePaths = await resolveCachePaths(localCachePath); - const cacheMisses = await restoreLocalCache(cachePaths); + const cachePaths = await resolveCachePaths(localCachePath); + const cacheMisses = await restoreLocalCache(cachePaths); - const fullHit = cacheMisses.length === 0; - core.setOutput(Output_CacheHit, fullHit.toString()); + const fullHit = cacheMisses.length === 0; + core.setOutput(Output_CacheHit, fullHit.toString()); - if (!fullHit) { - core.info(`Some cache paths missing: ${cacheMisses}.`); + if (!fullHit) { + core.info(`Some cache paths missing: ${cacheMisses}.`); - const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss); - if (failOnCacheMiss) { - throw new Error(`Some cache paths missing: ${cacheMisses}.`); + const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss); + if (failOnCacheMiss) { + throw new Error(`Some cache paths missing: ${cacheMisses}.`); + } + } else { + core.info("All cache paths found and restored."); } - } else { - core.info("All cache paths found and restored."); - } - try { - // Write/update cache volume metadata file - const metadata = utils.ensureCacheMetadata(localCachePath); - metadata.updatedAt = new Date().toISOString(); - metadata.version = 1; - if (!metadata.userRequest) { - metadata.userRequest = {}; - } + try { + // Write/update cache volume metadata file + const metadata = utils.ensureCacheMetadata(localCachePath); + metadata.updatedAt = new Date().toISOString(); + metadata.version = 1; + if (!metadata.userRequest) { + metadata.userRequest = {}; + } - for (const p of cachePaths) { - metadata.userRequest[p.pathInCache] = { - cacheFramework: p.framework, - mountTarget: [p.mountTarget], - source: ActionVersion, - }; + for (const p of cachePaths) { + metadata.userRequest[p.pathInCache] = { + cacheFramework: p.framework, + mountTarget: [p.mountTarget], + source: ActionVersion, + }; + } + utils.writeCacheMetadata(localCachePath, metadata); + } catch (e) { + core.warning("Failed to record cache metadata."); + core.info(e.message); } - utils.writeCacheMetadata(localCachePath, metadata); - } catch (e) { - core.warning("Failed to record cache metadata."); - core.info(e.message); - } - // Save the list of cache paths to actions state for the post-cache action - core.saveState(utils.StatePathsKey, cachePaths); + // Save the list of cache paths to actions state for the post-cache action + core.saveState(utils.StatePathsKey, cachePaths); - const cacheUtilInfo = await getCacheSummaryUtil(localCachePath); - core.info( - `Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.` - ); + const cacheUtilInfo = await getCacheSummaryUtil(localCachePath); + core.info( + `Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.` + ); + } catch (error) { + // Fail the workflow run if an error occurs + if (error instanceof Error) core.setFailed(error.message); + } } export async function restoreLocalCache(