Add restore cache.

This commit is contained in:
Miles Johnson 2023-04-17 14:02:21 -07:00
parent 7b19c2c567
commit 8cbd5dda6b
3 changed files with 54 additions and 18 deletions

View file

@ -3,3 +3,11 @@ import path from 'path';
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo');
export function getPrimaryCacheKey(): string {
return `setup-rustcargo-${process.platform}`;
}
export function getPathsToCache(): string[] {
return [path.join(CARGO_HOME, 'registry')];
}

View file

@ -1,10 +1,11 @@
import fs from 'fs';
import path from 'path';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import TOML from '@ltd/j-toml';
import { CARGO_HOME } from './helpers';
import { CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers';
interface Toolchain {
channel: string;
@ -185,6 +186,35 @@ async function installBins() {
await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]);
}
async function restoreCache() {
const enabled = core.getBooleanInput('cache');
if (!cache.isFeatureAvailable() || !enabled) {
return;
}
core.info('Attempting to restore cache');
const primaryKey = await getPrimaryCacheKey();
const cacheKey = await cache.restoreCache(
getPathsToCache(),
primaryKey,
[`setup-rustcargo-${process.platform}`, 'setup-rustcargo'],
{},
false,
);
if (cacheKey) {
core.saveState('cacheHitKey', cacheKey);
core.info(`Cache restored using key ${primaryKey}`);
} else {
core.warning(`Cache does not exist using key ${primaryKey}`);
}
core.setOutput('cache-key', cacheKey ?? primaryKey);
core.setOutput('cache-hit', !!cacheKey);
}
async function run() {
core.info('Setting cargo environment variables');
@ -195,6 +225,7 @@ async function run() {
core.exportVariable('CARGO_TERM_COLOR', 'always');
try {
await restoreCache();
await installToolchain(detectToolchain());
await installBins();
} catch (error: unknown) {

21
post.ts
View file

@ -1,23 +1,15 @@
import fs from 'fs';
import path from 'path';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import { CARGO_HOME } from './helpers';
import { getPathsToCache, getPrimaryCacheKey } from './helpers';
async function run() {
async function saveCache() {
const enabled = core.getBooleanInput('cache');
if (!cache.isFeatureAvailable() || !enabled) {
return;
}
if (!fs.existsSync(CARGO_HOME)) {
core.warning(`~/.cargo does not exist, not saving cache`);
return;
}
try {
const primaryKey = 'v0-test';
const primaryKey = getPrimaryCacheKey();
const cacheHitKey = core.getState('cacheHitKey');
if (cacheHitKey === primaryKey) {
@ -27,7 +19,12 @@ async function run() {
core.info(`Saving cache with key ${primaryKey}`);
await cache.saveCache([path.join(CARGO_HOME, 'registry')], primaryKey, {}, false);
await cache.saveCache(getPathsToCache(), primaryKey, {}, false);
}
async function run() {
try {
await saveCache();
} catch (error: unknown) {
core.setFailed((error as Error).message);
}