Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching on GHES #452

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
work on resolving comments
  • Loading branch information
dmitry-shibanov committed Mar 30, 2022
commit 62e9de96c272bb270d57d36be925e5cdd03a0765
4 changes: 0 additions & 4 deletions __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe('cache-restore', () => {
let debugSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance;
let getCommandOutputSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;
let restoreCacheSpy: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance;

Expand Down Expand Up @@ -103,9 +102,6 @@ describe('cache-restore', () => {

// cache-utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');

isCacheActionAvailable = jest.spyOn(utils, 'isCacheFeatureAvailable');
isCacheActionAvailable.mockImplementation(() => true);
});

describe('Validate provided package manager', () => {
Expand Down
4 changes: 0 additions & 4 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('run', () => {
let getStateSpy: jest.SpyInstance;
let saveCacheSpy: jest.SpyInstance;
let getCommandOutputSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;
let hashFilesSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance;

Expand Down Expand Up @@ -71,9 +70,6 @@ describe('run', () => {

// utils
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');

isCacheActionAvailable = jest.spyOn(utils, 'isCacheFeatureAvailable');
isCacheActionAvailable.mockImplementation(() => true);
});

afterEach(() => {
Expand Down
19 changes: 8 additions & 11 deletions __tests__/cache-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,28 @@ describe('cache-utils', () => {
});
});

it('isCacheFeatureAvailable is false', () => {
it('isCacheFeatureAvailable for GHES is false', () => {
isFeatureAvailable.mockImplementation(() => false);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';

expect(isCacheFeatureAvailable()).toBe(false);
expect(info).toHaveBeenCalledWith(
'[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.'
expect(() => isCacheFeatureAvailable()).toThrowError(
'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.'
);
});

it('isCacheFeatureAvailable is false', () => {
it('isCacheFeatureAvailable for GHES has an interhal error', () => {
isFeatureAvailable.mockImplementation(() => false);
process.env['GITHUB_SERVER_URL'] = '';

expect(isCacheFeatureAvailable()).toBe(false);
expect(info).toHaveBeenCalledWith(
'[warning]An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
expect(() => isCacheFeatureAvailable()).toThrowError(
'An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
);
});

it('isCacheFeatureAvailable is true', () => {
it('isCacheFeatureAvailable for GHES is available', () => {
isFeatureAvailable.mockImplementation(() => true);

expect(isCacheFeatureAvailable()).toBe(true);
expect(info).not.toHaveBeenCalled();
expect(isCacheFeatureAvailable()).toStrictEqual(true);
});

afterEach(() => {
Expand Down
48 changes: 48 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as im from '../src/installer';
import * as cache from '@actions/cache';
import fs from 'fs';
import cp from 'child_process';
import osm = require('os');
Expand Down Expand Up @@ -36,6 +37,7 @@ describe('setup-node', () => {
let execSpy: jest.SpyInstance;
let authSpy: jest.SpyInstance;
let parseNodeVersionSpy: jest.SpyInstance;
let isCacheActionAvailable: jest.SpyInstance;

beforeEach(() => {
// @actions/core
Expand Down Expand Up @@ -67,6 +69,9 @@ describe('setup-node', () => {
existsSpy = jest.spyOn(fs, 'existsSync');
mkdirpSpy = jest.spyOn(io, 'mkdirP');

// @actions/tool-cache
isCacheActionAvailable = jest.spyOn(cache, 'isFeatureAvailable');

// disable authentication portion for installer tests
authSpy = jest.spyOn(auth, 'configAuthentication');
authSpy.mockImplementation(() => {});
Expand Down Expand Up @@ -644,6 +649,49 @@ describe('setup-node', () => {
);
});
});

describe('cache on GHES', () => {
it('Should throw an error, because cache is not supported', async () => {
inputs['node-version'] = '12';
inputs['cache'] = 'npm';

inSpy.mockImplementation(name => inputs[name]);

let toolPath = path.normalize('/cache/node/12.16.1/x64');
findSpy.mockImplementation(() => toolPath);

// expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => false);

await main.run();

expect(cnSpy).toHaveBeenCalledWith(
`::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.${osm.EOL}`
);
});

it('Should throw an internal error', async () => {
inputs['node-version'] = '12';
inputs['cache'] = 'npm';

inSpy.mockImplementation(name => inputs[name]);

let toolPath = path.normalize('/cache/node/12.16.1/x64');
findSpy.mockImplementation(() => toolPath);

// expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`);
process.env['GITHUB_SERVER_URL'] = '';
isCacheActionAvailable.mockImplementation(() => false);

await main.run();

expect(cnSpy).toHaveBeenCalledWith(
`::error::An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.${osm.EOL}`
);
});
});

describe('LTS version', () => {
beforeEach(() => {
os.platform = 'linux';
Expand Down
9 changes: 2 additions & 7 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3873,10 +3873,6 @@ exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaite
core.debug(`${packageManager} path is ${stdOut}`);
return stdOut;
});
function logWarning(message) {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
}
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
Expand All @@ -3885,12 +3881,11 @@ exports.isGhes = isGhes;
function isCacheFeatureAvailable() {
if (!cache.isFeatureAvailable()) {
if (isGhes()) {
logWarning('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.');
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 {
logWarning('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.');
throw new Error('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.');
}
return false;
}
return true;
}
Expand Down
14 changes: 3 additions & 11 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6617,10 +6617,7 @@ function run() {
if (registryUrl) {
auth.configAuthentication(registryUrl, alwaysAuth);
}
if (cache) {
if (!cache_utils_1.isCacheFeatureAvailable()) {
throw new Error('Caching is not supported on GHES');
}
if (cache && cache_utils_1.isCacheFeatureAvailable()) {
const cacheDependencyPath = core.getInput('cache-dependency-path');
yield cache_restore_1.restoreCache(cache, cacheDependencyPath);
}
Expand Down Expand Up @@ -46129,10 +46126,6 @@ exports.getCacheDirectoryPath = (packageManagerInfo, packageManager) => __awaite
core.debug(`${packageManager} path is ${stdOut}`);
return stdOut;
});
function logWarning(message) {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
}
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
Expand All @@ -46141,12 +46134,11 @@ exports.isGhes = isGhes;
function isCacheFeatureAvailable() {
if (!cache.isFeatureAvailable()) {
if (isGhes()) {
logWarning('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.');
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 {
logWarning('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.');
throw new Error('An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.');
}
return false;
}
return true;
}
Expand Down
10 changes: 2 additions & 8 deletions src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ export const getCacheDirectoryPath = async (
return stdOut;
};

function logWarning(message: string): void {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
}

export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
Expand All @@ -112,15 +107,14 @@ export function isGhes(): boolean {
export function isCacheFeatureAvailable(): boolean {
if (!cache.isFeatureAvailable()) {
if (isGhes()) {
logWarning(
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 {
logWarning(
throw new Error(
'An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitry-shibanov in actions/setup-python#363 we ended up changing this warning:

Suggested change
'An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.'
'The runner was not able to contact the cache service. Caching will be skipped'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you.

);
}
return false;
}

return true;
Expand Down
5 changes: 1 addition & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ export async function run() {
auth.configAuthentication(registryUrl, alwaysAuth);
}

if (cache) {
if (!isCacheFeatureAvailable()) {
throw new Error('Caching is not supported on GHES');
}
if (cache && isCacheFeatureAvailable()) {
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath);
}
Expand Down