Skip to content

Commit 911e913

Browse files
work on built-in caching
1 parent 2248a7a commit 911e913

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

dist/setup/index.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43387,6 +43387,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
4338743387
step((generator = generator.apply(thisArg, _arguments || [])).next());
4338843388
});
4338943389
};
43390+
var __asyncValues = (this && this.__asyncValues) || function (o) {
43391+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
43392+
var m = o[Symbol.asyncIterator], i;
43393+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
43394+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
43395+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
43396+
};
4339043397
var __importStar = (this && this.__importStar) || function (mod) {
4339143398
if (mod && mod.__esModule) return mod;
4339243399
var result = {};
@@ -43397,16 +43404,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
4339743404
Object.defineProperty(exports, "__esModule", { value: true });
4339843405
const cache = __importStar(__webpack_require__(638));
4339943406
const core = __importStar(__webpack_require__(470));
43407+
const glob = __importStar(__webpack_require__(281));
43408+
const crypto = __importStar(__webpack_require__(417));
43409+
const fs = __importStar(__webpack_require__(747));
43410+
const stream = __importStar(__webpack_require__(794));
43411+
const util = __importStar(__webpack_require__(669));
43412+
const path = __importStar(__webpack_require__(622));
4340043413
const constants_1 = __webpack_require__(694);
4340143414
const cache_1 = __webpack_require__(913);
4340243415
exports.restoreCache = (type) => __awaiter(void 0, void 0, void 0, function* () {
4340343416
let tool = 'npm';
43404-
const primaryKey = core.getInput(constants_1.Inputs.Key, { required: true });
43405-
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
43417+
const lockKey = core.getInput(constants_1.Inputs.Key, { required: true });
43418+
const currentOs = process.env.RUNNER_OS;
43419+
const fileHash = yield hashFile(lockKey);
4340643420
if (type === constants_1.LockType.Yarn) {
4340743421
const yarnVersion = yield cache_1.getYarnVersion();
4340843422
tool = `yarn${yarnVersion}`;
4340943423
}
43424+
const primaryKey = `${currentOs}-${tool}-${fileHash}`;
43425+
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
4341043426
const cachePath = yield cache_1.getDefaultCacheDirectory(tool);
4341143427
core.info(`cachePath is ${cachePath}`);
4341243428
core.info(`primaryKey is ${primaryKey}`);
@@ -43421,6 +43437,51 @@ exports.restoreCache = (type) => __awaiter(void 0, void 0, void 0, function* ()
4342143437
core.setOutput(constants_1.Outputs.CacheHit, isExactMatch);
4342243438
core.info(`Cache restored from key: ${cacheKey}`);
4342343439
});
43440+
function hashFile(matchPatterns) {
43441+
var e_1, _a;
43442+
return __awaiter(this, void 0, void 0, function* () {
43443+
let followSymbolicLinks = false;
43444+
if (process.env.followSymbolicLinks === 'true') {
43445+
followSymbolicLinks = true;
43446+
}
43447+
let hasMatch = false;
43448+
const githubWorkspace = process.env.GITHUB_WORKSPACE;
43449+
const result = crypto.createHash('sha256');
43450+
let count = 0;
43451+
const globber = yield glob.create(matchPatterns, { followSymbolicLinks });
43452+
try {
43453+
for (var _b = __asyncValues(globber.globGenerator()), _c; _c = yield _b.next(), !_c.done;) {
43454+
const file = _c.value;
43455+
console.log(file);
43456+
if (!file.startsWith(`${githubWorkspace}${path.sep}`)) {
43457+
console.log(`Ignore '${file}' since it is not under GITHUB_WORKSPACE.`);
43458+
continue;
43459+
}
43460+
if (fs.statSync(file).isDirectory()) {
43461+
console.log(`Skip directory '${file}'.`);
43462+
continue;
43463+
}
43464+
const hash = crypto.createHash('sha256');
43465+
const pipeline = util.promisify(stream.pipeline);
43466+
yield pipeline(fs.createReadStream(file), hash);
43467+
result.write(hash.digest());
43468+
count++;
43469+
if (!hasMatch) {
43470+
hasMatch = true;
43471+
}
43472+
}
43473+
}
43474+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
43475+
finally {
43476+
try {
43477+
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
43478+
}
43479+
finally { if (e_1) throw e_1.error; }
43480+
}
43481+
result.end();
43482+
return result.digest('hex');
43483+
});
43484+
}
4342443485

4342543486

4342643487
/***/ }),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@actions/core": "^1.2.6",
2828
"@actions/exec": "^1.0.3",
2929
"@actions/github": "^1.1.0",
30+
"@actions/glob": "^0.1.2",
3031
"@actions/http-client": "^1.0.6",
3132
"@actions/io": "^1.0.2",
3233
"@actions/tool-cache": "^1.5.4",

src/cache-restore.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import * as cache from '@actions/cache';
22
import * as core from '@actions/core';
3+
import * as glob from '@actions/glob';
4+
import * as crypto from 'crypto';
5+
import * as fs from 'fs';
6+
import * as stream from 'stream';
7+
import * as util from 'util';
8+
import * as path from 'path';
9+
310
import {State, LockType, Inputs, Outputs} from './constants';
411
import {getYarnVersion, getDefaultCacheDirectory} from './cache';
512

613
export const restoreCache = async (type: LockType | string) => {
714
let tool = 'npm';
815

9-
const primaryKey = core.getInput(Inputs.Key, {required: true});
10-
core.saveState(State.CachePrimaryKey, primaryKey);
16+
const lockKey = core.getInput(Inputs.Key, {required: true});
17+
const currentOs = process.env.RUNNER_OS;
18+
const fileHash = await hashFile(lockKey);
1119

1220
if (type === LockType.Yarn) {
1321
const yarnVersion = await getYarnVersion();
1422
tool = `yarn${yarnVersion}`;
1523
}
1624

25+
const primaryKey = `${currentOs}-${tool}-${fileHash}`;
26+
core.saveState(State.CachePrimaryKey, primaryKey);
27+
1728
const cachePath = await getDefaultCacheDirectory(tool);
1829
core.info(`cachePath is ${cachePath}`);
1930
core.info(`primaryKey is ${primaryKey}`);
@@ -30,3 +41,38 @@ export const restoreCache = async (type: LockType | string) => {
3041
core.setOutput(Outputs.CacheHit, isExactMatch);
3142
core.info(`Cache restored from key: ${cacheKey}`);
3243
};
44+
45+
async function hashFile(matchPatterns: string): Promise<string> {
46+
let followSymbolicLinks = false;
47+
if (process.env.followSymbolicLinks === 'true') {
48+
followSymbolicLinks = true;
49+
}
50+
51+
let hasMatch = false;
52+
const githubWorkspace = process.env.GITHUB_WORKSPACE;
53+
const result = crypto.createHash('sha256');
54+
let count = 0;
55+
const globber = await glob.create(matchPatterns, {followSymbolicLinks});
56+
for await (const file of globber.globGenerator()) {
57+
console.log(file);
58+
if (!file.startsWith(`${githubWorkspace}${path.sep}`)) {
59+
console.log(`Ignore '${file}' since it is not under GITHUB_WORKSPACE.`);
60+
continue;
61+
}
62+
if (fs.statSync(file).isDirectory()) {
63+
console.log(`Skip directory '${file}'.`);
64+
continue;
65+
}
66+
const hash = crypto.createHash('sha256');
67+
const pipeline = util.promisify(stream.pipeline);
68+
await pipeline(fs.createReadStream(file), hash);
69+
result.write(hash.digest());
70+
count++;
71+
if (!hasMatch) {
72+
hasMatch = true;
73+
}
74+
}
75+
result.end();
76+
77+
return result.digest('hex');
78+
}

0 commit comments

Comments
 (0)