This commit is contained in:
Sayak Mukhopadhyay 2022-12-27 16:41:12 +05:30 committed by GitHub
commit 768feb38f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 1139 additions and 1025 deletions

View file

@ -138,6 +138,7 @@ If the runner is not able to access github.com, any Nodejs versions requested du
8. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
9. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
10. [Using private packages](docs/advanced-usage.md#use-private-packages)
11. [Enabling Corepack](docs/advanced-usage.md#enabling-corepack)
## License

View file

@ -1253,6 +1253,44 @@ describe('setup-node', () => {
}
);
});
describe('corepack flag', () => {
it('use corepack if specified', async () => {
inputs['corepack'] = 'true';
await main.run();
expect(getExecOutputSpy).toHaveBeenCalledWith(
'corepack',
['enable'],
expect.anything()
);
});
it('use corepack with given package manager', async () => {
inputs['corepack'] = 'npm';
await main.run();
expect(getExecOutputSpy).toHaveBeenCalledWith(
'corepack',
['enable', 'npm'],
expect.anything()
);
});
it('use corepack with multiple package managers', async () => {
inputs['corepack'] = 'npm yarn';
await main.run();
expect(getExecOutputSpy).toHaveBeenCalledWith(
'corepack',
['enable', 'npm', 'yarn'],
expect.anything()
);
});
it('fails to use corepack with an invalid package manager', async () => {
await expect(im.enableCorepack('npm turbo')).rejects.toThrowError(
`One or more of the specified package managers [ npm turbo ] are not supported by corepack`
);
});
});
});
describe('helper methods', () => {

View file

@ -25,6 +25,9 @@ inputs:
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
cache-dependency-path:
description: 'Used to specify the path to a dependency file: package-lock.json, yarn.lock, etc. Supports wildcards or a list of file names for caching multiple dependencies.'
corepack:
description: 'Used to specify whether to enable Corepack. Set to true to enable all package managers or set it to one or more package manager names (separate package manager names by a space. Supported package manager names: npm, yarn, pnpm.'
default: 'false'
# TODO: add input to control forcing to pull from cloud or dist.
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
outputs:

View file

@ -61018,74 +61018,74 @@ exports.fromPromise = function (fn) {
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const cache = __importStar(__nccwpck_require__(7799));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678);
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn.
process.on('uncaughtException', e => {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${e.message}`);
});
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const cacheLock = core.getInput('cache');
yield cachePackages(cacheLock);
}
catch (error) {
core.setFailed(error.message);
}
});
}
exports.run = run;
const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
const state = core.getState(constants_1.State.CacheMatchedKey);
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
if (!packageManagerInfo) {
core.debug(`Caching for '${packageManager}' is not supported`);
return;
}
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
if (!fs_1.default.existsSync(cachePath)) {
throw new Error(`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePath}`);
}
if (primaryKey === state) {
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
return;
}
const cacheId = yield cache.saveCache([cachePath], primaryKey);
if (cacheId == -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
});
run();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const cache = __importStar(__nccwpck_require__(7799));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678);
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn.
process.on('uncaughtException', e => {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${e.message}`);
});
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const cacheLock = core.getInput('cache');
yield cachePackages(cacheLock);
}
catch (error) {
core.setFailed(error.message);
}
});
}
exports.run = run;
const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
const state = core.getState(constants_1.State.CacheMatchedKey);
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
if (!packageManagerInfo) {
core.debug(`Caching for '${packageManager}' is not supported`);
return;
}
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo, packageManager);
if (!fs_1.default.existsSync(cachePath)) {
throw new Error(`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePath}`);
}
if (primaryKey === state) {
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
return;
}
const cacheId = yield cache.saveCache([cachePath], primaryKey);
if (cacheId == -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
});
run();
/***/ }),
@ -61094,107 +61094,107 @@ run();
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
exports.supportedPackageManagers = {
npm: {
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
getCacheFolderCommand: 'npm config get cache'
},
pnpm: {
lockFilePatterns: ['pnpm-lock.yaml'],
getCacheFolderCommand: 'pnpm store path --silent'
},
yarn1: {
lockFilePatterns: ['yarn.lock'],
getCacheFolderCommand: 'yarn cache dir'
},
yarn2: {
lockFilePatterns: ['yarn.lock'],
getCacheFolderCommand: 'yarn config get cacheFolder'
}
};
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
: stderr;
throw new Error(stderr);
}
return stdout.trim();
});
const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`);
if (!stdOut) {
throw new Error(`Could not retrieve version of ${packageManager}`);
}
return stdOut;
});
exports.getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
if (packageManager === 'npm') {
return exports.supportedPackageManagers.npm;
}
else if (packageManager === 'pnpm') {
return exports.supportedPackageManagers.pnpm;
}
else if (packageManager === 'yarn') {
const yarnVersion = yield getPackageManagerVersion('yarn', '--version');
core.debug(`Consumed yarn version is ${yarnVersion}`);
if (yarnVersion.startsWith('1.')) {
return exports.supportedPackageManagers.yarn1;
}
else {
return exports.supportedPackageManagers.yarn2;
}
}
else {
return null;
}
});
exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
if (!stdOut) {
throw new Error(`Could not get cache folder path for ${packageManager}`);
}
core.debug(`${packageManager} path is ${stdOut}`);
return stdOut.trim();
});
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
exports.isGhes = isGhes;
function isCacheFeatureAvailable() {
if (cache.isFeatureAvailable())
return true;
if (isGhes()) {
core.warning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.');
return false;
}
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514));
const cache = __importStar(__nccwpck_require__(7799));
exports.supportedPackageManagers = {
npm: {
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
getCacheFolderCommand: 'npm config get cache'
},
pnpm: {
lockFilePatterns: ['pnpm-lock.yaml'],
getCacheFolderCommand: 'pnpm store path --silent'
},
yarn1: {
lockFilePatterns: ['yarn.lock'],
getCacheFolderCommand: 'yarn cache dir'
},
yarn2: {
lockFilePatterns: ['yarn.lock'],
getCacheFolderCommand: 'yarn config get cacheFolder'
}
};
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
: stderr;
throw new Error(stderr);
}
return stdout.trim();
});
const getPackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(`${packageManager} ${command}`);
if (!stdOut) {
throw new Error(`Could not retrieve version of ${packageManager}`);
}
return stdOut;
});
exports.getPackageManagerInfo = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
if (packageManager === 'npm') {
return exports.supportedPackageManagers.npm;
}
else if (packageManager === 'pnpm') {
return exports.supportedPackageManagers.pnpm;
}
else if (packageManager === 'yarn') {
const yarnVersion = yield getPackageManagerVersion('yarn', '--version');
core.debug(`Consumed yarn version is ${yarnVersion}`);
if (yarnVersion.startsWith('1.')) {
return exports.supportedPackageManagers.yarn1;
}
else {
return exports.supportedPackageManagers.yarn2;
}
}
else {
return null;
}
});
exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaiter(void 0, void 0, void 0, function* () {
const stdOut = yield exports.getCommandOutput(packageManagerInfo.getCacheFolderCommand);
if (!stdOut) {
throw new Error(`Could not get cache folder path for ${packageManager}`);
}
core.debug(`${packageManager} path is ${stdOut}`);
return stdOut.trim();
});
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
exports.isGhes = isGhes;
function isCacheFeatureAvailable() {
if (cache.isFeatureAvailable())
return true;
if (isGhes()) {
core.warning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.');
return false;
}
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/***/ }),
@ -61203,23 +61203,23 @@ exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/***/ ((__unused_webpack_module, exports) => {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
var LockType;
(function (LockType) {
LockType["Npm"] = "npm";
LockType["Pnpm"] = "pnpm";
LockType["Yarn"] = "yarn";
})(LockType = exports.LockType || (exports.LockType = {}));
var State;
(function (State) {
State["CachePrimaryKey"] = "CACHE_KEY";
State["CacheMatchedKey"] = "CACHE_RESULT";
})(State = exports.State || (exports.State = {}));
var Outputs;
(function (Outputs) {
Outputs["CacheHit"] = "cache-hit";
})(Outputs = exports.Outputs || (exports.Outputs = {}));
Object.defineProperty(exports, "__esModule", ({ value: true }));
var LockType;
(function (LockType) {
LockType["Npm"] = "npm";
LockType["Pnpm"] = "pnpm";
LockType["Yarn"] = "yarn";
})(LockType = exports.LockType || (exports.LockType = {}));
var State;
(function (State) {
State["CachePrimaryKey"] = "CACHE_KEY";
State["CacheMatchedKey"] = "CACHE_RESULT";
})(State = exports.State || (exports.State = {}));
var Outputs;
(function (Outputs) {
Outputs["CacheHit"] = "cache-hit";
})(Outputs = exports.Outputs || (exports.Outputs = {}));
/***/ }),

1699
dist/setup/index.js vendored

File diff suppressed because it is too large Load diff

View file

@ -358,3 +358,32 @@ NOTE: As per https://github.com/actions/setup-node/issues/49 you cannot use `sec
### always-auth input
The always-auth input sets `always-auth=true` in .npmrc file. With this option set [npm](https://docs.npmjs.com/cli/v6/using-npm/config#always-auth)/yarn sends the authentication credentials when making a request to the registries.
## Enabling Corepack
You can enable [Corepack](https://github.com/nodejs/corepack) by using the `corepack` input. This will enable Corepack. You can then use `pnpm` and `yarn` commands in your project.
```yaml
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
corepack: true
- name: Install dependencies
run: yarn install --immutable
```
You can also pass package manager names instead if you want to enable corepack for specific package managers only.
```yaml
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
corepack: yarn pnpm
- name: Install dependencies
run: yarn install --immutable
```
This option by default is `false` as Corepack is still in experimental phase.

View file

@ -4,6 +4,7 @@ import * as core from '@actions/core';
import * as hc from '@actions/http-client';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as exec from '@actions/exec';
import * as path from 'path';
import * as semver from 'semver';
import fs from 'fs';
@ -604,3 +605,21 @@ export function parseNodeVersionFile(contents: string): string {
function isLatestSyntax(versionSpec): boolean {
return ['current', 'latest', 'node'].includes(versionSpec);
}
export async function enableCorepack(input: string): Promise<void> {
let corepackArgs = ['enable'];
if (input.length > 0 && input !== 'false') {
if (input !== 'true') {
const packageManagers = input.split(' ');
if (!packageManagers.every(pm => ['npm', 'yarn', 'pnpm'].includes(pm))) {
throw new Error(
`One or more of the specified package managers [ ${input} ] are not supported by corepack`
);
}
corepackArgs.push(...packageManagers);
}
await exec.getExecOutput('corepack', corepackArgs, {
ignoreReturnCode: true
});
}
}

View file

@ -49,6 +49,9 @@ export async function run() {
auth.configAuthentication(registryUrl, alwaysAuth);
}
const corepack = core.getInput('corepack') || 'false';
await installer.enableCorepack(corepack);
if (cache && isCacheFeatureAvailable()) {
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath);