-
Notifications
You must be signed in to change notification settings - Fork 2
/
post.ts
64 lines (51 loc) · 1.53 KB
/
post.ts
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
import fs from 'node:fs';
import execa from 'execa';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import {
getPluginsDir,
getToolchainCacheKey,
getToolsDir,
getUidFile,
isCacheEnabled,
} from './helpers';
async function cleanToolchain() {
try {
core.info(`Cleaning toolchain of stale items before caching`);
await execa('proto', ['clean', '--yes']);
} catch (error: unknown) {
core.warning(error as Error);
}
}
function shouldSaveCache() {
const base = core.getInput('cache-base');
// Only save the cache for the following 2 scenarios:
// - If not using the base warmup strategy.
// - If using the base warmup strategy, and the current ref matches.
return !base || !!(base && !!(process.env.GITHUB_REF_NAME ?? '').match(base));
}
async function saveCache() {
if (!isCacheEnabled() || !shouldSaveCache()) {
return;
}
const toolsDir = getToolsDir();
if (!fs.existsSync(toolsDir)) {
core.info(`Toolchain does not exist, not saving cache`);
return;
}
try {
const primaryKey = await getToolchainCacheKey();
const cacheHitKey = core.getState('cacheHitKey');
if (cacheHitKey === primaryKey) {
core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`);
return;
}
await cleanToolchain();
core.info(`Saving cache with key ${primaryKey}`);
await cache.saveCache([getPluginsDir(), toolsDir, getUidFile()], primaryKey);
} catch (error: unknown) {
core.setFailed(error as Error);
}
}
// eslint-disable-next-line unicorn/prefer-top-level-await
void saveCache();