Skip to content

Commit

Permalink
Cache on ghes (#363)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
tiwarishub and brcrista authored Mar 31, 2022
1 parent 6c56602 commit 05fb98d
Show file tree
Hide file tree
Showing 8 changed files with 1,869 additions and 1,784 deletions.
2 changes: 1 addition & 1 deletion .licenses/npm/@actions/cache.dep.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 43 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import {
validateVersion,
validatePythonVersionFormatForPyPy
validatePythonVersionFormatForPyPy,
isCacheFeatureAvailable
} from '../src/utils';

jest.mock('@actions/cache');
jest.mock('@actions/core');

describe('validatePythonVersionFormatForPyPy', () => {
it.each([
['3.6', true],
Expand Down Expand Up @@ -32,3 +38,39 @@ describe('validateVersion', () => {
expect(validateVersion(input)).toEqual(expected);
});
});

describe('isCacheFeatureAvailable', () => {
it('isCacheFeatureAvailable disabled on GHES', () => {
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false);
try {
process.env['GITHUB_SERVER_URL'] = 'http://example.com';
isCacheFeatureAvailable();
} catch (error) {
expect(error).toHaveProperty(
'message',
'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.'
);
} finally {
delete process.env['GITHUB_SERVER_URL'];
}
});

it('isCacheFeatureAvailable disabled on dotcom', () => {
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false);
const infoMock = jest.spyOn(core, 'warning');
const message =
'The runner was not able to contact the cache service. Caching will be skipped';
try {
process.env['GITHUB_SERVER_URL'] = 'http://github.com';
expect(isCacheFeatureAvailable()).toBe(false);
expect(infoMock).toHaveBeenCalledWith(message);
} finally {
delete process.env['GITHUB_SERVER_URL'];
}
});

it('isCacheFeatureAvailable is enabled', () => {
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => true);
expect(isCacheFeatureAvailable()).toBe(true);
});
});
17 changes: 12 additions & 5 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3728,10 +3728,7 @@ const options_1 = __webpack_require__(538);
const requestUtils_1 = __webpack_require__(899);
const versionSalt = '1.0';
function getCacheApiUrl(resource) {
// Ideally we just use ACTIONS_CACHE_URL
const baseUrl = (process.env['ACTIONS_CACHE_URL'] ||
process.env['ACTIONS_RUNTIME_URL'] ||
'').replace('pipelines', 'artifactcache');
const baseUrl = process.env['ACTIONS_CACHE_URL'] || '';
if (!baseUrl) {
throw new Error('Cache Service Url not found, unable to restore cache.');
}
Expand Down Expand Up @@ -5920,7 +5917,8 @@ function downloadCacheStorageSDK(archiveLocation, archivePath, options) {
//
// If the file exceeds the buffer maximum length (~1 GB on 32-bit systems and ~2 GB
// on 64-bit systems), split the download into multiple segments
const maxSegmentSize = buffer.constants.MAX_LENGTH;
// ~2 GB = 2147483647, beyond this, we start getting out of range error. So, capping it accordingly.
const maxSegmentSize = Math.min(2147483647, buffer.constants.MAX_LENGTH);
const downloadProgress = new DownloadProgress(contentLength);
const fd = fs.openSync(archivePath, 'w');
try {
Expand Down Expand Up @@ -41451,6 +41449,15 @@ function checkKey(key) {
throw new ValidationError(`Key Validation Error: ${key} cannot contain commas.`);
}
}
/**
* isFeatureAvailable to check the presence of Actions cache service
*
* @returns boolean return true if Actions cache service feature is available, otherwise false
*/
function isFeatureAvailable() {
return !!process.env['ACTIONS_CACHE_URL'];
}
exports.isFeatureAvailable = isFeatureAvailable;
/**
* Restores cache from keys
*
Expand Down
3,533 changes: 1,776 additions & 1,757 deletions dist/setup/index.js

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setup-python",
"version": "2.2.2",
"version": "3.1.0",
"private": true,
"description": "Setup python action",
"main": "dist/index.js",
Expand All @@ -23,7 +23,7 @@
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/cache": "^1.0.8",
"@actions/cache": "^2.0.0",
"@actions/core": "^1.2.3",
"@actions/exec": "^1.1.0",
"@actions/glob": "^0.2.0",
Expand Down
7 changes: 2 additions & 5 deletions src/setup-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import * as finderPyPy from './find-pypy';
import * as path from 'path';
import * as os from 'os';
import {getCacheDistributor} from './cache-distributions/cache-factory';
import {isGhes} from './utils';
import {isCacheFeatureAvailable} from './utils';

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

async function cacheDependencies(cache: string, pythonVersion: string) {
if (isGhes()) {
throw new Error('Caching is not supported on GHES');
}
const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = getCacheDistributor(
Expand Down Expand Up @@ -43,7 +40,7 @@ async function run() {
}

const cache = core.getInput('cache');
if (cache) {
if (cache && isCacheFeatureAvailable()) {
await cacheDependencies(cache, pythonVersion);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
Expand Down Expand Up @@ -99,3 +101,21 @@ export function isGhes(): boolean {
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}

export function isCacheFeatureAvailable(): boolean {
if (!cache.isFeatureAvailable()) {
if (isGhes()) {
throw new Error(
'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.'
);
} else {
core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
}

return false;
}

return true;
}

0 comments on commit 05fb98d

Please sign in to comment.