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

Add linux os release info to primary key #467

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions __tests__/cache-restore.test.ts
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache';
import * as exec from '@actions/exec';
import {getCacheDistributor} from '../src/cache-distributions/cache-factory';
import * as utils from './../src/utils';

describe('restore-cache', () => {
const pipFileLockHash =
Expand All @@ -28,6 +29,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
let saveSatetSpy: jest.SpyInstance;
let getStateSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance;
let getLinuxOSReleaseInfoSpy: jest.SpyInstance;

// cache spy
let restoreCacheSpy: jest.SpyInstance;
Expand Down Expand Up @@ -74,6 +76,8 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
return primaryKey;
}
);

getLinuxOSReleaseInfoSpy = jest.spyOn(utils, 'getLinuxOSReleaseInfo');
});

describe('Validate provided package manager', () => {
Expand Down Expand Up @@ -109,11 +113,24 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
pythonVersion,
dependencyFile
);

if (process.platform === 'linux') {
getLinuxOSReleaseInfoSpy.mockImplementation(() =>
Promise.resolve('Ubuntu-20.4')
);
}

await cacheDistributor.restoreCache();

expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
);
if (process.platform === 'linux') {
expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-Ubuntu-20.4-python-${pythonVersion}-${packageManager}-${fileHash}`
);
} else {
expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
);
}
},
30000
);
Expand Down
70 changes: 63 additions & 7 deletions dist/setup/index.js
Expand Up @@ -64430,8 +64430,19 @@ class PipCache extends cache_distributor_1.default {
computeKeys() {
return __awaiter(this, void 0, void 0, function* () {
const hash = yield glob.hashFiles(this.cacheDependencyPath);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
let primaryKey = '';
let restoreKey = '';
if (utils_1.IS_LINUX) {
console.log('here');
const osRelease = yield utils_1.getLinuxOSReleaseInfo();
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`;
}
else {
console.log('here2');
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
}
return {
primaryKey,
restoreKey: [restoreKey]
Expand Down Expand Up @@ -64485,6 +64496,7 @@ const glob = __importStar(__nccwpck_require__(8090));
const os = __importStar(__nccwpck_require__(2037));
const path = __importStar(__nccwpck_require__(1017));
const core = __importStar(__nccwpck_require__(2186));
const utils_1 = __nccwpck_require__(1314);
const cache_distributor_1 = __importDefault(__nccwpck_require__(8953));
class PipenvCache extends cache_distributor_1.default {
constructor(pythonVersion, patterns = '**/Pipfile.lock') {
Expand All @@ -64511,9 +64523,16 @@ class PipenvCache extends cache_distributor_1.default {
}
computeKeys() {
return __awaiter(this, void 0, void 0, function* () {
const hash = yield glob.hashFiles(this.patterns);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const hash = yield glob.hashFiles(this.cacheDependencyPath);
let primaryKey = '';
const restoreKey = undefined;
if (utils_1.IS_LINUX) {
const osRelease = yield utils_1.getLinuxOSReleaseInfo();
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
}
else {
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
}
return {
primaryKey,
restoreKey
Expand Down Expand Up @@ -64566,6 +64585,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const glob = __importStar(__nccwpck_require__(8090));
const path = __importStar(__nccwpck_require__(1017));
const exec = __importStar(__nccwpck_require__(1514));
const utils_1 = __nccwpck_require__(1314);
const cache_distributor_1 = __importDefault(__nccwpck_require__(8953));
class PoetryCache extends cache_distributor_1.default {
constructor(pythonVersion, patterns = '**/poetry.lock') {
Expand All @@ -64587,9 +64607,16 @@ class PoetryCache extends cache_distributor_1.default {
}
computeKeys() {
return __awaiter(this, void 0, void 0, function* () {
const hash = yield glob.hashFiles(this.patterns);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const hash = yield glob.hashFiles(this.cacheDependencyPath);
let primaryKey = '';
const restoreKey = undefined;
if (utils_1.IS_LINUX) {
const osRelease = yield utils_1.getLinuxOSReleaseInfo();
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
}
else {
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
}
return {
primaryKey,
restoreKey
Expand Down Expand Up @@ -65354,16 +65381,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod);
return result;
};
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
exports.getLinuxOSReleaseInfo = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const path = __importStar(__nccwpck_require__(1017));
const semver = __importStar(__nccwpck_require__(1383));
const exec = __importStar(__nccwpck_require__(1514));
exports.IS_WINDOWS = process.platform === 'win32';
exports.IS_LINUX = process.platform === 'linux';
exports.WINDOWS_ARCHS = ['x86', 'x64'];
Expand Down Expand Up @@ -65447,6 +65484,25 @@ function isCacheFeatureAvailable() {
return true;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getLinuxOSReleaseInfo() {
return __awaiter(this, void 0, void 0, function* () {
const versionId = yield exec.getExecOutput('lsb_release', ['-a'], {
silent: true
});
let osVersion = '';
let osRelease = '';
versionId.stdout.split('\n').forEach(elem => {
if (elem.includes('Distributor'))
osVersion = elem.split(':')[1].trim();
if (elem.includes('Release'))
osRelease = elem.split(':')[1].trim();
});
core.info(osRelease);
core.info(osVersion);
return `${osVersion}-${osRelease}`;
});
}
exports.getLinuxOSReleaseInfo = getLinuxOSReleaseInfo;


/***/ }),
Expand Down
17 changes: 14 additions & 3 deletions src/cache-distributions/pip-cache.ts
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path';
import os from 'os';

import CacheDistributor from './cache-distributor';
import {IS_WINDOWS} from '../utils';
import {getLinuxOSReleaseInfo, IS_LINUX, IS_WINDOWS} from '../utils';

class PipCache extends CacheDistributor {
constructor(
Expand Down Expand Up @@ -57,8 +57,19 @@ class PipCache extends CacheDistributor {

protected async computeKeys() {
const hash = await glob.hashFiles(this.cacheDependencyPath);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
let primaryKey = '';
let restoreKey = '';

if (IS_LINUX) {
console.log('here');
panticmilos marked this conversation as resolved.
Show resolved Hide resolved
const osRelease = await getLinuxOSReleaseInfo();
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`;
} else {
console.log('here2');
panticmilos marked this conversation as resolved.
Show resolved Hide resolved
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
}

return {
primaryKey,
Expand Down
13 changes: 11 additions & 2 deletions src/cache-distributions/pipenv-cache.ts
Expand Up @@ -2,6 +2,7 @@ import * as glob from '@actions/glob';
import * as os from 'os';
import * as path from 'path';
import * as core from '@actions/core';
import {getLinuxOSReleaseInfo, IS_LINUX} from '../utils';

import CacheDistributor from './cache-distributor';

Expand Down Expand Up @@ -31,9 +32,17 @@ class PipenvCache extends CacheDistributor {
}

protected async computeKeys() {
const hash = await glob.hashFiles(this.patterns);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const hash = await glob.hashFiles(this.cacheDependencyPath);
let primaryKey = '';
const restoreKey = undefined;

if (IS_LINUX) {
const osRelease = await getLinuxOSReleaseInfo();
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
} else {
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
}

return {
primaryKey,
restoreKey
Expand Down
14 changes: 11 additions & 3 deletions src/cache-distributions/poetry-cache.ts
@@ -1,7 +1,7 @@
import * as glob from '@actions/glob';
import * as os from 'os';
import * as path from 'path';
import * as exec from '@actions/exec';
import {getLinuxOSReleaseInfo, IS_LINUX} from '../utils';

import CacheDistributor from './cache-distributor';

Expand Down Expand Up @@ -32,9 +32,17 @@ class PoetryCache extends CacheDistributor {
}

protected async computeKeys() {
const hash = await glob.hashFiles(this.patterns);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const hash = await glob.hashFiles(this.cacheDependencyPath);
let primaryKey = '';
const restoreKey = undefined;

if (IS_LINUX) {
const osRelease = await getLinuxOSReleaseInfo();
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
} else {
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
}

return {
primaryKey,
restoreKey
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Expand Up @@ -3,6 +3,7 @@ import * as core from '@actions/core';
import fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import * as exec from '@actions/exec';

export const IS_WINDOWS = process.platform === 'win32';
export const IS_LINUX = process.platform === 'linux';
Expand Down Expand Up @@ -119,3 +120,20 @@ export function isCacheFeatureAvailable(): boolean {

return true;
}

export async function getLinuxOSReleaseInfo() {
const versionId = await exec.getExecOutput('lsb_release', ['-a'], {
panticmilos marked this conversation as resolved.
Show resolved Hide resolved
silent: true
});
panticmilos marked this conversation as resolved.
Show resolved Hide resolved
let osVersion = '';
let osRelease = '';

versionId.stdout.split('\n').forEach(elem => {
if (elem.includes('Distributor')) osVersion = elem.split(':')[1].trim();
if (elem.includes('Release')) osRelease = elem.split(':')[1].trim();
});
panticmilos marked this conversation as resolved.
Show resolved Hide resolved

core.info(osRelease);
core.info(osVersion);
panticmilos marked this conversation as resolved.
Show resolved Hide resolved
return `${osVersion}-${osRelease}`;
}