This commit is contained in:
Miles Johnson 2023-04-17 14:13:30 -07:00
parent 293503e099
commit 81acac128f
4 changed files with 20 additions and 22 deletions

View file

@ -7,7 +7,7 @@ inputs:
bins:
description: 'Comma-separated list of global binaries to install into Cargo.'
cache:
description: 'Toggle caching of ~/.cargo/registry and /target.'
description: 'Toggle caching of ~/.cargo/registry and /target/debug directories.'
default: true
channel:
description: 'Toolchain specification/channel to install.'
@ -19,9 +19,9 @@ inputs:
description: 'Profile to install. Defaults to "minimal".'
outputs:
cache-key:
description: 'The cache key used.'
description: 'The generated cache key used.'
cache-hit:
description: 'A boolean to indicate an exact match was found for the cache key.'
description: 'Indicates an exact match was found for the cache key.'
runs:
using: 'node16'
main: 'dist/index.js'

View file

@ -1,9 +1,13 @@
import os from 'os';
import path from 'path';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo');
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
export function getPrimaryCacheKey(): string {
return `setup-rustcargo-${process.platform}`;
}

View file

@ -5,7 +5,7 @@ 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, getPathsToCache, getPrimaryCacheKey } from './helpers';
import { CACHE_ENABLED, CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers';
interface Toolchain {
channel: string;
@ -171,7 +171,7 @@ async function installBins() {
.filter(Boolean)
.map((bin) => (bin.startsWith('cargo-') ? bin : `cargo-${bin}`));
if (core.getBooleanInput('cache')) {
if (CACHE_ENABLED) {
bins.push('cargo-cache');
}
@ -191,25 +191,21 @@ async function installBins() {
}
async function restoreCache() {
const enabled = core.getBooleanInput('cache');
if (!cache.isFeatureAvailable() || !enabled) {
if (!CACHE_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,
);
const cacheKey = await cache.restoreCache(getPathsToCache(), primaryKey, [
`setup-rustcargo-${process.platform}`,
'setup-rustcargo',
]);
if (cacheKey) {
core.saveState('cacheHitKey', cacheKey);
core.saveState('cache-hit-key', cacheKey);
core.info(`Cache restored using key ${primaryKey}`);
} else {
core.warning(`Cache does not exist using key ${primaryKey}`);

10
post.ts
View file

@ -1,17 +1,15 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import { getPathsToCache, getPrimaryCacheKey } from './helpers';
import { CACHE_ENABLED, getPathsToCache, getPrimaryCacheKey } from './helpers';
async function saveCache() {
const enabled = core.getBooleanInput('cache');
if (!cache.isFeatureAvailable() || !enabled) {
if (!CACHE_ENABLED) {
return;
}
const primaryKey = getPrimaryCacheKey();
const cacheHitKey = core.getState('cacheHitKey');
const cacheHitKey = core.getState('cache-hit-key');
if (cacheHitKey === primaryKey) {
core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`);
@ -24,7 +22,7 @@ async function saveCache() {
core.info(`Saving cache with key ${primaryKey}`);
await cache.saveCache(getPathsToCache(), primaryKey, {}, false);
await cache.saveCache(getPathsToCache(), primaryKey);
}
async function run() {