Skip to content

Commit e819eaa

Browse files
committed
Don't use the tool cache on GitHub-hosted runners by default
This introduces performance problems due to deficiencies in the filesystem layout of GitHub-hosted runners; see #34. The tool cache isn't very useful on such runners anyway, but I've added an option to override the default. Also add some more logging (related: #45). Resolves: #34
1 parent 5c89f10 commit e819eaa

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ inputs:
2020
description: 'The maximum permitted size of the global Zig cache directory, in MiB. When the cache directory exceeds this size, it is cleared. Default is 2048 (2 GiB). 0 means no limit.'
2121
required: true
2222
default: 2048
23+
use-tool-cache:
24+
description: 'Override whether to use the tool cache when caching Zig installations. Default is false for GitHub-hosted runners, true for other runners.'
25+
required: false
26+
default: ''
2327
runs:
2428
using: 'node20'
2529
main: 'main.js'

common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function getVersion() {
3232
return _cached_version;
3333
}
3434

35-
// Else, look for `mach_zig_version` first
35+
// Else, look for `minimum_zig_version`
3636
match = MINIMUM_ZIG_VERSION_REGEX.exec(zon);
3737
if (match !== null) {
3838
_cached_version = match[1];

main.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,27 +91,57 @@ async function main() {
9191
// * 'cache' only caches the unextracted archive, but it does so across runs. It's a little
9292
// less efficient, but still much preferable to fetching Zig from a mirror. We have this
9393
// dependency anyway for caching the global Zig cache.
94+
//
95+
// Unfortunately, tool-cache can lead to serious performance problems on GitHub-hosted Actions
96+
// runners -- or their Windows ones at least, because the tool cache is stored on a slow drive.
97+
// There are even hacky workarounds for this in official Actions:
98+
//
99+
// https://github.com/actions/setup-go/blob/d35c59abb061a4a6fb18e82ac0862c26744d6ab5/src/installer.ts#L174
100+
//
101+
// Since tool-cache is only really useful on self-hosted runners, let's just disable it by
102+
// default on GitHub-hosted runners, and hence execute Zig straight out of its extracted dir.
103+
let use_tool_cache = core.getInput('use-tool-cache');
104+
if (use_tool_cache === 'true') {
105+
use_tool_cache = true;
106+
} else if (use_tool_cache === 'false') {
107+
use_tool_cache = false;
108+
} else if (use_tool_cache === '') {
109+
use_tool_cache = process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted';
110+
} else {
111+
throw new Error("Invalid 'use-tool-cache' value. Valid values: 'true', 'false'");
112+
}
113+
core.info(`Using tool-cache: ${use_tool_cache}`);
94114

95-
let zig_dir = tc.find('zig', await common.getVersion());
96-
if (!zig_dir) {
115+
let zig_dir;
116+
if (use_tool_cache) {
117+
zig_dir = tc.find('zig', await common.getVersion());
118+
}
119+
if (zig_dir) {
120+
core.info('Using cached Zig installation from tool-cache');
121+
} else {
97122
const tarball_name = await common.getTarballName();
98123
const tarball_ext = await common.getTarballExt();
99124

100125
core.info(`Fetching ${tarball_name}${tarball_ext}`);
101126
const fetch_start = Date.now();
102127
const tarball_path = await retrieveTarball(tarball_name, tarball_ext);
103-
core.info(`fetch took ${Date.now() - fetch_start} ms`);
128+
core.info(`Fetch took ${Date.now() - fetch_start} ms`);
104129

105130
core.info(`Extracting tarball ${tarball_name}${tarball_ext}`);
106131

107132
const extract_start = Date.now();
108133
const zig_parent_dir = tarball_ext === '.zip' ?
109134
await tc.extractZip(tarball_path) :
110135
await tc.extractTar(tarball_path, null, 'xJ'); // J for xz
111-
core.info(`extract took ${Date.now() - extract_start} ms`);
136+
core.info(`Extract took ${Date.now() - extract_start} ms`);
112137

113138
const zig_inner_dir = path.join(zig_parent_dir, tarball_name);
114-
zig_dir = await tc.cacheDir(zig_inner_dir, 'zig', await common.getVersion());
139+
if (use_tool_cache) {
140+
core.info('Copying Zig installation to tool-cache');
141+
zig_dir = await tc.cacheDir(zig_inner_dir, 'zig', await common.getVersion());
142+
} else {
143+
zig_dir = zig_inner_dir;
144+
}
115145
}
116146

117147
core.addPath(zig_dir);
@@ -120,6 +150,7 @@ async function main() {
120150
core.exportVariable('ZIG_LOCAL_CACHE_DIR', await common.getZigCachePath());
121151

122152
if (core.getBooleanInput('use-cache')) {
153+
core.info('Attempting restore of Zig cache');
123154
await cache.restoreCache([await common.getZigCachePath()], await common.getCachePrefix());
124155
}
125156
} catch (err) {

post.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async function main() {
1818
}
1919

2020
if (accessible) {
21+
core.info('Checking cache size');
2122
const size = await totalSize(cache_path);
2223
const size_limit = core.getInput('cache-size-limit') * 1024 * 1024; // MiB -> bytes
2324
if (size_limit !== 0 && size > size_limit) {
@@ -26,10 +27,13 @@ async function main() {
2627
// remove the old cache entries, so we instead want to save an empty cache directory.
2728
// To do this, delete all the contents of the cache directory before saving the cache.
2829
await rmDirContents(cache_path);
30+
} else {
31+
core.info(`Cache directory is ${size} bytes, below limit of ${size_limit} bytes; keeping intact`);
2932
}
3033

3134
const prefix = await common.getCachePrefix();
3235
const name = prefix + github.context.runId;
36+
core.info('Saving Zig cache');
3337
await cache.saveCache([cache_path], name);
3438
}
3539
}

0 commit comments

Comments
 (0)