Skip to content

Commit dca64a2

Browse files
authored
Suggest in error how to configure caching. (#7)
* Suggest in error how to configure caching. * rebuild * handle exceptions nicely.
1 parent 4cb3aff commit dca64a2

File tree

2 files changed

+127
-87
lines changed

2 files changed

+127
-87
lines changed

dist/index/index.js

+61-41
Original file line numberDiff line numberDiff line change
@@ -27230,54 +27230,74 @@ const Output_CacheHit = "cache-hit";
2723027230
const ActionVersion = "nscloud-action-cache@v1";
2723127231
void main();
2723227232
async function main() {
27233-
const localCachePath = process.env[Env_CacheRoot];
27234-
if (localCachePath == null) {
27235-
throw new Error(`Local cache path not found.
27236-
27237-
Did you configure the Namespace cross-invocation cache? https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume
27233+
try {
27234+
const localCachePath = process.env[Env_CacheRoot];
27235+
if (localCachePath == null) {
27236+
let hint = `Please update your \x1b[1mruns-on\x1b[0m labels. E.g.:
27237+
27238+
\x1b[32mruns-on\x1b[34m:\x1b[0m
27239+
- \x1b[34mnscloud-ubuntu-22.04-amd64-8x16-\x1b[1mwith-cache\x1b[0m
27240+
- \x1b[34m\x1b[1mnscloud-cache-size-50gb\x1b[0m
27241+
- \x1b[34m\x1b[1mnscloud-cache-tag-my-cache-key\x1b[0m
27242+
27243+
You can replace \x1b[1mmy-cache-key\x1b[0m with something that represents what you’re storing in the cache.`;
27244+
if (process.env.NSC_RUNNER_PROFILE_INFO) {
27245+
hint = "Please enable \x1b[1mCaching\x1b[0m in your runner profile.";
27246+
}
27247+
throw new Error(`nscloud-cache-action requires a cache volume to be configured.
27248+
27249+
${hint}
27250+
27251+
See also https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume
2723827252

2723927253
Are you running in a container? Check out https://namespace.so/docs/actions/nscloud-cache-action#advanced-running-github-jobs-in-containers`);
27240-
}
27241-
core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`);
27242-
const cachePaths = await resolveCachePaths(localCachePath);
27243-
const cacheMisses = await restoreLocalCache(cachePaths);
27244-
const fullHit = cacheMisses.length === 0;
27245-
core.setOutput(Output_CacheHit, fullHit.toString());
27246-
if (!fullHit) {
27247-
core.info(`Some cache paths missing: ${cacheMisses}.`);
27248-
const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss);
27249-
if (failOnCacheMiss) {
27250-
throw new Error(`Some cache paths missing: ${cacheMisses}.`);
2725127254
}
27252-
}
27253-
else {
27254-
core.info("All cache paths found and restored.");
27255-
}
27256-
try {
27257-
// Write/update cache volume metadata file
27258-
const metadata = ensureCacheMetadata(localCachePath);
27259-
metadata.updatedAt = new Date().toISOString();
27260-
metadata.version = 1;
27261-
if (!metadata.userRequest) {
27262-
metadata.userRequest = {};
27255+
core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`);
27256+
const cachePaths = await resolveCachePaths(localCachePath);
27257+
const cacheMisses = await restoreLocalCache(cachePaths);
27258+
const fullHit = cacheMisses.length === 0;
27259+
core.setOutput(Output_CacheHit, fullHit.toString());
27260+
if (!fullHit) {
27261+
core.info(`Some cache paths missing: ${cacheMisses}.`);
27262+
const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss);
27263+
if (failOnCacheMiss) {
27264+
throw new Error(`Some cache paths missing: ${cacheMisses}.`);
27265+
}
2726327266
}
27264-
for (const p of cachePaths) {
27265-
metadata.userRequest[p.pathInCache] = {
27266-
cacheFramework: p.framework,
27267-
mountTarget: [p.mountTarget],
27268-
source: ActionVersion,
27269-
};
27267+
else {
27268+
core.info("All cache paths found and restored.");
27269+
}
27270+
try {
27271+
// Write/update cache volume metadata file
27272+
const metadata = ensureCacheMetadata(localCachePath);
27273+
metadata.updatedAt = new Date().toISOString();
27274+
metadata.version = 1;
27275+
if (!metadata.userRequest) {
27276+
metadata.userRequest = {};
27277+
}
27278+
for (const p of cachePaths) {
27279+
metadata.userRequest[p.pathInCache] = {
27280+
cacheFramework: p.framework,
27281+
mountTarget: [p.mountTarget],
27282+
source: ActionVersion,
27283+
};
27284+
}
27285+
writeCacheMetadata(localCachePath, metadata);
27286+
}
27287+
catch (e) {
27288+
core.warning("Failed to record cache metadata.");
27289+
core.info(e.message);
2727027290
}
27271-
writeCacheMetadata(localCachePath, metadata);
27291+
// Save the list of cache paths to actions state for the post-cache action
27292+
core.saveState(StatePathsKey, cachePaths);
27293+
const cacheUtilInfo = await getCacheSummaryUtil(localCachePath);
27294+
core.info(`Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.`);
2727227295
}
27273-
catch (e) {
27274-
core.warning("Failed to record cache metadata.");
27275-
core.info(e.message);
27296+
catch (error) {
27297+
// Fail the workflow run if an error occurs
27298+
if (error instanceof Error)
27299+
core.setFailed(error.message);
2727627300
}
27277-
// Save the list of cache paths to actions state for the post-cache action
27278-
core.saveState(StatePathsKey, cachePaths);
27279-
const cacheUtilInfo = await getCacheSummaryUtil(localCachePath);
27280-
core.info(`Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.`);
2728127301
}
2728227302
async function restoreLocalCache(cachePaths) {
2728327303
const cacheMisses = [];

src/index.ts

+66-46
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,84 @@ const ActionVersion = "nscloud-action-cache@v1";
1515
void main();
1616

1717
async function main() {
18-
const localCachePath = process.env[utils.Env_CacheRoot];
19-
if (localCachePath == null) {
20-
throw new Error(
21-
`Local cache path not found.
22-
23-
Did you configure the Namespace cross-invocation cache? https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume
18+
try {
19+
const localCachePath = process.env[utils.Env_CacheRoot];
20+
if (localCachePath == null) {
21+
let hint = `Please update your \x1b[1mruns-on\x1b[0m labels. E.g.:
22+
23+
\x1b[32mruns-on\x1b[34m:\x1b[0m
24+
- \x1b[34mnscloud-ubuntu-22.04-amd64-8x16-\x1b[1mwith-cache\x1b[0m
25+
- \x1b[34m\x1b[1mnscloud-cache-size-50gb\x1b[0m
26+
- \x1b[34m\x1b[1mnscloud-cache-tag-my-cache-key\x1b[0m
27+
28+
You can replace \x1b[1mmy-cache-key\x1b[0m with something that represents what you’re storing in the cache.`;
29+
30+
if (process.env.NSC_RUNNER_PROFILE_INFO) {
31+
hint = "Please enable \x1b[1mCaching\x1b[0m in your runner profile.";
32+
}
33+
34+
throw new Error(
35+
`nscloud-cache-action requires a cache volume to be configured.
36+
37+
${hint}
38+
39+
See also https://namespace.so/docs/features/faster-github-actions#using-a-cache-volume
2440
2541
Are you running in a container? Check out https://namespace.so/docs/actions/nscloud-cache-action#advanced-running-github-jobs-in-containers`
26-
);
27-
}
28-
core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`);
42+
);
43+
}
44+
core.info(`Found Namespace cross-invocation cache at ${localCachePath}.`);
2945

30-
const cachePaths = await resolveCachePaths(localCachePath);
31-
const cacheMisses = await restoreLocalCache(cachePaths);
46+
const cachePaths = await resolveCachePaths(localCachePath);
47+
const cacheMisses = await restoreLocalCache(cachePaths);
3248

33-
const fullHit = cacheMisses.length === 0;
34-
core.setOutput(Output_CacheHit, fullHit.toString());
49+
const fullHit = cacheMisses.length === 0;
50+
core.setOutput(Output_CacheHit, fullHit.toString());
3551

36-
if (!fullHit) {
37-
core.info(`Some cache paths missing: ${cacheMisses}.`);
52+
if (!fullHit) {
53+
core.info(`Some cache paths missing: ${cacheMisses}.`);
3854

39-
const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss);
40-
if (failOnCacheMiss) {
41-
throw new Error(`Some cache paths missing: ${cacheMisses}.`);
55+
const failOnCacheMiss = core.getBooleanInput(Input_FailOnCacheMiss);
56+
if (failOnCacheMiss) {
57+
throw new Error(`Some cache paths missing: ${cacheMisses}.`);
58+
}
59+
} else {
60+
core.info("All cache paths found and restored.");
4261
}
43-
} else {
44-
core.info("All cache paths found and restored.");
45-
}
4662

47-
try {
48-
// Write/update cache volume metadata file
49-
const metadata = utils.ensureCacheMetadata(localCachePath);
50-
metadata.updatedAt = new Date().toISOString();
51-
metadata.version = 1;
52-
if (!metadata.userRequest) {
53-
metadata.userRequest = {};
54-
}
63+
try {
64+
// Write/update cache volume metadata file
65+
const metadata = utils.ensureCacheMetadata(localCachePath);
66+
metadata.updatedAt = new Date().toISOString();
67+
metadata.version = 1;
68+
if (!metadata.userRequest) {
69+
metadata.userRequest = {};
70+
}
5571

56-
for (const p of cachePaths) {
57-
metadata.userRequest[p.pathInCache] = {
58-
cacheFramework: p.framework,
59-
mountTarget: [p.mountTarget],
60-
source: ActionVersion,
61-
};
72+
for (const p of cachePaths) {
73+
metadata.userRequest[p.pathInCache] = {
74+
cacheFramework: p.framework,
75+
mountTarget: [p.mountTarget],
76+
source: ActionVersion,
77+
};
78+
}
79+
utils.writeCacheMetadata(localCachePath, metadata);
80+
} catch (e) {
81+
core.warning("Failed to record cache metadata.");
82+
core.info(e.message);
6283
}
63-
utils.writeCacheMetadata(localCachePath, metadata);
64-
} catch (e) {
65-
core.warning("Failed to record cache metadata.");
66-
core.info(e.message);
67-
}
6884

69-
// Save the list of cache paths to actions state for the post-cache action
70-
core.saveState(utils.StatePathsKey, cachePaths);
85+
// Save the list of cache paths to actions state for the post-cache action
86+
core.saveState(utils.StatePathsKey, cachePaths);
7187

72-
const cacheUtilInfo = await getCacheSummaryUtil(localCachePath);
73-
core.info(
74-
`Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.`
75-
);
88+
const cacheUtilInfo = await getCacheSummaryUtil(localCachePath);
89+
core.info(
90+
`Total available cache space is ${cacheUtilInfo.size}, and ${cacheUtilInfo.used} have been used.`
91+
);
92+
} catch (error) {
93+
// Fail the workflow run if an error occurs
94+
if (error instanceof Error) core.setFailed(error.message);
95+
}
7696
}
7797

7898
export async function restoreLocalCache(

0 commit comments

Comments
 (0)