Skip to content

Commit 6f08740

Browse files
Make ccache-action respect environment variables (#217)
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
1 parent ed97999 commit 6f08740

6 files changed

Lines changed: 89 additions & 41 deletions

File tree

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
sccache -V
5757
sccache -s
5858
# sccache -s | grep -E 'Max cache size.+10 MiB'
59-
sccache -s | grep -E 'Cache location.+ccache-action/\.sccache'
59+
sccache -s | grep -E "Cache location.+${SCCACHE_DIR:-ccache-action[\\/]+\.sccache}"
6060
else
6161
which ccache
6262
ccache -V

dist/restore/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59573,6 +59573,19 @@ var exec = __nccwpck_require__(1514);
5957359573
const external_process_namespaceObject = require("process");
5957459574
// EXTERNAL MODULE: ./node_modules/@actions/cache/lib/cache.js
5957559575
var cache = __nccwpck_require__(7799);
59576+
;// CONCATENATED MODULE: ./src/common.ts
59577+
59578+
function cacheDir(ccacheVariant) {
59579+
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
59580+
if (ccacheVariant === "ccache") {
59581+
return process.env.CCACHE_DIR || external_path_default().join(ghWorkSpace, ".ccache");
59582+
}
59583+
else if (ccacheVariant === "sccache") {
59584+
return process.env.SCCACHE_DIR || external_path_default().join(ghWorkSpace, ".sccache");
59585+
}
59586+
throw Error("Unknown ccache variant: " + ccacheVariant);
59587+
}
59588+
5957659589
;// CONCATENATED MODULE: ./src/restore.ts
5957759590

5957859591

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

5958459597

5958559598

59599+
5958659600
const SELF_CI = external_process_namespaceObject.env.CCACHE_ACTION_CI === "true";
5958759601
// based on https://cristianadam.eu/20200113/speeding-up-c-plus-plus-github-actions-using-ccache/
5958859602
async function restore(ccacheVariant) {
@@ -59594,7 +59608,7 @@ async function restore(ccacheVariant) {
5959459608
const keyPrefix = ccacheVariant + "-";
5959559609
const primaryKey = inputs.primaryKey ? keyPrefix + inputs.primaryKey + "-" : keyPrefix;
5959659610
const restoreKeys = inputs.restoreKeys.map(k => keyPrefix + k + "-");
59597-
const paths = [`.${ccacheVariant}`];
59611+
const paths = [cacheDir(ccacheVariant)];
5959859612
core.saveState("primaryKey", primaryKey);
5959959613
const shouldRestore = core.getBooleanInput("restore");
5960059614
if (!shouldRestore) {
@@ -59616,10 +59630,9 @@ async function restore(ccacheVariant) {
5961659630
}
5961759631
}
5961859632
async function configure(ccacheVariant, platform) {
59619-
const ghWorkSpace = external_process_namespaceObject.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
5962059633
const maxSize = core.getInput('max-size');
5962159634
if (ccacheVariant === "ccache") {
59622-
await execBash(`ccache --set-config=cache_dir='${external_path_default().join(ghWorkSpace, '.ccache')}'`);
59635+
await execBash(`ccache --set-config=cache_dir='${cacheDir(ccacheVariant)}'`);
5962359636
await execBash(`ccache --set-config=max_size='${maxSize}'`);
5962459637
await execBash(`ccache --set-config=compression=true`);
5962559638
if (platform === "darwin") {
@@ -59640,7 +59653,7 @@ async function configure(ccacheVariant, platform) {
5964059653
await execBash("ccache -p");
5964159654
}
5964259655
else {
59643-
const options = `SCCACHE_IDLE_TIMEOUT=0 SCCACHE_DIR='${ghWorkSpace}'/.sccache SCCACHE_CACHE_SIZE='${maxSize}'`;
59656+
const options = `SCCACHE_IDLE_TIMEOUT=0 SCCACHE_DIR='${cacheDir(ccacheVariant)}' SCCACHE_CACHE_SIZE='${maxSize}'`;
5964459657
await execBash(`env ${options} sccache --start-server`);
5964559658
}
5964659659
}

dist/save/index.js

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59543,16 +59543,38 @@ var __webpack_exports__ = {};
5954359543
// This entry need to be wrapped in an IIFE because it need to be in strict mode.
5954459544
(() => {
5954559545
"use strict";
59546+
// ESM COMPAT FLAG
5954659547
__nccwpck_require__.r(__webpack_exports__);
59547-
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
59548-
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
59549-
/* harmony export */ });
59550-
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(2186);
59551-
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
59552-
/* harmony import */ var _actions_cache__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(7799);
59553-
/* harmony import */ var _actions_cache__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__nccwpck_require__.n(_actions_cache__WEBPACK_IMPORTED_MODULE_1__);
59554-
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_2__ = __nccwpck_require__(1514);
59555-
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__nccwpck_require__.n(_actions_exec__WEBPACK_IMPORTED_MODULE_2__);
59548+
59549+
// EXPORTS
59550+
__nccwpck_require__.d(__webpack_exports__, {
59551+
"default": () => (/* binding */ save)
59552+
});
59553+
59554+
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
59555+
var core = __nccwpck_require__(2186);
59556+
// EXTERNAL MODULE: ./node_modules/@actions/cache/lib/cache.js
59557+
var cache = __nccwpck_require__(7799);
59558+
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
59559+
var exec = __nccwpck_require__(1514);
59560+
// EXTERNAL MODULE: external "path"
59561+
var external_path_ = __nccwpck_require__(1017);
59562+
var external_path_default = /*#__PURE__*/__nccwpck_require__.n(external_path_);
59563+
;// CONCATENATED MODULE: ./src/common.ts
59564+
59565+
function cacheDir(ccacheVariant) {
59566+
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
59567+
if (ccacheVariant === "ccache") {
59568+
return process.env.CCACHE_DIR || external_path_default().join(ghWorkSpace, ".ccache");
59569+
}
59570+
else if (ccacheVariant === "sccache") {
59571+
return process.env.SCCACHE_DIR || external_path_default().join(ghWorkSpace, ".sccache");
59572+
}
59573+
throw Error("Unknown ccache variant: " + ccacheVariant);
59574+
}
59575+
59576+
;// CONCATENATED MODULE: ./src/save.ts
59577+
5955659578

5955759579

5955859580

@@ -59578,51 +59600,51 @@ async function getVerbosity(verbositySetting) {
5957859600
case '2':
5957959601
return ' -vv';
5958059602
default:
59581-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Invalid value "${verbositySetting}" of "verbose" option ignored.`);
59603+
core.warning(`Invalid value "${verbositySetting}" of "verbose" option ignored.`);
5958259604
return '';
5958359605
}
5958459606
}
5958559607
function getExecBashOutput(cmd) {
59586-
return _actions_exec__WEBPACK_IMPORTED_MODULE_2__.getExecOutput("bash", ["-xc", cmd], { silent: true });
59608+
return exec.getExecOutput("bash", ["-xc", cmd], { silent: true });
5958759609
}
5958859610
async function run(earlyExit) {
5958959611
try {
59590-
const ccacheVariant = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("ccacheVariant");
59591-
const primaryKey = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("primaryKey");
59612+
const ccacheVariant = core.getState("ccacheVariant");
59613+
const primaryKey = core.getState("primaryKey");
5959259614
if (!ccacheVariant || !primaryKey) {
59593-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.notice("ccache setup failed, skipping saving.");
59615+
core.notice("ccache setup failed, skipping saving.");
5959459616
return;
5959559617
}
5959659618
// Some versions of ccache do not support --verbose
5959759619
const ccacheKnowsVerbosityFlag = !!(await getExecBashOutput(`${ccacheVariant} --help`)).stdout.includes("--verbose");
59598-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.startGroup(`${ccacheVariant} stats`);
59599-
const verbosity = ccacheKnowsVerbosityFlag ? await getVerbosity(_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput("verbose")) : '';
59600-
await _actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec(`${ccacheVariant} -s${verbosity}`);
59601-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.endGroup();
59602-
if (_actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("shouldSave") !== "true") {
59603-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Not saving cache because 'save' is set to 'false'.");
59620+
core.startGroup(`${ccacheVariant} stats`);
59621+
const verbosity = ccacheKnowsVerbosityFlag ? await getVerbosity(core.getInput("verbose")) : '';
59622+
await exec.exec(`${ccacheVariant} -s${verbosity}`);
59623+
core.endGroup();
59624+
if (core.getState("shouldSave") !== "true") {
59625+
core.info("Not saving cache because 'save' is set to 'false'.");
5960459626
return;
5960559627
}
5960659628
if (await ccacheIsEmpty(ccacheVariant, ccacheKnowsVerbosityFlag)) {
59607-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Not saving cache because no objects are cached.");
59629+
core.info("Not saving cache because no objects are cached.");
5960859630
}
5960959631
else {
5961059632
let saveKey = primaryKey;
59611-
if (_actions_core__WEBPACK_IMPORTED_MODULE_0__.getState("appendTimestamp") == "true") {
59633+
if (core.getState("appendTimestamp") == "true") {
5961259634
saveKey += new Date().toISOString();
5961359635
}
5961459636
else {
59615-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.debug("Not appending timestamp because 'append-timestamp' is not set to 'true'.");
59637+
core.debug("Not appending timestamp because 'append-timestamp' is not set to 'true'.");
5961659638
}
59617-
const paths = [`.${ccacheVariant}`];
59618-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Save cache using key "${saveKey}".`);
59619-
await _actions_cache__WEBPACK_IMPORTED_MODULE_1__.saveCache(paths, saveKey);
59639+
const paths = [cacheDir(ccacheVariant)];
59640+
core.info(`Save cache using key "${saveKey}".`);
59641+
await cache.saveCache(paths, saveKey);
5962059642
}
5962159643
}
5962259644
catch (error) {
5962359645
// A failure to save cache shouldn't prevent the entire CI run from
5962459646
// failing, so do not call setFailed() here.
59625-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Saving cache failed: ${error}`);
59647+
core.warning(`Saving cache failed: ${error}`);
5962659648
}
5962759649
// Since we are not using http requests after this
5962859650
// we can safely exit early
@@ -59631,7 +59653,7 @@ async function run(earlyExit) {
5963159653
}
5963259654
}
5963359655
run(true);
59634-
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (run);
59656+
/* harmony default export */ const save = (run);
5963559657

5963659658
})();
5963759659

src/common.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import path from "path";
2+
3+
export function cacheDir(ccacheVariant: string): string {
4+
const ghWorkSpace = process.env.GITHUB_WORKSPACE || "unreachable, make ncc happy";
5+
if (ccacheVariant === "ccache") {
6+
return process.env.CCACHE_DIR || path.join(ghWorkSpace, ".ccache");
7+
} else if (ccacheVariant === "sccache") {
8+
return process.env.SCCACHE_DIR || path.join(ghWorkSpace, ".sccache");
9+
}
10+
throw Error("Unknown ccache variant: " + ccacheVariant);
11+
}

src/restore.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as io from "@actions/io";
77
import * as exec from "@actions/exec";
88
import * as process from "process";
99
import * as cache from "@actions/cache";
10+
import { cacheDir } from "./common";
1011

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

@@ -22,8 +23,8 @@ async function restore(ccacheVariant : string) : Promise<void> {
2223
const keyPrefix = ccacheVariant + "-";
2324
const primaryKey = inputs.primaryKey ? keyPrefix + inputs.primaryKey + "-" : keyPrefix;
2425
const restoreKeys = inputs.restoreKeys.map(k => keyPrefix + k + "-")
25-
const paths = [`.${ccacheVariant}`];
26-
26+
const paths = [cacheDir(ccacheVariant)];
27+
2728
core.saveState("primaryKey", primaryKey);
2829

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

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

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

src/save.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from "@actions/core";
22
import * as cache from "@actions/cache";
33
import * as exec from "@actions/exec";
4+
import { cacheDir } from "./common";
45

56
async function ccacheIsEmpty(ccacheVariant : string, ccacheKnowsVerbosityFlag : boolean) : Promise<boolean> {
67
if (ccacheVariant === "ccache") {
@@ -51,7 +52,7 @@ async function run(earlyExit : boolean | undefined) : Promise<void> {
5152
const verbosity = ccacheKnowsVerbosityFlag ? await getVerbosity(core.getInput("verbose")) : '';
5253
await exec.exec(`${ccacheVariant} -s${verbosity}`);
5354
core.endGroup();
54-
55+
5556
if (core.getState("shouldSave") !== "true") {
5657
core.info("Not saving cache because 'save' is set to 'false'.");
5758
return;
@@ -66,8 +67,9 @@ async function run(earlyExit : boolean | undefined) : Promise<void> {
6667
} else {
6768
core.debug("Not appending timestamp because 'append-timestamp' is not set to 'true'.");
6869
}
69-
const paths = [`.${ccacheVariant}`];
70-
70+
71+
const paths = [cacheDir(ccacheVariant)];
72+
7173
core.info(`Save cache using key "${saveKey}".`);
7274
await cache.saveCache(paths, saveKey);
7375
}

0 commit comments

Comments
 (0)