Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid downloading package when local cache exists #123

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
downloadTool,
extractTar,
extractZip,
cacheDir
cacheDir,
find
} from '@actions/tool-cache';
import {getOctokit} from '@actions/github';

Expand All @@ -39,8 +40,45 @@ async function setup() {
}
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 === '') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 dirname = getDirname(version);

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

// Compare the checksums.
if (calculatedChecksum !== providedChecksum) {
core.setFailed('Checksum verification failed');
return;
return Error('Checksum verification failed');
}
core.info(`Correct checksum: ${calculatedChecksum}`);

Expand All @@ -75,27 +112,7 @@ async function setup() {
sccachePath = await extractTar(sccachePackage);
}
core.info(`sccache extracted to: ${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 || ''
);
return sccachePath;
}

function getFilename(version: string): Error | string {
Expand Down
Loading