Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 11 additions & 36 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,34 @@
const core = require("@actions/core");
const { execSync } = require("child_process");
const path = require("path");
const cache = require("@actions/cache");

function parseBooleanInput(value, defaultValue = false) {
const normalized = value.trim().toLowerCase();
return { 'true': true, 'false': false }[normalized] ?? defaultValue;
}
const { parseBooleanInput, buildBaseConfig } = require("./utils");

async function fetchCache() {
try {
const paths = [];
const restoreKeys = [];

const mixkey = core.getInput("mixkey");
const prefix = core.getInput("prefix");
const cleanUpCache = parseBooleanInput(core.getInput("clean"));

if (cleanUpCache) return;

if (prefix) {
process.chdir(prefix);
core.debug(`Changed working directory to: ${prefix}`);
}

let keyString = mixkey ? `${mixkey}-cache-openwrt` : "cache-openwrt";

const cacheToolchain = parseBooleanInput(core.getInput("toolchain"), true);
const { keyString: baseKey, paths, cacheToolchain, cacheCcache } = buildBaseConfig();
const skipBuildingToolchain = parseBooleanInput(core.getInput("skip"), true);

if (cacheToolchain) {
const toolchainHash = execSync('git log --pretty=tformat:"%h" -n1 tools toolchain')
.toString()
.trim();

keyString += `-${toolchainHash}`;
paths.push(
path.join("staging_dir", "host*"),
path.join("staging_dir", "tool*")
);
} else {
core.debug("Skipping toolchain processing");
}
let keyString = baseKey;
const restoreKeys = [];

const cacheCcache = parseBooleanInput(core.getInput("ccache"));
if (cacheCcache) {
const timestamp = execSync("date +%s").toString().trim();
restoreKeys.unshift(keyString);
keyString += `-${timestamp}`;
paths.push(".ccache");
}

if (paths.length === 0) {
core.debug("No paths configured for caching, skipping");
return;
}

core.debug(`Cache key: ${keyString}`);
core.debug(`Cache restore keys: ${restoreKeys.join(", ")}`);
core.debug(`Cache paths: ${paths.join(", ")}`);
console.log(keyString, restoreKeys);

const cacheFetchingResult = await cache.restoreCache(paths, keyString, restoreKeys);

Expand All @@ -69,7 +45,6 @@ async function fetchCache() {
}
} catch (error) {
core.setFailed(error.message);
process.exit(1);
}
}

Expand Down
80 changes: 36 additions & 44 deletions save.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
const core = require("@actions/core");
const { execSync } = require("child_process");
const path = require("path");
const cache = require("@actions/cache");

function parseBooleanInput(value, defaultValue = false) {
const normalized = value.trim().toLowerCase();
return { 'true': true, 'false': false }[normalized] ?? defaultValue;
}
const { parseBooleanInput, buildBaseConfig } = require("./utils");

async function saveCache() {
try {
const cleanUpCache = parseBooleanInput(core.getInput("clean"));
if (cleanUpCache) {
core.debug("Cache clean requested, skipping save");
return;
}

const skipSaving = parseBooleanInput(core.getInput("skip_saving"));
if (skipSaving) {
core.debug("skip_saving is set, skipping save");
return;
}

const cacheState = core.getState("CACHE_STATE");
if (cacheState === "hit") {
core.debug("Cache was already restored, skipping save");
return;
}

const { keyString: baseKey, paths, cacheCcache } = buildBaseConfig();

let keyString = baseKey;
if (cacheCcache) {
const timestamp = execSync("date +%s").toString().trim();
keyString += `-${timestamp}`;
paths.push(".ccache");
}

if (paths.length === 0) {
core.debug("No paths configured for caching, skipping");
return;
}

core.debug(`Saving cache with key: ${keyString}`);
core.debug(`Cache paths: ${paths.join(", ")}`);

if (cacheState !== "hit" && !skipSaving) {
const paths = [];
const mixkey = core.getInput("mixkey");
let keyString = mixkey ? `${mixkey}-cache-openwrt` : "cache-openwrt";

const prefix = core.getInput("prefix");
if (prefix) {
process.chdir(prefix);
core.debug(`Changed working directory to: ${prefix}`);
}

const cacheToolchain = parseBooleanInput(core.getInput("toolchain"), true);
if (cacheToolchain) {
const toolchainHash = execSync(
'git log --pretty=tformat:"%h" -n1 tools toolchain'
).toString().trim();

keyString += `-${toolchainHash}`;
paths.push(
path.join("staging_dir", "host*"),
path.join("staging_dir", "tool*")
);
}

const cacheCcache = parseBooleanInput(core.getInput("ccache"));
if (cacheCcache) {
const timestamp = execSync("date +%s").toString().trim();
keyString += `-${timestamp}`;
paths.push(".ccache");
}

console.log(keyString);

await cache.saveCache(paths, keyString)
.then(res => {
if (res) console.log(res, " cache saved");
})
.catch(err => core.error(`Cache save failed: ${err.stack}`));
const cacheId = await cache.saveCache(paths, keyString);
if (cacheId) {
core.info(`Cache saved with key: ${keyString} (id: ${cacheId})`);
}
} catch (error) {
core.warning(error.message);
Expand Down
38 changes: 38 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const core = require("@actions/core");
const { execSync } = require("child_process");
const path = require("path");

function parseBooleanInput(value, defaultValue = false) {
const normalized = value.trim().toLowerCase();
return { 'true': true, 'false': false }[normalized] ?? defaultValue;
}

function buildBaseConfig() {
const prefix = core.getInput("prefix");
if (prefix) {
process.chdir(prefix);
core.debug(`Changed working directory to: ${prefix}`);
}

const mixkey = core.getInput("mixkey");
let keyString = mixkey ? `${mixkey}-cache-openwrt` : "cache-openwrt";
const paths = [];

const cacheToolchain = parseBooleanInput(core.getInput("toolchain"), true);
if (cacheToolchain) {
const toolchainHash = execSync('git log --pretty=tformat:"%h" -n1 tools toolchain')
.toString()
.trim();
keyString += `-${toolchainHash}`;
paths.push(
path.join("staging_dir", "host*"),
path.join("staging_dir", "tool*")
);
}

const cacheCcache = parseBooleanInput(core.getInput("ccache"));

return { keyString, paths, cacheToolchain, cacheCcache };
}

module.exports = { parseBooleanInput, buildBaseConfig };
Loading