1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2025-04-12 00:47:47 +02:00
cache/src/saveImpl.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-12-06 18:26:58 +00:00
import * as cache from "@actions/cache";
import * as core from "@actions/core";
import { Events, Inputs, State } from "./constants";
2022-12-12 07:26:18 +00:00
import { IStateProvider } from "./stateProvider";
2022-12-06 18:26:58 +00:00
import * as utils from "./utils/actionUtils";
async function saveImpl(stateProvider: IStateProvider): Promise<void> {
2022-12-20 10:10:04 +00:00
if (!utils.isCacheFeatureAvailable()) {
return;
}
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
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;
}
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
// 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);
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
if (!primaryKey) {
utils.logWarning(`Key is not specified.`);
return;
}
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
// If matched restore key is same as primary key, then do not save cache
// NO-OP in case of SaveOnly action
const restoredKey = stateProvider.getCacheState();
2022-12-09 13:56:33 +00:00
2022-12-20 10:10:04 +00:00
if (utils.isExactKeyMatch(primaryKey, restoredKey)) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
}
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
const cachePaths = utils.getInputAsArray(Inputs.Path, {
required: true
});
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
const cacheId = await cache.saveCache(cachePaths, primaryKey, {
uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
});
2022-12-06 18:26:58 +00:00
2022-12-20 10:10:04 +00:00
if (cacheId != -1) {
core.info(`Cache saved with key: ${primaryKey}`);
2022-12-06 18:26:58 +00:00
}
}
export default saveImpl;