-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpost.js
More file actions
73 lines (66 loc) · 2.47 KB
/
post.js
File metadata and controls
73 lines (66 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path');
const fs = require('fs').promises;
const core = require('@actions/core');
const github = require('@actions/github');
const cache = require('@actions/cache');
const common = require('./common');
async function main() {
try {
if (core.getBooleanInput('use-cache')) {
const cache_path = await common.getZigCachePath();
let accessible = true;
try {
await fs.access(cache_path, fs.constants.R_OK);
} catch {
accessible = false;
}
if (accessible) {
core.info(`Checking size of cache directory at ${cache_path}`);
const size = await dirSize(cache_path);
const size_limit = core.getInput('cache-size-limit') * 1024 * 1024; // MiB -> bytes
if (size_limit !== 0 && size > size_limit) {
core.info(`Cache directory reached ${size} bytes, exceeding limit of ${size_limit} bytes; clearing cache`);
// We want to clear the cache and start over. Unfortunately, we can't programmatically
// remove the old cache entries, so we instead want to save an empty cache directory.
// To do this, delete all the contents of the cache directory before saving the cache.
await rmDirContents(cache_path);
} else {
core.info(`Cache directory is ${size} bytes, below limit of ${size_limit} bytes; keeping intact`);
}
const prefix = await common.getCachePrefix();
const name = `${prefix}${github.context.runId}-${github.context.runAttempt}`;
core.info(`Saving Zig cache with key '${name}'`);
await cache.saveCache([cache_path], name);
} else {
core.info('Zig cache directory is inaccessible; nothing to save');
}
}
} catch (err) {
core.setFailed(err.message);
}
}
async function dirSize(dir_path) {
try {
let total = 0;
for (const ent of await fs.readdir(dir_path, { withFileTypes: true, recursive: true })) {
if (ent.isFile()) {
const p = path.join(ent.parentPath, ent.name);
try {
const stat = await fs.stat(p);
total += stat.size;
} catch {
core.warning(`Failed to stat ${p}: ${err}`);
}
}
}
return total;
} catch (err) {
core.warning(`Failed to compute size of '${dir_path}': ${err}`);
return 0;
}
}
async function rmDirContents(dir) {
const entries = await fs.readdir(dir);
await Promise.all(entries.map(e => fs.rm(path.join(dir, e), { recursive: true, force: true })));
}
main();