Skip to content

Commit

Permalink
Suggest in error how to configure caching. (#7)
Browse files Browse the repository at this point in the history
* Suggest in error how to configure caching.

* rebuild

* handle exceptions nicely.
  • Loading branch information
n-g authored Sep 26, 2024
1 parent 4cb3aff commit dca64a2
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 87 deletions.
102 changes: 61 additions & 41 deletions dist/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
112 changes: 66 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dca64a2

Please sign in to comment.