refactoring

This commit is contained in:
Sergey Dolin 2023-07-20 08:48:13 +02:00
parent 42a70ecdee
commit d6ad0fa4fe
3 changed files with 100 additions and 93 deletions

View file

@ -1,6 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import * as io from '@actions/io'; import * as io from '@actions/io';
import {addExecutablesToCache, IGoVersionInfo} from '../src/installer'; import * as tc from '@actions/tool-cache';
import path from 'path'; import path from 'path';
describe('Windows performance workaround', () => { describe('Windows performance workaround', () => {
@ -43,25 +43,18 @@ describe('Windows performance workaround', () => {
jest.clearAllMocks(); jest.clearAllMocks();
process.env['RUNNER_TOOL_CACHE'] = runnerToolCache; process.env['RUNNER_TOOL_CACHE'] = runnerToolCache;
}); });
// addExecutablesToCache uses 3rd party dependency toolkit.cache under the hood // cacheWindowsToolkitDir depends on implementation of tc.cacheDir
// that currently is implemented with RUNNER_TOOL_CACHE environment variable // with the assumption that target dir is passed by RUNNER_TOOL_CACHE environment variable
// Make sure the implementation has not been changed // Make sure the implementation has not been changed
it('addExecutablesToCache should depend on env[RUNNER_TOOL_CACHE]', async () => { it('addExecutablesToCache should depend on env[RUNNER_TOOL_CACHE]', async () => {
const info: IGoVersionInfo = {
type: 'dist',
downloadUrl: 'http://nowhere.com',
resolvedVersion: '1.2.3',
fileName: 'ignore'
};
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache1'; process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache1';
const cacheDir1 = await addExecutablesToCache('/qzx', info, 'arch'); const cacheDir1 = await tc.cacheDir('/qzx', 'go', '1.2.3', 'arch');
expect(cacheDir1).toBe( expect(cacheDir1).toBe(
path.join('/', 'faked-hostedtoolcache1', 'go', '1.2.3', 'arch') path.join('/', 'faked-hostedtoolcache1', 'go', '1.2.3', 'arch')
); );
process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache2'; process.env['RUNNER_TOOL_CACHE'] = '/faked-hostedtoolcache2';
const cacheDir2 = await addExecutablesToCache('/qzx', info, 'arch'); const cacheDir2 = await tc.cacheDir('/qzx', 'go', '1.2.3', 'arch');
expect(cacheDir2).toBe( expect(cacheDir2).toBe(
path.join('/', 'faked-hostedtoolcache2', 'go', '1.2.3', 'arch') path.join('/', 'faked-hostedtoolcache2', 'go', '1.2.3', 'arch')
); );

77
dist/setup/index.js vendored
View file

@ -61338,7 +61338,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.addExecutablesToCache = exports.getGo = void 0; exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0;
const tc = __importStar(__nccwpck_require__(7784)); const tc = __importStar(__nccwpck_require__(7784));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const path = __importStar(__nccwpck_require__(1017)); const path = __importStar(__nccwpck_require__(1017));
@ -61441,15 +61441,46 @@ function resolveVersionFromManifest(versionSpec, stable, auth, arch, manifest) {
} }
}); });
} }
function addExecutablesToCache(extPath, info, arch) { // for github hosted windows runner handle latency of OS drive
// by avoiding write operations to C:
function cacheWindowsDir(extPath, tool, version, arch) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
core.info('Adding to the cache ...'); if (os_1.default.platform() !== 'win32')
const cachedDir = yield tc.cacheDir(extPath, 'go', makeSemver(info.resolvedVersion), arch); return false;
core.info(`Successfully cached go to ${cachedDir}`); const isHosted = process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
return cachedDir; process.env['AGENT_ISSELFHOSTED'] === '0';
if (!isHosted)
return false;
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot)
return false;
if (!fs_1.default.existsSync('d:\\') || !fs_1.default.existsSync('c:\\'))
return false;
const actualToolCacheRoot = defaultToolCacheRoot
.replace('C:', 'D:')
.replace('c:', 'd:');
// make toolcache root to be on drive d:
process.env['RUNNER_TOOL_CACHE'] = actualToolCacheRoot;
const actualToolCacheDir = yield tc.cacheDir(extPath, tool, version, arch);
// create a link from c: to d:
const defaultToolCacheDir = actualToolCacheDir.replace(actualToolCacheRoot, defaultToolCacheRoot);
fs_1.default.mkdirSync(path.dirname(defaultToolCacheDir), { recursive: true });
fs_1.default.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
// make outer code to continue using toolcache as if it were installed on c:
// restore toolcache root to default drive c:
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
return defaultToolCacheDir;
});
}
function addExecutablesToToolCache(extPath, info, arch) {
return __awaiter(this, void 0, void 0, function* () {
const tool = 'go';
const version = makeSemver(info.resolvedVersion);
return ((yield cacheWindowsDir(extPath, tool, version, arch)) ||
(yield tc.cacheDir(extPath, tool, version, arch)));
}); });
} }
exports.addExecutablesToCache = addExecutablesToCache;
function installGoVersion(info, auth, arch) { function installGoVersion(info, auth, arch) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
@ -61464,34 +61495,10 @@ function installGoVersion(info, auth, arch) {
if (info.type === 'dist') { if (info.type === 'dist') {
extPath = path.join(extPath, 'go'); extPath = path.join(extPath, 'go');
} }
// for github hosted windows runner handle latency of OS drive core.info('Adding to the cache ...');
// by avoiding write operations to C: const toolCacheDir = yield addExecutablesToToolCache(extPath, info, arch);
if (!isWindows) core.info(`Successfully cached go to ${toolCacheDir}`);
return addExecutablesToCache(extPath, info, arch); return toolCacheDir;
const isHosted = process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
process.env['AGENT_ISSELFHOSTED'] === '0';
if (!isHosted)
return addExecutablesToCache(extPath, info, arch);
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot)
return addExecutablesToCache(extPath, info, arch);
if (!fs_1.default.existsSync('d:\\') || !fs_1.default.existsSync('c:\\'))
return addExecutablesToCache(extPath, info, arch);
const substitutedToolCacheRoot = defaultToolCacheRoot
.replace('C:', 'D:')
.replace('c:', 'd:');
// make toolcache root to be on drive d:
process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;
const actualToolCacheDir = yield addExecutablesToCache(extPath, info, arch);
// create a link from c: to d:
const defaultToolCacheDir = actualToolCacheDir.replace(substitutedToolCacheRoot, defaultToolCacheRoot);
fs_1.default.mkdirSync(path.dirname(defaultToolCacheDir), { recursive: true });
fs_1.default.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
// restore toolcache root to default drive c:
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
// make outer code to continue using toolcache as if it were installed on c:
return defaultToolCacheDir;
}); });
} }
function extractGoArchive(archivePath) { function extractGoArchive(archivePath) {

View file

@ -164,20 +164,60 @@ async function resolveVersionFromManifest(
} }
} }
export async function addExecutablesToCache( // for github hosted windows runner handle latency of OS drive
// by avoiding write operations to C:
async function cacheWindowsDir(
extPath: string,
tool: string,
version: string,
arch: string
): Promise<string | false> {
if (os.platform() !== 'win32') return false;
const isHosted =
process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
process.env['AGENT_ISSELFHOSTED'] === '0';
if (!isHosted) return false;
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot) return false;
if (!fs.existsSync('d:\\') || !fs.existsSync('c:\\')) return false;
const actualToolCacheRoot = defaultToolCacheRoot
.replace('C:', 'D:')
.replace('c:', 'd:');
// make toolcache root to be on drive d:
process.env['RUNNER_TOOL_CACHE'] = actualToolCacheRoot;
const actualToolCacheDir = await tc.cacheDir(extPath, tool, version, arch);
// create a link from c: to d:
const defaultToolCacheDir = actualToolCacheDir.replace(
actualToolCacheRoot,
defaultToolCacheRoot
);
fs.mkdirSync(path.dirname(defaultToolCacheDir), {recursive: true});
fs.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
// make outer code to continue using toolcache as if it were installed on c:
// restore toolcache root to default drive c:
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
return defaultToolCacheDir;
}
async function addExecutablesToToolCache(
extPath: string, extPath: string,
info: IGoVersionInfo, info: IGoVersionInfo,
arch: string arch: string
): Promise<string> { ): Promise<string> {
core.info('Adding to the cache ...'); const tool = 'go';
const cachedDir = await tc.cacheDir( const version = makeSemver(info.resolvedVersion);
extPath, return (
'go', (await cacheWindowsDir(extPath, tool, version, arch)) ||
makeSemver(info.resolvedVersion), (await tc.cacheDir(extPath, tool, version, arch))
arch
); );
core.info(`Successfully cached go to ${cachedDir}`);
return cachedDir;
} }
async function installGoVersion( async function installGoVersion(
@ -201,44 +241,11 @@ async function installGoVersion(
extPath = path.join(extPath, 'go'); extPath = path.join(extPath, 'go');
} }
// for github hosted windows runner handle latency of OS drive core.info('Adding to the cache ...');
// by avoiding write operations to C: const toolCacheDir = await addExecutablesToToolCache(extPath, info, arch);
core.info(`Successfully cached go to ${toolCacheDir}`);
if (!isWindows) return addExecutablesToCache(extPath, info, arch); return toolCacheDir;
const isHosted =
process.env['RUNNER_ENVIRONMENT'] === 'github-hosted' ||
process.env['AGENT_ISSELFHOSTED'] === '0';
if (!isHosted) return addExecutablesToCache(extPath, info, arch);
const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'];
if (!defaultToolCacheRoot) return addExecutablesToCache(extPath, info, arch);
if (!fs.existsSync('d:\\') || !fs.existsSync('c:\\'))
return addExecutablesToCache(extPath, info, arch);
const substitutedToolCacheRoot = defaultToolCacheRoot
.replace('C:', 'D:')
.replace('c:', 'd:');
// make toolcache root to be on drive d:
process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;
const actualToolCacheDir = await addExecutablesToCache(extPath, info, arch);
// create a link from c: to d:
const defaultToolCacheDir = actualToolCacheDir.replace(
substitutedToolCacheRoot,
defaultToolCacheRoot
);
fs.mkdirSync(path.dirname(defaultToolCacheDir), {recursive: true});
fs.symlinkSync(actualToolCacheDir, defaultToolCacheDir, 'junction');
core.info(`Created link ${defaultToolCacheDir} => ${actualToolCacheDir}`);
// restore toolcache root to default drive c:
process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
// make outer code to continue using toolcache as if it were installed on c:
return defaultToolCacheDir;
} }
export async function extractGoArchive(archivePath: string): Promise<string> { export async function extractGoArchive(archivePath: string): Promise<string> {