Skip to content

Commit 05fb98d

Browse files
tiwarishubbrcrista
andauthored
Cache on ghes (actions#363)
* initial changes * updated version * format check * refactored code * updated test cases * Update src/utils.ts Co-authored-by: Brian Cristante <[email protected]> * Update utils.ts * Update utils.test.ts * review comments * dist update * Review comment * update version * updated version Co-authored-by: Brian Cristante <[email protected]>
1 parent 6c56602 commit 05fb98d

File tree

8 files changed

+1869
-1784
lines changed

8 files changed

+1869
-1784
lines changed

.licenses/npm/@actions/cache.dep.yml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__tests__/utils.test.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import * as cache from '@actions/cache';
2+
import * as core from '@actions/core';
13
import {
24
validateVersion,
3-
validatePythonVersionFormatForPyPy
5+
validatePythonVersionFormatForPyPy,
6+
isCacheFeatureAvailable
47
} from '../src/utils';
58

9+
jest.mock('@actions/cache');
10+
jest.mock('@actions/core');
11+
612
describe('validatePythonVersionFormatForPyPy', () => {
713
it.each([
814
['3.6', true],
@@ -32,3 +38,39 @@ describe('validateVersion', () => {
3238
expect(validateVersion(input)).toEqual(expected);
3339
});
3440
});
41+
42+
describe('isCacheFeatureAvailable', () => {
43+
it('isCacheFeatureAvailable disabled on GHES', () => {
44+
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false);
45+
try {
46+
process.env['GITHUB_SERVER_URL'] = 'http://example.com';
47+
isCacheFeatureAvailable();
48+
} catch (error) {
49+
expect(error).toHaveProperty(
50+
'message',
51+
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
52+
);
53+
} finally {
54+
delete process.env['GITHUB_SERVER_URL'];
55+
}
56+
});
57+
58+
it('isCacheFeatureAvailable disabled on dotcom', () => {
59+
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false);
60+
const infoMock = jest.spyOn(core, 'warning');
61+
const message =
62+
'The runner was not able to contact the cache service. Caching will be skipped';
63+
try {
64+
process.env['GITHUB_SERVER_URL'] = 'http://github.com';
65+
expect(isCacheFeatureAvailable()).toBe(false);
66+
expect(infoMock).toHaveBeenCalledWith(message);
67+
} finally {
68+
delete process.env['GITHUB_SERVER_URL'];
69+
}
70+
});
71+
72+
it('isCacheFeatureAvailable is enabled', () => {
73+
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => true);
74+
expect(isCacheFeatureAvailable()).toBe(true);
75+
});
76+
});

dist/cache-save/index.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -3728,10 +3728,7 @@ const options_1 = __webpack_require__(538);
37283728
const requestUtils_1 = __webpack_require__(899);
37293729
const versionSalt = '1.0';
37303730
function getCacheApiUrl(resource) {
3731-
// Ideally we just use ACTIONS_CACHE_URL
3732-
const baseUrl = (process.env['ACTIONS_CACHE_URL'] ||
3733-
process.env['ACTIONS_RUNTIME_URL'] ||
3734-
'').replace('pipelines', 'artifactcache');
3731+
const baseUrl = process.env['ACTIONS_CACHE_URL'] || '';
37353732
if (!baseUrl) {
37363733
throw new Error('Cache Service Url not found, unable to restore cache.');
37373734
}
@@ -5920,7 +5917,8 @@ function downloadCacheStorageSDK(archiveLocation, archivePath, options) {
59205917
//
59215918
// If the file exceeds the buffer maximum length (~1 GB on 32-bit systems and ~2 GB
59225919
// on 64-bit systems), split the download into multiple segments
5923-
const maxSegmentSize = buffer.constants.MAX_LENGTH;
5920+
// ~2 GB = 2147483647, beyond this, we start getting out of range error. So, capping it accordingly.
5921+
const maxSegmentSize = Math.min(2147483647, buffer.constants.MAX_LENGTH);
59245922
const downloadProgress = new DownloadProgress(contentLength);
59255923
const fd = fs.openSync(archivePath, 'w');
59265924
try {
@@ -41451,6 +41449,15 @@ function checkKey(key) {
4145141449
throw new ValidationError(`Key Validation Error: ${key} cannot contain commas.`);
4145241450
}
4145341451
}
41452+
/**
41453+
* isFeatureAvailable to check the presence of Actions cache service
41454+
*
41455+
* @returns boolean return true if Actions cache service feature is available, otherwise false
41456+
*/
41457+
function isFeatureAvailable() {
41458+
return !!process.env['ACTIONS_CACHE_URL'];
41459+
}
41460+
exports.isFeatureAvailable = isFeatureAvailable;
4145441461
/**
4145541462
* Restores cache from keys
4145641463
*

dist/setup/index.js

+1,776-1,757
Large diffs are not rendered by default.

package-lock.json

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setup-python",
3-
"version": "2.2.2",
3+
"version": "3.1.0",
44
"private": true,
55
"description": "Setup python action",
66
"main": "dist/index.js",
@@ -23,7 +23,7 @@
2323
"author": "GitHub",
2424
"license": "MIT",
2525
"dependencies": {
26-
"@actions/cache": "^1.0.8",
26+
"@actions/cache": "^2.0.0",
2727
"@actions/core": "^1.2.3",
2828
"@actions/exec": "^1.1.0",
2929
"@actions/glob": "^0.2.0",

src/setup-python.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import * as finderPyPy from './find-pypy';
44
import * as path from 'path';
55
import * as os from 'os';
66
import {getCacheDistributor} from './cache-distributions/cache-factory';
7-
import {isGhes} from './utils';
7+
import {isCacheFeatureAvailable} from './utils';
88

99
function isPyPyVersion(versionSpec: string) {
1010
return versionSpec.startsWith('pypy-');
1111
}
1212

1313
async function cacheDependencies(cache: string, pythonVersion: string) {
14-
if (isGhes()) {
15-
throw new Error('Caching is not supported on GHES');
16-
}
1714
const cacheDependencyPath =
1815
core.getInput('cache-dependency-path') || undefined;
1916
const cacheDistributor = getCacheDistributor(
@@ -43,7 +40,7 @@ async function run() {
4340
}
4441

4542
const cache = core.getInput('cache');
46-
if (cache) {
43+
if (cache && isCacheFeatureAvailable()) {
4744
await cacheDependencies(cache, pythonVersion);
4845
}
4946
}

src/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as cache from '@actions/cache';
2+
import * as core from '@actions/core';
13
import fs from 'fs';
24
import * as path from 'path';
35
import * as semver from 'semver';
@@ -99,3 +101,21 @@ export function isGhes(): boolean {
99101
);
100102
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
101103
}
104+
105+
export function isCacheFeatureAvailable(): boolean {
106+
if (!cache.isFeatureAvailable()) {
107+
if (isGhes()) {
108+
throw new Error(
109+
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
110+
);
111+
} else {
112+
core.warning(
113+
'The runner was not able to contact the cache service. Caching will be skipped'
114+
);
115+
}
116+
117+
return false;
118+
}
119+
120+
return true;
121+
}

0 commit comments

Comments
 (0)