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: bins:
description: 'Comma-separated list of global binaries to install into Cargo.' description: 'Comma-separated list of global binaries to install into Cargo.'
cache: cache:
description: 'Toggle caching of ~/.cargo/registry and /target.' description: 'Toggle caching of ~/.cargo/registry and /target/debug directories.'
default: true default: true
channel: channel:
description: 'Toolchain specification/channel to install.' description: 'Toolchain specification/channel to install.'
@ -19,9 +19,9 @@ inputs:
description: 'Profile to install. Defaults to "minimal".' description: 'Profile to install. Defaults to "minimal".'
outputs: outputs:
cache-key: cache-key:
description: 'The cache key used.' description: 'The generated cache key used.'
cache-hit: 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: runs:
using: 'node16' using: 'node16'
main: 'dist/index.js' main: 'dist/index.js'

View file

@ -1,9 +1,13 @@
import os from 'os'; import os from 'os';
import path from 'path'; 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 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo'); 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 { export function getPrimaryCacheKey(): string {
return `setup-rustcargo-${process.platform}`; 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 exec from '@actions/exec';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import TOML from '@ltd/j-toml'; import TOML from '@ltd/j-toml';
import { CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers'; import { CACHE_ENABLED, CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers';
interface Toolchain { interface Toolchain {
channel: string; channel: string;
@ -171,7 +171,7 @@ async function installBins() {
.filter(Boolean) .filter(Boolean)
.map((bin) => (bin.startsWith('cargo-') ? bin : `cargo-${bin}`)); .map((bin) => (bin.startsWith('cargo-') ? bin : `cargo-${bin}`));
if (core.getBooleanInput('cache')) { if (CACHE_ENABLED) {
bins.push('cargo-cache'); bins.push('cargo-cache');
} }
@ -191,25 +191,21 @@ async function installBins() {
} }
async function restoreCache() { async function restoreCache() {
const enabled = core.getBooleanInput('cache'); if (!CACHE_ENABLED) {
if (!cache.isFeatureAvailable() || !enabled) {
return; return;
} }
core.info('Attempting to restore cache'); core.info('Attempting to restore cache');
const primaryKey = await getPrimaryCacheKey(); const primaryKey = await getPrimaryCacheKey();
const cacheKey = await cache.restoreCache(
getPathsToCache(), const cacheKey = await cache.restoreCache(getPathsToCache(), primaryKey, [
primaryKey, `setup-rustcargo-${process.platform}`,
[`setup-rustcargo-${process.platform}`, 'setup-rustcargo'], 'setup-rustcargo',
{}, ]);
false,
);
if (cacheKey) { if (cacheKey) {
core.saveState('cacheHitKey', cacheKey); core.saveState('cache-hit-key', cacheKey);
core.info(`Cache restored using key ${primaryKey}`); core.info(`Cache restored using key ${primaryKey}`);
} else { } else {
core.warning(`Cache does not exist using key ${primaryKey}`); 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 cache from '@actions/cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import { getPathsToCache, getPrimaryCacheKey } from './helpers'; import { CACHE_ENABLED, getPathsToCache, getPrimaryCacheKey } from './helpers';
async function saveCache() { async function saveCache() {
const enabled = core.getBooleanInput('cache'); if (!CACHE_ENABLED) {
if (!cache.isFeatureAvailable() || !enabled) {
return; return;
} }
const primaryKey = getPrimaryCacheKey(); const primaryKey = getPrimaryCacheKey();
const cacheHitKey = core.getState('cacheHitKey'); const cacheHitKey = core.getState('cache-hit-key');
if (cacheHitKey === primaryKey) { if (cacheHitKey === primaryKey) {
core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`); 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}`); core.info(`Saving cache with key ${primaryKey}`);
await cache.saveCache(getPathsToCache(), primaryKey, {}, false); await cache.saveCache(getPathsToCache(), primaryKey);
} }
async function run() { async function run() {