Skip to content

Commit

Permalink
Make ccache-action respect environment variables (#217)
Browse files Browse the repository at this point in the history
Currently, if environment variables are set for CCACHE_DIR or SCCACHE_DIR then this action will ignore them and hardcode its own.

This is a problem if CCACHE_DIR is set for example in a docker file environment which is very hard to eliminate

Fixes #96
  • Loading branch information
TrentHouliston authored Jul 23, 2024
1 parent ed97999 commit 6f08740
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
sccache -V
sccache -s
# sccache -s | grep -E 'Max cache size.+10 MiB'
sccache -s | grep -E 'Cache location.+ccache-action/\.sccache'
sccache -s | grep -E "Cache location.+${SCCACHE_DIR:-ccache-action[\\/]+\.sccache}"
else
which ccache
ccache -V
Expand Down
21 changes: 17 additions & 4 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59573,6 +59573,19 @@ var exec = __nccwpck_require__(1514);
const external_process_namespaceObject = require("process");
// EXTERNAL MODULE: ./node_modules/@actions/cache/lib/cache.js
var cache = __nccwpck_require__(7799);
;// CONCATENATED MODULE: ./src/common.ts

function cacheDir(ccacheVariant) {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
if (ccacheVariant === "ccache") {
return process.env.CCACHE_DIR || external_path_default().join(ghWorkSpace, ".ccache");
}
else if (ccacheVariant === "sccache") {
return process.env.SCCACHE_DIR || external_path_default().join(ghWorkSpace, ".sccache");
}
throw Error("Unknown ccache variant: " + ccacheVariant);
}

;// CONCATENATED MODULE: ./src/restore.ts


Expand All @@ -59583,6 +59596,7 @@ var cache = __nccwpck_require__(7799);




const SELF_CI = external_process_namespaceObject.env.CCACHE_ACTION_CI === "true";
// based on https://cristianadam.eu/20200113/speeding-up-c-plus-plus-github-actions-using-ccache/
async function restore(ccacheVariant) {
Expand All @@ -59594,7 +59608,7 @@ async function restore(ccacheVariant) {
const keyPrefix = ccacheVariant + "-";
const primaryKey = inputs.primaryKey ? keyPrefix + inputs.primaryKey + "-" : keyPrefix;
const restoreKeys = inputs.restoreKeys.map(k => keyPrefix + k + "-");
const paths = [`.${ccacheVariant}`];
const paths = [cacheDir(ccacheVariant)];
core.saveState("primaryKey", primaryKey);
const shouldRestore = core.getBooleanInput("restore");
if (!shouldRestore) {
Expand All @@ -59616,10 +59630,9 @@ async function restore(ccacheVariant) {
}
}
async function configure(ccacheVariant, platform) {
const ghWorkSpace = external_process_namespaceObject.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
const maxSize = core.getInput('max-size');
if (ccacheVariant === "ccache") {
await execBash(`ccache --set-config=cache_dir='${external_path_default().join(ghWorkSpace, '.ccache')}'`);
await execBash(`ccache --set-config=cache_dir='${cacheDir(ccacheVariant)}'`);
await execBash(`ccache --set-config=max_size='${maxSize}'`);
await execBash(`ccache --set-config=compression=true`);
if (platform === "darwin") {
Expand All @@ -59640,7 +59653,7 @@ async function configure(ccacheVariant, platform) {
await execBash("ccache -p");
}
else {
const options = `SCCACHE_IDLE_TIMEOUT=0 SCCACHE_DIR='${ghWorkSpace}'/.sccache SCCACHE_CACHE_SIZE='${maxSize}'`;
const options = `SCCACHE_IDLE_TIMEOUT=0 SCCACHE_DIR='${cacheDir(ccacheVariant)}' SCCACHE_CACHE_SIZE='${maxSize}'`;
await execBash(`env ${options} sccache --start-server`);
}
}
Expand Down
78 changes: 50 additions & 28 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59543,16 +59543,38 @@ var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be in strict mode.
(() => {
"use strict";
// ESM COMPAT FLAG
__nccwpck_require__.r(__webpack_exports__);
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
/* harmony export */ });
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(2186);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _actions_cache__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(7799);
/* harmony import */ var _actions_cache__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__nccwpck_require__.n(_actions_cache__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_2__ = __nccwpck_require__(1514);
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__nccwpck_require__.n(_actions_exec__WEBPACK_IMPORTED_MODULE_2__);

// EXPORTS
__nccwpck_require__.d(__webpack_exports__, {
"default": () => (/* binding */ save)
});

// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
var core = __nccwpck_require__(2186);
// EXTERNAL MODULE: ./node_modules/@actions/cache/lib/cache.js
var cache = __nccwpck_require__(7799);
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
var exec = __nccwpck_require__(1514);
// EXTERNAL MODULE: external "path"
var external_path_ = __nccwpck_require__(1017);
var external_path_default = /*#__PURE__*/__nccwpck_require__.n(external_path_);
;// CONCATENATED MODULE: ./src/common.ts

function cacheDir(ccacheVariant) {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
if (ccacheVariant === "ccache") {
return process.env.CCACHE_DIR || external_path_default().join(ghWorkSpace, ".ccache");
}
else if (ccacheVariant === "sccache") {
return process.env.SCCACHE_DIR || external_path_default().join(ghWorkSpace, ".sccache");
}
throw Error("Unknown ccache variant: " + ccacheVariant);
}

;// CONCATENATED MODULE: ./src/save.ts




Expand All @@ -59578,51 +59600,51 @@ async function getVerbosity(verbositySetting) {
case '2':
return ' -vv';
default:
_actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Invalid value "${verbositySetting}" of "verbose" option ignored.`);
core.warning(`Invalid value "${verbositySetting}" of "verbose" option ignored.`);
return '';
}
}
function getExecBashOutput(cmd) {
return _actions_exec__WEBPACK_IMPORTED_MODULE_2__.getExecOutput("bash", ["-xc", cmd], { silent: true });
return exec.getExecOutput("bash", ["-xc", cmd], { silent: true });
}
async function run(earlyExit) {
try {
const ccacheVariant = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("ccacheVariant");
const primaryKey = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("primaryKey");
const ccacheVariant = core.getState("ccacheVariant");
const primaryKey = core.getState("primaryKey");
if (!ccacheVariant || !primaryKey) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.notice("ccache setup failed, skipping saving.");
core.notice("ccache setup failed, skipping saving.");
return;
}
// Some versions of ccache do not support --verbose
const ccacheKnowsVerbosityFlag = !!(await getExecBashOutput(`${ccacheVariant} --help`)).stdout.includes("--verbose");
_actions_core__WEBPACK_IMPORTED_MODULE_0__.startGroup(`${ccacheVariant} stats`);
const verbosity = ccacheKnowsVerbosityFlag ? await getVerbosity(_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput("verbose")) : '';
await _actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec(`${ccacheVariant} -s${verbosity}`);
_actions_core__WEBPACK_IMPORTED_MODULE_0__.endGroup();
if (_actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("shouldSave") !== "true") {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Not saving cache because 'save' is set to 'false'.");
core.startGroup(`${ccacheVariant} stats`);
const verbosity = ccacheKnowsVerbosityFlag ? await getVerbosity(core.getInput("verbose")) : '';
await exec.exec(`${ccacheVariant} -s${verbosity}`);
core.endGroup();
if (core.getState("shouldSave") !== "true") {
core.info("Not saving cache because 'save' is set to 'false'.");
return;
}
if (await ccacheIsEmpty(ccacheVariant, ccacheKnowsVerbosityFlag)) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Not saving cache because no objects are cached.");
core.info("Not saving cache because no objects are cached.");
}
else {
let saveKey = primaryKey;
if (_actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("appendTimestamp") == "true") {
if (core.getState("appendTimestamp") == "true") {
saveKey += new Date().toISOString();
}
else {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.debug("Not appending timestamp because 'append-timestamp' is not set to 'true'.");
core.debug("Not appending timestamp because 'append-timestamp' is not set to 'true'.");
}
const paths = [`.${ccacheVariant}`];
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Save cache using key "${saveKey}".`);
await _actions_cache__WEBPACK_IMPORTED_MODULE_1__.saveCache(paths, saveKey);
const paths = [cacheDir(ccacheVariant)];
core.info(`Save cache using key "${saveKey}".`);
await cache.saveCache(paths, saveKey);
}
}
catch (error) {
// A failure to save cache shouldn't prevent the entire CI run from
// failing, so do not call setFailed() here.
_actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Saving cache failed: ${error}`);
core.warning(`Saving cache failed: ${error}`);
}
// Since we are not using http requests after this
// we can safely exit early
Expand All @@ -59631,7 +59653,7 @@ async function run(earlyExit) {
}
}
run(true);
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (run);
/* harmony default export */ const save = (run);

})();

Expand Down
11 changes: 11 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from "path";

export function cacheDir(ccacheVariant: string): string {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
if (ccacheVariant === "ccache") {
return process.env.CCACHE_DIR || path.join(ghWorkSpace, ".ccache");
} else if (ccacheVariant === "sccache") {
return process.env.SCCACHE_DIR || path.join(ghWorkSpace, ".sccache");
}
throw Error("Unknown ccache variant: " + ccacheVariant);
}
10 changes: 5 additions & 5 deletions src/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as io from "@actions/io";
import * as exec from "@actions/exec";
import * as process from "process";
import * as cache from "@actions/cache";
import { cacheDir } from "./common";

const SELF_CI = process.env["CCACHE_ACTION_CI"] === "true"

Expand All @@ -22,8 +23,8 @@ async function restore(ccacheVariant : string) : Promise<void> {
const keyPrefix = ccacheVariant + "-";
const primaryKey = inputs.primaryKey ? keyPrefix + inputs.primaryKey + "-" : keyPrefix;
const restoreKeys = inputs.restoreKeys.map(k => keyPrefix + k + "-")
const paths = [`.${ccacheVariant}`];

const paths = [cacheDir(ccacheVariant)];
core.saveState("primaryKey", primaryKey);

const shouldRestore = core.getBooleanInput("restore");
Expand All @@ -46,11 +47,10 @@ async function restore(ccacheVariant : string) : Promise<void> {
}

async function configure(ccacheVariant : string, platform : string) : Promise<void> {
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
const maxSize = core.getInput('max-size');

if (ccacheVariant === "ccache") {
await execBash(`ccache --set-config=cache_dir='${path.join(ghWorkSpace, '.ccache')}'`);
await execBash(`ccache --set-config=cache_dir='${cacheDir(ccacheVariant)}'`);
await execBash(`ccache --set-config=max_size='${maxSize}'`);
await execBash(`ccache --set-config=compression=true`);
if (platform === "darwin") {
Expand All @@ -70,7 +70,7 @@ async function configure(ccacheVariant : string, platform : string) : Promise<vo
core.info("Cccache config:");
await execBash("ccache -p");
} else {
const options = `SCCACHE_IDLE_TIMEOUT=0 SCCACHE_DIR='${ghWorkSpace}'/.sccache SCCACHE_CACHE_SIZE='${maxSize}'`;
const options = `SCCACHE_IDLE_TIMEOUT=0 SCCACHE_DIR='${cacheDir(ccacheVariant)}' SCCACHE_CACHE_SIZE='${maxSize}'`;
await execBash(`env ${options} sccache --start-server`);
}

Expand Down
8 changes: 5 additions & 3 deletions src/save.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from "@actions/core";
import * as cache from "@actions/cache";
import * as exec from "@actions/exec";
import { cacheDir } from "./common";

async function ccacheIsEmpty(ccacheVariant : string, ccacheKnowsVerbosityFlag : boolean) : Promise<boolean> {
if (ccacheVariant === "ccache") {
Expand Down Expand Up @@ -51,7 +52,7 @@ async function run(earlyExit : boolean | undefined) : Promise<void> {
const verbosity = ccacheKnowsVerbosityFlag ? await getVerbosity(core.getInput("verbose")) : '';
await exec.exec(`${ccacheVariant} -s${verbosity}`);
core.endGroup();

if (core.getState("shouldSave") !== "true") {
core.info("Not saving cache because 'save' is set to 'false'.");
return;
Expand All @@ -66,8 +67,9 @@ async function run(earlyExit : boolean | undefined) : Promise<void> {
} else {
core.debug("Not appending timestamp because 'append-timestamp' is not set to 'true'.");
}
const paths = [`.${ccacheVariant}`];


const paths = [cacheDir(ccacheVariant)];

core.info(`Save cache using key "${saveKey}".`);
await cache.saveCache(paths, saveKey);
}
Expand Down

0 comments on commit 6f08740

Please sign in to comment.