mirror of
https://github.com/mozilla-actions/sccache-action
synced 2026-09-14 20:13:56 +00:00
fix: avoid downloading package when local cache exists (#123)
* fix: avoid downloading package when local cache exists As mentioned in https://github.com/Mozilla-Actions/sccache-action/issues/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
This commit is contained in:
+42
-25
@@ -17,7 +17,8 @@ import {
|
|||||||
downloadTool,
|
downloadTool,
|
||||||
extractTar,
|
extractTar,
|
||||||
extractZip,
|
extractZip,
|
||||||
cacheDir
|
cacheDir,
|
||||||
|
find
|
||||||
} from '@actions/tool-cache';
|
} from '@actions/tool-cache';
|
||||||
import {getOctokit} from '@actions/github';
|
import {getOctokit} from '@actions/github';
|
||||||
|
|
||||||
@@ -39,8 +40,45 @@ async function setup() {
|
|||||||
}
|
}
|
||||||
core.info(`try to setup sccache version: ${version}`);
|
core.info(`try to setup sccache version: ${version}`);
|
||||||
|
|
||||||
|
// Search local file system cache for sccache.
|
||||||
|
// This is useful when actions run on a self-hosted runner.
|
||||||
|
let sccacheHome = find('sccache', version);
|
||||||
|
if (sccacheHome === '') {
|
||||||
|
const sccachePath = await downloadSCCache(version);
|
||||||
|
if (sccachePath instanceof Error) {
|
||||||
|
core.setFailed(sccachePath.message);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
const dirname = getDirname(version);
|
||||||
|
// Cache sccache.
|
||||||
|
sccacheHome = await cacheDir(
|
||||||
|
`${sccachePath}/${dirname}`,
|
||||||
|
'sccache',
|
||||||
|
version
|
||||||
|
);
|
||||||
|
core.info(`sccache cached to: ${sccacheHome}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
core.info(`find sccache at: ${sccacheHome}`);
|
||||||
|
}
|
||||||
|
// Add sccache into path.
|
||||||
|
core.addPath(`${sccacheHome}`);
|
||||||
|
// Expose the sccache path as env.
|
||||||
|
core.exportVariable('SCCACHE_PATH', `${sccacheHome}/sccache`);
|
||||||
|
|
||||||
|
// Expose the gha cache related variable to make it easier for users to
|
||||||
|
// integrate with gha support.
|
||||||
|
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
|
||||||
|
core.exportVariable(
|
||||||
|
'ACTIONS_RUNTIME_TOKEN',
|
||||||
|
process.env.ACTIONS_RUNTIME_TOKEN || ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param version sccache version
|
||||||
|
* @returns Path to sccache on success. Error on checksum verification failure. */
|
||||||
|
async function downloadSCCache(version: string): Promise<Error | string> {
|
||||||
const filename = getFilename(version);
|
const filename = getFilename(version);
|
||||||
const dirname = getDirname(version);
|
|
||||||
|
|
||||||
const downloadUrl = `https://github.com/mozilla/sccache/releases/download/${version}/${filename}`;
|
const downloadUrl = `https://github.com/mozilla/sccache/releases/download/${version}/${filename}`;
|
||||||
const sha256Url = `${downloadUrl}.sha256`;
|
const sha256Url = `${downloadUrl}.sha256`;
|
||||||
@@ -63,8 +101,7 @@ async function setup() {
|
|||||||
|
|
||||||
// Compare the checksums.
|
// Compare the checksums.
|
||||||
if (calculatedChecksum !== providedChecksum) {
|
if (calculatedChecksum !== providedChecksum) {
|
||||||
core.setFailed('Checksum verification failed');
|
return Error('Checksum verification failed');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
core.info(`Correct checksum: ${calculatedChecksum}`);
|
core.info(`Correct checksum: ${calculatedChecksum}`);
|
||||||
|
|
||||||
@@ -75,27 +112,7 @@ async function setup() {
|
|||||||
sccachePath = await extractTar(sccachePackage);
|
sccachePath = await extractTar(sccachePackage);
|
||||||
}
|
}
|
||||||
core.info(`sccache extracted to: ${sccachePath}`);
|
core.info(`sccache extracted to: ${sccachePath}`);
|
||||||
|
return sccachePath;
|
||||||
// Cache sccache.
|
|
||||||
const sccacheHome = await cacheDir(
|
|
||||||
`${sccachePath}/${dirname}`,
|
|
||||||
'sccache',
|
|
||||||
version
|
|
||||||
);
|
|
||||||
core.info(`sccache cached to: ${sccacheHome}`);
|
|
||||||
|
|
||||||
// Add cached sccache into path.
|
|
||||||
core.addPath(`${sccacheHome}`);
|
|
||||||
// Expose the sccache path as env.
|
|
||||||
core.exportVariable('SCCACHE_PATH', `${sccacheHome}/sccache`);
|
|
||||||
|
|
||||||
// Expose the gha cache related variable to make it easier for users to
|
|
||||||
// integrate with gha support.
|
|
||||||
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
|
|
||||||
core.exportVariable(
|
|
||||||
'ACTIONS_RUNTIME_TOKEN',
|
|
||||||
process.env.ACTIONS_RUNTIME_TOKEN || ''
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilename(version: string): Error | string {
|
function getFilename(version: string): Error | string {
|
||||||
|
|||||||
Reference in New Issue
Block a user