Skip to content

Commit 3405202

Browse files
authored
fix: avoid downloading package when local cache exists (#123)
* fix: avoid downloading package when local cache exists As mentioned in #107, `setup` function didn't use `find` function even though it uses `cacheDir` to store downloaded files, which leads to redundant download. It is not significant on GitHub-hosted runners because `cacheDir` stores files in local file system, which is ephemeral. However, it wastes time on self-hosted runners. This commit adds a step to search local file system cache for sccache as well as splitting download procedure to a dedicated function. * fix: explicit comparison for boolean-like value
1 parent 6ab633f commit 3405202

1 file changed

Lines changed: 42 additions & 25 deletions

File tree

src/setup.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
downloadTool,
1818
extractTar,
1919
extractZip,
20-
cacheDir
20+
cacheDir,
21+
find
2122
} from '@actions/tool-cache';
2223
import {getOctokit} from '@actions/github';
2324

@@ -39,8 +40,45 @@ async function setup() {
3940
}
4041
core.info(`try to setup sccache version: ${version}`);
4142

43+
// Search local file system cache for sccache.
44+
// This is useful when actions run on a self-hosted runner.
45+
let sccacheHome = find('sccache', version);
46+
if (sccacheHome === '') {
47+
const sccachePath = await downloadSCCache(version);
48+
if (sccachePath instanceof Error) {
49+
core.setFailed(sccachePath.message);
50+
return;
51+
} else {
52+
const dirname = getDirname(version);
53+
// Cache sccache.
54+
sccacheHome = await cacheDir(
55+
`${sccachePath}/${dirname}`,
56+
'sccache',
57+
version
58+
);
59+
core.info(`sccache cached to: ${sccacheHome}`);
60+
}
61+
} else {
62+
core.info(`find sccache at: ${sccacheHome}`);
63+
}
64+
// Add sccache into path.
65+
core.addPath(`${sccacheHome}`);
66+
// Expose the sccache path as env.
67+
core.exportVariable('SCCACHE_PATH', `${sccacheHome}/sccache`);
68+
69+
// Expose the gha cache related variable to make it easier for users to
70+
// integrate with gha support.
71+
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
72+
core.exportVariable(
73+
'ACTIONS_RUNTIME_TOKEN',
74+
process.env.ACTIONS_RUNTIME_TOKEN || ''
75+
);
76+
}
77+
/**
78+
* @param version sccache version
79+
* @returns Path to sccache on success. Error on checksum verification failure. */
80+
async function downloadSCCache(version: string): Promise<Error | string> {
4281
const filename = getFilename(version);
43-
const dirname = getDirname(version);
4482

4583
const downloadUrl = `https://github.com/mozilla/sccache/releases/download/${version}/${filename}`;
4684
const sha256Url = `${downloadUrl}.sha256`;
@@ -63,8 +101,7 @@ async function setup() {
63101

64102
// Compare the checksums.
65103
if (calculatedChecksum !== providedChecksum) {
66-
core.setFailed('Checksum verification failed');
67-
return;
104+
return Error('Checksum verification failed');
68105
}
69106
core.info(`Correct checksum: ${calculatedChecksum}`);
70107

@@ -75,27 +112,7 @@ async function setup() {
75112
sccachePath = await extractTar(sccachePackage);
76113
}
77114
core.info(`sccache extracted to: ${sccachePath}`);
78-
79-
// Cache sccache.
80-
const sccacheHome = await cacheDir(
81-
`${sccachePath}/${dirname}`,
82-
'sccache',
83-
version
84-
);
85-
core.info(`sccache cached to: ${sccacheHome}`);
86-
87-
// Add cached sccache into path.
88-
core.addPath(`${sccacheHome}`);
89-
// Expose the sccache path as env.
90-
core.exportVariable('SCCACHE_PATH', `${sccacheHome}/sccache`);
91-
92-
// Expose the gha cache related variable to make it easier for users to
93-
// integrate with gha support.
94-
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
95-
core.exportVariable(
96-
'ACTIONS_RUNTIME_TOKEN',
97-
process.env.ACTIONS_RUNTIME_TOKEN || ''
98-
);
115+
return sccachePath;
99116
}
100117

101118
function getFilename(version: string): Error | string {

0 commit comments

Comments
 (0)