2019-10-30 18:48:49 +00:00
|
|
|
import * as core from "@actions/core";
|
|
|
|
import * as path from "path";
|
2020-03-18 13:35:13 +00:00
|
|
|
|
2019-10-30 18:48:49 +00:00
|
|
|
import * as cacheHttpClient from "./cacheHttpClient";
|
2019-11-13 15:54:39 +00:00
|
|
|
import { Events, Inputs, State } from "./constants";
|
2019-12-13 22:24:37 +00:00
|
|
|
import { extractTar } from "./tar";
|
2019-10-30 18:48:49 +00:00
|
|
|
import * as utils from "./utils/actionUtils";
|
|
|
|
|
2019-11-12 21:48:02 +00:00
|
|
|
async function run(): Promise<void> {
|
2019-10-30 18:48:49 +00:00
|
|
|
try {
|
|
|
|
// Validate inputs, this can cause task failure
|
2019-11-13 15:54:39 +00:00
|
|
|
if (!utils.isValidEvent()) {
|
2019-11-21 19:37:54 +00:00
|
|
|
utils.logWarning(
|
2019-11-13 15:54:39 +00:00
|
|
|
`Event Validation Error: The event type ${
|
|
|
|
process.env[Events.Key]
|
2020-04-17 20:46:46 +01:00
|
|
|
} is not supported because it's not tied to a branch or tag ref.`
|
2019-11-13 15:54:39 +00:00
|
|
|
);
|
2019-11-21 19:37:54 +00:00
|
|
|
return;
|
2019-11-13 15:54:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 18:48:49 +00:00
|
|
|
const primaryKey = core.getInput(Inputs.Key, { required: true });
|
|
|
|
core.saveState(State.CacheKey, primaryKey);
|
|
|
|
|
2019-11-05 20:33:41 +00:00
|
|
|
const restoreKeys = core
|
|
|
|
.getInput(Inputs.RestoreKeys)
|
|
|
|
.split("\n")
|
|
|
|
.filter(x => x !== "");
|
2019-10-30 18:48:49 +00:00
|
|
|
const keys = [primaryKey, ...restoreKeys];
|
|
|
|
|
|
|
|
core.debug("Resolved Keys:");
|
|
|
|
core.debug(JSON.stringify(keys));
|
|
|
|
|
|
|
|
if (keys.length > 10) {
|
|
|
|
core.setFailed(
|
|
|
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const key of keys) {
|
|
|
|
if (key.length > 512) {
|
|
|
|
core.setFailed(
|
|
|
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const regex = /^[^,]*$/;
|
|
|
|
if (!regex.test(key)) {
|
|
|
|
core.setFailed(
|
|
|
|
`Key Validation Error: ${key} cannot contain commas.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:36:34 +01:00
|
|
|
const compressionMethod = await utils.getCompressionMethod();
|
|
|
|
|
2019-10-30 18:48:49 +00:00
|
|
|
try {
|
2020-04-22 21:36:34 +01:00
|
|
|
const cacheEntry = await cacheHttpClient.getCacheEntry(keys, {
|
|
|
|
compressionMethod: compressionMethod
|
|
|
|
});
|
2020-01-06 18:05:50 +00:00
|
|
|
if (!cacheEntry?.archiveLocation) {
|
2020-02-25 19:16:36 +00:00
|
|
|
core.info(`Cache not found for input keys: ${keys.join(", ")}`);
|
2019-11-04 16:03:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:14:16 +00:00
|
|
|
const archivePath = path.join(
|
2019-10-30 18:48:49 +00:00
|
|
|
await utils.createTempDirectory(),
|
2020-04-22 21:36:34 +01:00
|
|
|
utils.getCacheFileName(compressionMethod)
|
2019-10-30 18:48:49 +00:00
|
|
|
);
|
|
|
|
core.debug(`Archive Path: ${archivePath}`);
|
|
|
|
|
|
|
|
// Store the cache result
|
|
|
|
utils.setCacheState(cacheEntry);
|
|
|
|
|
2020-03-18 13:43:56 +00:00
|
|
|
try {
|
|
|
|
// Download the cache from the cache entry
|
|
|
|
await cacheHttpClient.downloadCache(
|
|
|
|
cacheEntry.archiveLocation,
|
|
|
|
archivePath
|
|
|
|
);
|
2019-10-30 18:48:49 +00:00
|
|
|
|
2020-03-18 13:43:56 +00:00
|
|
|
const archiveFileSize = utils.getArchiveFileSize(archivePath);
|
|
|
|
core.info(
|
|
|
|
`Cache Size: ~${Math.round(
|
|
|
|
archiveFileSize / (1024 * 1024)
|
|
|
|
)} MB (${archiveFileSize} B)`
|
|
|
|
);
|
2019-11-06 18:41:45 +00:00
|
|
|
|
2020-04-22 21:36:34 +01:00
|
|
|
await extractTar(archivePath, compressionMethod);
|
2020-03-18 13:43:56 +00:00
|
|
|
} finally {
|
|
|
|
// Try to delete the archive to save space
|
|
|
|
try {
|
|
|
|
await utils.unlinkFile(archivePath);
|
|
|
|
} catch (error) {
|
|
|
|
core.debug(`Failed to delete archive: ${error}`);
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 18:48:49 +00:00
|
|
|
|
|
|
|
const isExactKeyMatch = utils.isExactKeyMatch(
|
|
|
|
primaryKey,
|
|
|
|
cacheEntry
|
|
|
|
);
|
|
|
|
utils.setCacheHitOutput(isExactKeyMatch);
|
|
|
|
|
|
|
|
core.info(
|
2019-11-04 16:03:18 +00:00
|
|
|
`Cache restored from key: ${cacheEntry && cacheEntry.cacheKey}`
|
2019-10-30 18:48:49 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
2019-11-21 19:37:54 +00:00
|
|
|
utils.logWarning(error.message);
|
2019-10-30 18:48:49 +00:00
|
|
|
utils.setCacheHitOutput(false);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|
|
|
|
|
|
|
|
export default run;
|