Skip to content

Commit 180b77f

Browse files
author
MaksimZhukov
committed
Update the caching section in the README
1 parent 9f31dbb commit 180b77f

File tree

2 files changed

+176
-172
lines changed

2 files changed

+176
-172
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ nvm lts syntax: `lts/erbium`, `lts/fermium`, `lts/*`
4141

4242
### Caching packages dependencies
4343

44-
The action has a built-in functionality for caching and restoring npm/yarn dependencies. Supported package managers are `npm`, `yarn`, `pnpm`. The `cache` input is optional, and caching is turned off by default. By default, the action searches for the dependency file in the project root and uses its hash as a part of cache key. Use `cache-dependency-path` to specify custom dependency file path. The field accepts wildcards or an array of files to be cached.
44+
The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings.
45+
46+
Supported package managers are `npm`, `yarn`, `pnpm`. The `cache` input is optional, and caching is turned off by default.
47+
48+
The action defaults to search for the dependency file (`package-lock.json` or `yarn.lock`) in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` for cases when multiple dependency files are used, or they are located in different subdirectories. See the examples of `cache-dependency-path` usage in the [Advanced usage](docs/advanced-usage.md#caching-packages-dependencies) guide.
49+
50+
The action follows [actions/cache](https://github.com/actions/cache/blob/main/examples.md#node---npm) guidelines, and caches global cache on the machine instead of `node_modules`, so cache can be reused between different Node.js versions.
4551

4652
**Caching npm dependencies:**
4753
```yaml
@@ -90,8 +96,6 @@ steps:
9096
- run: pnpm test
9197
```
9298

93-
For more examlpes of caching, please see the [Advanced usage](docs/advanced-usage.md#caching-packages-dependencies) guide.
94-
9599
### Matrix Testing:
96100
```yaml
97101
jobs:

dist/cache-save/index.js

Lines changed: 169 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -4265,87 +4265,87 @@ exports.debug = debug; // for test
42654265
/***/ (function(__unusedmodule, exports, __webpack_require__) {
42664266

42674267
"use strict";
4268-
4269-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4270-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4271-
return new (P || (P = Promise))(function (resolve, reject) {
4272-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
4273-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
4274-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
4275-
step((generator = generator.apply(thisArg, _arguments || [])).next());
4276-
});
4277-
};
4278-
var __importStar = (this && this.__importStar) || function (mod) {
4279-
if (mod && mod.__esModule) return mod;
4280-
var result = {};
4281-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
4282-
result["default"] = mod;
4283-
return result;
4284-
};
4285-
Object.defineProperty(exports, "__esModule", { value: true });
4286-
const core = __importStar(__webpack_require__(470));
4287-
const exec = __importStar(__webpack_require__(986));
4288-
exports.supportedPackageManagers = {
4289-
npm: {
4290-
lockFilePatterns: ['package-lock.json', 'yarn.lock'],
4291-
getCacheFolderCommand: 'npm config get cache'
4292-
},
4293-
pnpm: {
4294-
lockFilePatterns: ['pnpm-lock.yaml'],
4295-
getCacheFolderCommand: 'pnpm store path'
4296-
},
4297-
yarn1: {
4298-
lockFilePatterns: ['yarn.lock'],
4299-
getCacheFolderCommand: 'yarn cache dir'
4300-
},
4301-
yarn2: {
4302-
lockFilePatterns: ['yarn.lock'],
4303-
getCacheFolderCommand: 'yarn config get cacheFolder'
4304-
}
4305-
};
4306-
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
4307-
const { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand);
4308-
if (stderr) {
4309-
throw new Error(stderr);
4310-
}
4311-
return stdout.trim();
4312-
});
4313-
const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
4314-
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`);
4315-
if (!stdOut) {
4316-
throw new Error(`Could not retrieve version of ${packageManager}`);
4317-
}
4318-
return stdOut;
4319-
});
4320-
exports.getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
4321-
if (packageManager === 'npm') {
4322-
return exports.supportedPackageManagers.npm;
4323-
}
4324-
else if (packageManager === 'pnpm') {
4325-
return exports.supportedPackageManagers.pnpm;
4326-
}
4327-
else if (packageManager === 'yarn') {
4328-
const yarnVersion = yield getPackageManagerVersion('yarn', '--version');
4329-
core.debug(`Consumed yarn version is ${yarnVersion}`);
4330-
if (yarnVersion.startsWith('1.')) {
4331-
return exports.supportedPackageManagers.yarn1;
4332-
}
4333-
else {
4334-
return exports.supportedPackageManagers.yarn2;
4335-
}
4336-
}
4337-
else {
4338-
return null;
4339-
}
4340-
});
4341-
exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaiter(void 0, void 0, void 0, function* () {
4342-
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
4343-
if (!stdOut) {
4344-
throw new Error(`Could not get cache folder path for ${packageManager}`);
4345-
}
4346-
core.debug(`${packageManager} path is ${stdOut}`);
4347-
return stdOut;
4348-
});
4268+
4269+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4270+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4271+
return new (P || (P = Promise))(function (resolve, reject) {
4272+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
4273+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
4274+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
4275+
step((generator = generator.apply(thisArg, _arguments || [])).next());
4276+
});
4277+
};
4278+
var __importStar = (this && this.__importStar) || function (mod) {
4279+
if (mod && mod.__esModule) return mod;
4280+
var result = {};
4281+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
4282+
result["default"] = mod;
4283+
return result;
4284+
};
4285+
Object.defineProperty(exports, "__esModule", { value: true });
4286+
const core = __importStar(__webpack_require__(470));
4287+
const exec = __importStar(__webpack_require__(986));
4288+
exports.supportedPackageManagers = {
4289+
npm: {
4290+
lockFilePatterns: ['package-lock.json', 'yarn.lock'],
4291+
getCacheFolderCommand: 'npm config get cache'
4292+
},
4293+
pnpm: {
4294+
lockFilePatterns: ['pnpm-lock.yaml'],
4295+
getCacheFolderCommand: 'pnpm store path'
4296+
},
4297+
yarn1: {
4298+
lockFilePatterns: ['yarn.lock'],
4299+
getCacheFolderCommand: 'yarn cache dir'
4300+
},
4301+
yarn2: {
4302+
lockFilePatterns: ['yarn.lock'],
4303+
getCacheFolderCommand: 'yarn config get cacheFolder'
4304+
}
4305+
};
4306+
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
4307+
const { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand);
4308+
if (stderr) {
4309+
throw new Error(stderr);
4310+
}
4311+
return stdout.trim();
4312+
});
4313+
const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
4314+
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`);
4315+
if (!stdOut) {
4316+
throw new Error(`Could not retrieve version of ${packageManager}`);
4317+
}
4318+
return stdOut;
4319+
});
4320+
exports.getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
4321+
if (packageManager === 'npm') {
4322+
return exports.supportedPackageManagers.npm;
4323+
}
4324+
else if (packageManager === 'pnpm') {
4325+
return exports.supportedPackageManagers.pnpm;
4326+
}
4327+
else if (packageManager === 'yarn') {
4328+
const yarnVersion = yield getPackageManagerVersion('yarn', '--version');
4329+
core.debug(`Consumed yarn version is ${yarnVersion}`);
4330+
if (yarnVersion.startsWith('1.')) {
4331+
return exports.supportedPackageManagers.yarn1;
4332+
}
4333+
else {
4334+
return exports.supportedPackageManagers.yarn2;
4335+
}
4336+
}
4337+
else {
4338+
return null;
4339+
}
4340+
});
4341+
exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaiter(void 0, void 0, void 0, function* () {
4342+
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
4343+
if (!stdOut) {
4344+
throw new Error(`Could not get cache folder path for ${packageManager}`);
4345+
}
4346+
core.debug(`${packageManager} path is ${stdOut}`);
4347+
return stdOut;
4348+
});
43494349

43504350

43514351
/***/ }),
@@ -5272,23 +5272,23 @@ module.exports = v1;
52725272
/***/ (function(__unusedmodule, exports) {
52735273

52745274
"use strict";
5275-
5276-
Object.defineProperty(exports, "__esModule", { value: true });
5277-
var LockType;
5278-
(function (LockType) {
5279-
LockType["Npm"] = "npm";
5280-
LockType["Pnpm"] = "pnpm";
5281-
LockType["Yarn"] = "yarn";
5282-
})(LockType = exports.LockType || (exports.LockType = {}));
5283-
var State;
5284-
(function (State) {
5285-
State["CachePrimaryKey"] = "CACHE_KEY";
5286-
State["CacheMatchedKey"] = "CACHE_RESULT";
5287-
})(State = exports.State || (exports.State = {}));
5288-
var Outputs;
5289-
(function (Outputs) {
5290-
Outputs["CacheHit"] = "cache-hit";
5291-
})(Outputs = exports.Outputs || (exports.Outputs = {}));
5275+
5276+
Object.defineProperty(exports, "__esModule", { value: true });
5277+
var LockType;
5278+
(function (LockType) {
5279+
LockType["Npm"] = "npm";
5280+
LockType["Pnpm"] = "pnpm";
5281+
LockType["Yarn"] = "yarn";
5282+
})(LockType = exports.LockType || (exports.LockType = {}));
5283+
var State;
5284+
(function (State) {
5285+
State["CachePrimaryKey"] = "CACHE_KEY";
5286+
State["CacheMatchedKey"] = "CACHE_RESULT";
5287+
})(State = exports.State || (exports.State = {}));
5288+
var Outputs;
5289+
(function (Outputs) {
5290+
Outputs["CacheHit"] = "cache-hit";
5291+
})(Outputs = exports.Outputs || (exports.Outputs = {}));
52925292

52935293

52945294
/***/ }),
@@ -50588,77 +50588,77 @@ module.exports = require("stream");
5058850588
/***/ (function(__unusedmodule, exports, __webpack_require__) {
5058950589

5059050590
"use strict";
50591-
50592-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
50593-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
50594-
return new (P || (P = Promise))(function (resolve, reject) {
50595-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
50596-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50597-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
50598-
step((generator = generator.apply(thisArg, _arguments || [])).next());
50599-
});
50600-
};
50601-
var __importStar = (this && this.__importStar) || function (mod) {
50602-
if (mod && mod.__esModule) return mod;
50603-
var result = {};
50604-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
50605-
result["default"] = mod;
50606-
return result;
50607-
};
50608-
var __importDefault = (this && this.__importDefault) || function (mod) {
50609-
return (mod && mod.__esModule) ? mod : { "default": mod };
50610-
};
50611-
Object.defineProperty(exports, "__esModule", { value: true });
50612-
const core = __importStar(__webpack_require__(470));
50613-
const cache = __importStar(__webpack_require__(692));
50614-
const fs_1 = __importDefault(__webpack_require__(747));
50615-
const constants_1 = __webpack_require__(196);
50616-
const cache_utils_1 = __webpack_require__(143);
50617-
function run() {
50618-
return __awaiter(this, void 0, void 0, function* () {
50619-
try {
50620-
const cacheLock = core.getInput('cache');
50621-
yield cachePackages(cacheLock);
50622-
}
50623-
catch (error) {
50624-
core.setFailed(error.message);
50625-
}
50626-
});
50627-
}
50628-
exports.run = run;
50629-
const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
50630-
const state = core.getState(constants_1.State.CacheMatchedKey);
50631-
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
50632-
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
50633-
if (!packageManagerInfo) {
50634-
core.debug(`Caching for '${packageManager}' is not supported`);
50635-
return;
50636-
}
50637-
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
50638-
if (!fs_1.default.existsSync(cachePath)) {
50639-
throw new Error(`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePath}`);
50640-
}
50641-
if (primaryKey === state) {
50642-
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
50643-
return;
50644-
}
50645-
try {
50646-
yield cache.saveCache([cachePath], primaryKey);
50647-
core.info(`Cache saved with the key: ${primaryKey}`);
50648-
}
50649-
catch (error) {
50650-
if (error.name === cache.ValidationError.name) {
50651-
throw error;
50652-
}
50653-
else if (error.name === cache.ReserveCacheError.name) {
50654-
core.info(error.message);
50655-
}
50656-
else {
50657-
core.warning(`${error.message}`);
50658-
}
50659-
}
50660-
});
50661-
run();
50591+
50592+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
50593+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
50594+
return new (P || (P = Promise))(function (resolve, reject) {
50595+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
50596+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50597+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
50598+
step((generator = generator.apply(thisArg, _arguments || [])).next());
50599+
});
50600+
};
50601+
var __importStar = (this && this.__importStar) || function (mod) {
50602+
if (mod && mod.__esModule) return mod;
50603+
var result = {};
50604+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
50605+
result["default"] = mod;
50606+
return result;
50607+
};
50608+
var __importDefault = (this && this.__importDefault) || function (mod) {
50609+
return (mod && mod.__esModule) ? mod : { "default": mod };
50610+
};
50611+
Object.defineProperty(exports, "__esModule", { value: true });
50612+
const core = __importStar(__webpack_require__(470));
50613+
const cache = __importStar(__webpack_require__(692));
50614+
const fs_1 = __importDefault(__webpack_require__(747));
50615+
const constants_1 = __webpack_require__(196);
50616+
const cache_utils_1 = __webpack_require__(143);
50617+
function run() {
50618+
return __awaiter(this, void 0, void 0, function* () {
50619+
try {
50620+
const cacheLock = core.getInput('cache');
50621+
yield cachePackages(cacheLock);
50622+
}
50623+
catch (error) {
50624+
core.setFailed(error.message);
50625+
}
50626+
});
50627+
}
50628+
exports.run = run;
50629+
const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
50630+
const state = core.getState(constants_1.State.CacheMatchedKey);
50631+
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
50632+
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
50633+
if (!packageManagerInfo) {
50634+
core.debug(`Caching for '${packageManager}' is not supported`);
50635+
return;
50636+
}
50637+
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
50638+
if (!fs_1.default.existsSync(cachePath)) {
50639+
throw new Error(`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePath}`);
50640+
}
50641+
if (primaryKey === state) {
50642+
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
50643+
return;
50644+
}
50645+
try {
50646+
yield cache.saveCache([cachePath], primaryKey);
50647+
core.info(`Cache saved with the key: ${primaryKey}`);
50648+
}
50649+
catch (error) {
50650+
if (error.name === cache.ValidationError.name) {
50651+
throw error;
50652+
}
50653+
else if (error.name === cache.ReserveCacheError.name) {
50654+
core.info(error.message);
50655+
}
50656+
else {
50657+
core.warning(`${error.message}`);
50658+
}
50659+
}
50660+
});
50661+
run();
5066250662

5066350663

5066450664
/***/ }),

0 commit comments

Comments
 (0)