mirror of https://github.com/actions/cache.git
update save as well
parent
d95c048983
commit
ac8fc97c06
File diff suppressed because it is too large
Load Diff
@ -1,16 +0,0 @@
|
|||||||
import * as core from "@actions/core";
|
|
||||||
|
|
||||||
export interface IOutputSetter {
|
|
||||||
setOutput(key: string, value: string): void;
|
|
||||||
setState(key: string, value: string): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class StateOutputSetter implements IOutputSetter {
|
|
||||||
setOutput = core.setOutput;
|
|
||||||
setState = core.saveState;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class NonStateOuputSetter implements IOutputSetter {
|
|
||||||
setOutput = core.setOutput;
|
|
||||||
setState = core.setOutput;
|
|
||||||
}
|
|
@ -1,8 +1,10 @@
|
|||||||
import { StateOutputSetter } from "./outputSetter";
|
import { StateProvider } from "./stateProvider";
|
||||||
import run from "./restoreImpl";
|
import restoreImpl from "./restoreImpl";
|
||||||
|
|
||||||
async function restore(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
await run(new StateOutputSetter());
|
await restoreImpl(new StateProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
export default restore;
|
run();
|
||||||
|
|
||||||
|
export default run;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import { NonStateOuputSetter } from "./outputSetter";
|
import restoreImpl from "./restoreImpl";
|
||||||
import run from "./restoreImpl";
|
import { NullStateProvider } from "./stateProvider";
|
||||||
|
|
||||||
async function restoreOnly(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
await run(new NonStateOuputSetter());
|
await restoreImpl(new NullStateProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
export default restoreOnly;
|
run();
|
||||||
|
|
||||||
|
export default run;
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
import * as cache from "@actions/cache";
|
||||||
|
import * as core from "@actions/core";
|
||||||
|
|
||||||
|
import { Events, Inputs, State } from "./constants";
|
||||||
|
import { IStateProvider } from "./stateProvider";
|
||||||
|
import * as utils from "./utils/actionUtils";
|
||||||
|
|
||||||
|
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
|
||||||
|
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
|
||||||
|
// throw an uncaught exception. Instead of failing this action, just warn.
|
||||||
|
process.on("uncaughtException", e => utils.logWarning(e.message));
|
||||||
|
|
||||||
|
async function saveImpl(stateProvider: IStateProvider): Promise<void> {
|
||||||
|
try {
|
||||||
|
if (!utils.isCacheFeatureAvailable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!utils.isValidEvent()) {
|
||||||
|
utils.logWarning(
|
||||||
|
`Event Validation Error: The event type ${
|
||||||
|
process.env[Events.Key]
|
||||||
|
} is not supported because it's not tied to a branch or tag ref.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If restore has stored a primary key in state, reuse that
|
||||||
|
// Else re-evaluate from inputs
|
||||||
|
const primaryKey =
|
||||||
|
stateProvider.getState(State.CachePrimaryKey) ||
|
||||||
|
core.getInput(Inputs.Key);
|
||||||
|
|
||||||
|
if (!primaryKey) {
|
||||||
|
utils.logWarning(`Error retrieving key from state.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If matched restore key is same as primary key, then do not save cache
|
||||||
|
// NO-OP in case of SaveOnly action
|
||||||
|
const state = stateProvider.getCacheState();
|
||||||
|
if (utils.isExactKeyMatch(primaryKey, state)) {
|
||||||
|
core.info(
|
||||||
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cachePaths = utils.getInputAsArray(Inputs.Path, {
|
||||||
|
required: true
|
||||||
|
});
|
||||||
|
|
||||||
|
const cacheId = await cache.saveCache(cachePaths, primaryKey, {
|
||||||
|
uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (cacheId != -1) {
|
||||||
|
core.info(`Cache saved with key: ${primaryKey}`);
|
||||||
|
}
|
||||||
|
} catch (error: unknown) {
|
||||||
|
utils.logWarning((error as Error).message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default saveImpl;
|
@ -0,0 +1,10 @@
|
|||||||
|
import saveImpl from "./saveImpl";
|
||||||
|
import { NullStateProvider } from "./stateProvider";
|
||||||
|
|
||||||
|
async function run(): Promise<void> {
|
||||||
|
await saveImpl(new NullStateProvider());
|
||||||
|
}
|
||||||
|
|
||||||
|
run();
|
||||||
|
|
||||||
|
export default run;
|
@ -0,0 +1,42 @@
|
|||||||
|
import * as core from "@actions/core";
|
||||||
|
|
||||||
|
import { State } from "./constants";
|
||||||
|
|
||||||
|
export interface IStateProvider {
|
||||||
|
//setOutput(key: string, value: string): void;
|
||||||
|
setState(key: string, value: string): void;
|
||||||
|
getState(key: string): string;
|
||||||
|
|
||||||
|
getCacheState(): string | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
class StateProviderBase implements IStateProvider {
|
||||||
|
getCacheState(): string | undefined {
|
||||||
|
const cacheKey = this.getState(State.CacheMatchedKey);
|
||||||
|
if (cacheKey) {
|
||||||
|
core.debug(`Cache state/key: ${cacheKey}`);
|
||||||
|
return cacheKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
|
||||||
|
setState = (key: string, value: string) => {};
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
getState = (key: string) => "";
|
||||||
|
}
|
||||||
|
|
||||||
|
export class StateProvider extends StateProviderBase {
|
||||||
|
//setOutput = core.setOutput;
|
||||||
|
setState = core.saveState;
|
||||||
|
getState = core.getState;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NullStateProvider extends StateProviderBase {
|
||||||
|
//setOutput = core.setOutput;
|
||||||
|
setState = core.setOutput;
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
getState = (key: string) => "";
|
||||||
|
}
|
Loading…
Reference in New Issue