Merge branch 'modules-caching' into v-dmshib/modules-caching

This commit is contained in:
Dmitry Shibanov 2022-04-11 18:09:48 +02:00
commit 666e762399
10 changed files with 166 additions and 39 deletions

View file

@ -105,7 +105,7 @@ steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: actions/setup-go@v3 - uses: actions/setup-go@v3
with: with:
go-version: '1.14' go-version: '1.17'
check-latest: true check-latest: true
cache: true cache: true
- run: go run hello.go - run: go run hello.go
@ -117,7 +117,7 @@ steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: actions/setup-go@v3 - uses: actions/setup-go@v3
with: with:
go-version: '1.14' go-version: '1.17'
check-latest: true check-latest: true
cache: true cache: true
cache-dependency-path: subdir/go.sum cache-dependency-path: subdir/go.sum

View file

@ -1,4 +1,6 @@
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as cacheUtils from '../src/cache-utils'; import * as cacheUtils from '../src/cache-utils';
import {PackageManagerInfo} from '../src/package-managers'; import {PackageManagerInfo} from '../src/package-managers';
@ -104,3 +106,74 @@ describe('getCacheDirectoryPath', () => {
}).rejects.toThrow(); }).rejects.toThrow();
}); });
}); });
describe('isCacheFeatureAvailable', () => {
//Arrange
let isFeatureAvailableSpy = jest.spyOn(cache, 'isFeatureAvailable');
let warningSpy = jest.spyOn(core, 'warning');
it('should return true when cache feature is available', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return true;
});
let functionResult;
//Act
functionResult = cacheUtils.isCacheFeatureAvailable();
//Assert
expect(functionResult).toBeTruthy();
});
it('should warn when cache feature is unavailable and GHES is not used ', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return false;
});
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
let warningMessage =
'The runner was not able to contact the cache service. Caching will be skipped';
//Act
cacheUtils.isCacheFeatureAvailable();
//Assert
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
});
it('should return false when cache feature is unavailable', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return false;
});
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
let functionResult;
//Act
functionResult = cacheUtils.isCacheFeatureAvailable();
//Assert
expect(functionResult).toBeFalsy();
});
it('should throw when cache feature is unavailable and GHES is used', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return false;
});
process.env['GITHUB_SERVER_URL'] = 'https://nongithub.com';
let errorMessage =
'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.';
//Act + Assert
expect(() => cacheUtils.isCacheFeatureAvailable()).toThrow(errorMessage);
});
});

View file

@ -12,11 +12,14 @@ inputs:
default: ${{ github.token }} default: ${{ github.token }}
cache: cache:
description: Used to specify whether go-modules caching is needed. Set to true, if you'd like to enable caching. description: Used to specify whether go-modules caching is needed. Set to true, if you'd like to enable caching.
default: false
cache-dependency-path: cache-dependency-path:
description: 'Used to specify the path to a dependency file - go.sum' description: 'Used to specify the path to a dependency file - go.sum'
outputs:
cache-hit:
description: 'A boolean value to indicate if a cache was hit'
runs: runs:
using: 'node16' using: 'node16'
main: 'dist/setup/index.js' main: 'dist/setup/index.js'
post: 'dist/cache-save/index.js' post: 'dist/cache-save/index.js'
post-if: success() post-if: success()

View file

@ -3924,7 +3924,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0; exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
const cache = __importStar(__webpack_require__(692));
const core = __importStar(__webpack_require__(470));
const exec = __importStar(__webpack_require__(986)); const exec = __importStar(__webpack_require__(986));
const package_managers_1 = __webpack_require__(813); const package_managers_1 = __webpack_require__(813);
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () { exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
@ -3951,6 +3953,24 @@ exports.getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0
} }
return stdout; return stdout;
}); });
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()) {
if (isGhes()) {
throw new Error('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.');
}
else {
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
}
return false;
}
return true;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/***/ }), /***/ }),
@ -46376,11 +46396,11 @@ function run() {
} }
exports.run = run; exports.run = run;
const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () { const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () {
const cacheInput = core.getInput('cache'); const cacheInput = core.getBooleanInput('cache');
if (!cacheInput) { if (!cacheInput) {
return; return;
} }
const packageManager = cacheInput.toUpperCase() === 'TRUE' ? 'default' : cacheInput; const packageManager = 'default';
const state = core.getState(constants_1.State.CacheMatchedKey); const state = core.getState(constants_1.State.CacheMatchedKey);
const primaryKey = core.getState(constants_1.State.CachePrimaryKey); const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager); const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);

42
dist/setup/index.js vendored
View file

@ -3689,9 +3689,9 @@ const installer = __importStar(__webpack_require__(923));
const semver = __importStar(__webpack_require__(280)); const semver = __importStar(__webpack_require__(280));
const path_1 = __importDefault(__webpack_require__(622)); const path_1 = __importDefault(__webpack_require__(622));
const cache_restore_1 = __webpack_require__(409); const cache_restore_1 = __webpack_require__(409);
const cache_utils_1 = __webpack_require__(143);
const child_process_1 = __importDefault(__webpack_require__(129)); const child_process_1 = __importDefault(__webpack_require__(129));
const fs_1 = __importDefault(__webpack_require__(747)); const fs_1 = __importDefault(__webpack_require__(747));
const url_1 = __webpack_require__(835);
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
@ -3700,11 +3700,11 @@ function run() {
// If not supplied then problem matchers will still be setup. Useful for self-hosted. // If not supplied then problem matchers will still be setup. Useful for self-hosted.
// //
let versionSpec = core.getInput('go-version'); let versionSpec = core.getInput('go-version');
const cache = core.getInput('cache'); const cache = core.getBooleanInput('cache');
core.info(`Setup go version spec ${versionSpec}`); core.info(`Setup go version spec ${versionSpec}`);
if (versionSpec) { if (versionSpec) {
let token = core.getInput('token'); let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`; let auth = !token || cache_utils_1.isGhes() ? undefined : `token ${token}`;
const checkLatest = core.getBooleanInput('check-latest'); const checkLatest = core.getBooleanInput('check-latest');
const installDir = yield installer.getGo(versionSpec, checkLatest, auth); const installDir = yield installer.getGo(versionSpec, checkLatest, auth);
core.addPath(path_1.default.join(installDir, 'bin')); core.addPath(path_1.default.join(installDir, 'bin'));
@ -3719,11 +3719,8 @@ function run() {
core.debug(`add bin ${added}`); core.debug(`add bin ${added}`);
core.info(`Successfully setup go version ${versionSpec}`); core.info(`Successfully setup go version ${versionSpec}`);
} }
if (cache) { if (cache && cache_utils_1.isCacheFeatureAvailable()) {
if (isGhes()) { const packageManager = 'default';
throw new Error('Caching is not supported on GHES');
}
const packageManager = cache.toUpperCase() === 'TRUE' ? 'default' : cache;
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
yield cache_restore_1.restoreCache(packageManager, cacheDependencyPath); yield cache_restore_1.restoreCache(packageManager, cacheDependencyPath);
} }
@ -3775,10 +3772,6 @@ function addBinToPath() {
}); });
} }
exports.addBinToPath = addBinToPath; exports.addBinToPath = addBinToPath;
function isGhes() {
const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
/***/ }), /***/ }),
@ -4171,7 +4164,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0; exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
const cache = __importStar(__webpack_require__(692));
const core = __importStar(__webpack_require__(470));
const exec = __importStar(__webpack_require__(986)); const exec = __importStar(__webpack_require__(986));
const package_managers_1 = __webpack_require__(813); const package_managers_1 = __webpack_require__(813);
exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () { exports.getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
@ -4198,6 +4193,24 @@ exports.getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0
} }
return stdout; return stdout;
}); });
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()) {
if (isGhes()) {
throw new Error('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.');
}
else {
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
}
return false;
}
return true;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
/***/ }), /***/ }),
@ -34244,6 +34257,7 @@ const cache_utils_1 = __webpack_require__(143);
exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () { exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager); const packageManagerInfo = yield cache_utils_1.getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; const platform = process.env.RUNNER_OS;
const versionSpec = core.getInput('go-version');
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo); const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManagerInfo);
const dependencyFilePath = cacheDependencyPath const dependencyFilePath = cacheDependencyPath
? cacheDependencyPath ? cacheDependencyPath
@ -34252,7 +34266,7 @@ exports.restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0
if (!fileHash) { if (!fileHash) {
throw new Error('Some specified paths were not resolved, unable to cache dependencies.'); throw new Error('Some specified paths were not resolved, unable to cache dependencies.');
} }
const primaryKey = `go-cache-${platform}-${fileHash}`; const primaryKey = `${platform}-go${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`); core.debug(`primary key is ${primaryKey}`);
core.saveState(constants_1.State.CachePrimaryKey, primaryKey); core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
const cacheKey = yield cache.restoreCache([cachePath], primaryKey); const cacheKey = yield cache.restoreCache([cachePath], primaryKey);

View file

@ -23,7 +23,7 @@
"author": "GitHub", "author": "GitHub",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/cache": "^2.0.0", "@actions/cache": "^2.0.2",
"@actions/core": "^1.6.0", "@actions/core": "^1.6.0",
"@actions/exec": "^1.1.0", "@actions/exec": "^1.1.0",
"@actions/glob": "^0.2.0", "@actions/glob": "^0.2.0",

View file

@ -14,6 +14,7 @@ export const restoreCache = async (
) => { ) => {
const packageManagerInfo = await getPackageManagerInfo(packageManager); const packageManagerInfo = await getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS; const platform = process.env.RUNNER_OS;
const versionSpec = core.getInput('go-version');
const cachePath = await getCacheDirectoryPath(packageManagerInfo); const cachePath = await getCacheDirectoryPath(packageManagerInfo);
@ -28,7 +29,7 @@ export const restoreCache = async (
); );
} }
const primaryKey = `go-cache-${platform}-${fileHash}`; const primaryKey = `${platform}-go${versionSpec}-${fileHash}`;
core.debug(`primary key is ${primaryKey}`); core.debug(`primary key is ${primaryKey}`);
core.saveState(State.CachePrimaryKey, primaryKey); core.saveState(State.CachePrimaryKey, primaryKey);

View file

@ -21,13 +21,12 @@ export async function run() {
} }
const cachePackages = async () => { const cachePackages = async () => {
const cacheInput = core.getInput('cache'); const cacheInput = core.getBooleanInput('cache');
if (!cacheInput) { if (!cacheInput) {
return; return;
} }
const packageManager = const packageManager = 'default';
cacheInput.toUpperCase() === 'TRUE' ? 'default' : cacheInput;
const state = core.getState(State.CacheMatchedKey); const state = core.getState(State.CacheMatchedKey);
const primaryKey = core.getState(State.CachePrimaryKey); const primaryKey = core.getState(State.CachePrimaryKey);

View file

@ -1,3 +1,5 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import {supportedPackageManagers, PackageManagerInfo} from './package-managers'; import {supportedPackageManagers, PackageManagerInfo} from './package-managers';
@ -42,3 +44,28 @@ export const getCacheDirectoryPath = async (
return stdout; return stdout;
}; };
export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
export function isCacheFeatureAvailable(): boolean {
if (!cache.isFeatureAvailable()) {
if (isGhes()) {
throw new Error(
'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.'
);
} else {
core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
}
return false;
}
return true;
}

View file

@ -4,9 +4,9 @@ import * as installer from './installer';
import * as semver from 'semver'; import * as semver from 'semver';
import path from 'path'; import path from 'path';
import {restoreCache} from './cache-restore'; import {restoreCache} from './cache-restore';
import {isGhes, isCacheFeatureAvailable} from './cache-utils';
import cp from 'child_process'; import cp from 'child_process';
import fs from 'fs'; import fs from 'fs';
import {URL} from 'url';
export async function run() { export async function run() {
try { try {
@ -16,7 +16,7 @@ export async function run() {
// //
let versionSpec = core.getInput('go-version'); let versionSpec = core.getInput('go-version');
const cache = core.getInput('cache'); const cache = core.getBooleanInput('cache');
core.info(`Setup go version spec ${versionSpec}`); core.info(`Setup go version spec ${versionSpec}`);
if (versionSpec) { if (versionSpec) {
@ -41,11 +41,8 @@ export async function run() {
core.info(`Successfully setup go version ${versionSpec}`); core.info(`Successfully setup go version ${versionSpec}`);
} }
if (cache) { if (cache && isCacheFeatureAvailable()) {
if (isGhes()) { const packageManager = 'default';
throw new Error('Caching is not supported on GHES');
}
const packageManager = cache.toUpperCase() === 'TRUE' ? 'default' : cache;
const cacheDependencyPath = core.getInput('cache-dependency-path'); const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(packageManager, cacheDependencyPath); await restoreCache(packageManager, cacheDependencyPath);
} }
@ -98,10 +95,3 @@ export async function addBinToPath(): Promise<boolean> {
} }
return added; return added;
} }
function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}