Skip to content

Commit

Permalink
fix: directly tar to cache folder
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnowack committed Apr 26, 2023
1 parent 5e299dc commit 1a87442
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 104 deletions.
25 changes: 3 additions & 22 deletions dist/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function restoreCache(paths, primaryKey, restoreKeys, lookupOnly) {
let archivePath = '';
try {
// path are needed to compute version
const cacheEntry = await (0, local_1.getLocalCacheEntry)(keys);
const cacheEntry = await (0, local_1.getLocalCacheEntry)(keys, compressionMethod);
if (!cacheEntry?.archiveLocation) {
// Cache not found
return undefined;
Expand Down Expand Up @@ -111,15 +111,6 @@ async function restoreCache(paths, primaryKey, restoreKeys, lookupOnly) {
core.warning(`Failed to restore: ${error.message}`);
}
}
finally {
// Try to delete the archive to save space
try {
await utils.unlinkFile(archivePath);
}
catch (error) {
core.debug(`Failed to delete archive: ${error.message}`);
}
}
return undefined;
}
exports.restoreCache = restoreCache;
Expand All @@ -144,7 +135,8 @@ async function saveCache(paths, key) {
// eslint-disable-next-line max-len
'Path Validation Error: Path(s) specified in the action for caching do(es) not exist, hence no cache is being saved.');
}
const archiveFolder = await utils.createTempDirectory();
const archiveFolder = (0, local_1.getLocalArchiveFolder)(key);
await io.mkdirP(archiveFolder);
const archivePath = path.join(archiveFolder, utils.getCacheFileName(compressionMethod));
core.debug(`Archive Path: ${archivePath}`);
try {
Expand All @@ -159,8 +151,6 @@ async function saveCache(paths, key) {
if (archiveFileSize > fileSizeLimit && !utils.isGhes()) {
throw new Error(`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.`);
}
core.debug(`Saving Cache (ID: ${key})`);
await io.cp(archivePath, await (0, local_1.getLocalArchivePath)(key));
}
catch (error) {
const typedError = error;
Expand All @@ -174,14 +164,5 @@ async function saveCache(paths, key) {
core.warning(`Failed to save: ${typedError.message}`);
}
}
finally {
// Try to delete the archive to save space
try {
await utils.unlinkFile(archivePath);
}
catch (error) {
core.debug(`Failed to delete archive: ${error.message}`);
}
}
}
exports.saveCache = saveCache;
5 changes: 3 additions & 2 deletions dist/cache/local.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CompressionMethod } from './constants';
interface CacheResult {
cacheKey: string;
archiveLocation: string;
}
export declare function getLocalCacheEntry(keys: string[]): Promise<CacheResult | undefined>;
export declare function getLocalArchivePath(key: string): Promise<string>;
export declare function getLocalCacheEntry(keys: string[], compressionMethod: CompressionMethod): Promise<CacheResult | undefined>;
export declare function getLocalArchiveFolder(key: string): string;
export {};
33 changes: 13 additions & 20 deletions dist/cache/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalArchivePath = exports.getLocalCacheEntry = void 0;
exports.getLocalArchiveFolder = exports.getLocalCacheEntry = void 0;
const path = __importStar(require("path"));
const io_1 = require("@actions/io");
const io_util_1 = require("@actions/io/lib/io-util");
const { GITHUB_REPOSITORY, RUNNER_TOOL_CACHE } = process.env;
async function getLocalCacheEntry(keys) {
if (!RUNNER_TOOL_CACHE) {
throw new TypeError('Expected RUNNER_TOOL_CACHE environment variable to be defined.');
}
if (!GITHUB_REPOSITORY) {
throw new TypeError('Expected GITHUB_REPOSITORY environment variable to be defined.');
}
const result = await keys.reduce(async (memo, key) => {
if (await memo)
const tar_1 = require("./tar");
async function getLocalCacheEntry(keys, compressionMethod) {
const cacheFileName = await (0, tar_1.getCacheFileName)(compressionMethod);
const result = await keys.reduce(async (asyncMemo, key) => {
const memo = await asyncMemo;
if (memo)
return memo;
const cacheDir = path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key);
const cacheDir = getLocalArchiveFolder(key);
if (!await (0, io_util_1.exists)(cacheDir))
return undefined;
const archiveLocation = path.join(cacheDir, 'cache.tgz');
const archiveLocation = path.join(cacheDir, cacheFileName);
if (!await (0, io_util_1.exists)(archiveLocation))
return undefined;
return {
Expand All @@ -52,16 +47,14 @@ async function getLocalCacheEntry(keys) {
return result;
}
exports.getLocalCacheEntry = getLocalCacheEntry;
async function getLocalArchivePath(key) {
function getLocalArchiveFolder(key) {
const { GITHUB_REPOSITORY, RUNNER_TOOL_CACHE } = process.env;
if (!RUNNER_TOOL_CACHE) {
throw new TypeError('Expected RUNNER_TOOL_CACHE environment variable to be defined.');
}
if (!GITHUB_REPOSITORY) {
throw new TypeError('Expected GITHUB_REPOSITORY environment variable to be defined.');
}
const cacheDir = path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key);
await (0, io_1.mkdirP)(cacheDir);
const archiveLocation = path.join(cacheDir, 'cache.tgz');
return archiveLocation;
return path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key);
}
exports.getLocalArchivePath = getLocalArchivePath;
exports.getLocalArchiveFolder = getLocalArchiveFolder;
2 changes: 2 additions & 0 deletions dist/cache/tar.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ArchiveTool } from './contracts';
import { CompressionMethod } from './constants';
export declare function getCacheFileName(compressionMethod: CompressionMethod, resolveTarPath?: Promise<ArchiveTool>): Promise<string>;
export declare function listTar(archivePath: string, compressionMethod: CompressionMethod): Promise<void>;
export declare function extractTar(archivePath: string, compressionMethod: CompressionMethod): Promise<void>;
export declare function createTar(archiveFolder: string, sourceDirectories: string[], compressionMethod: CompressionMethod): Promise<void>;
24 changes: 15 additions & 9 deletions dist/cache/tar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTar = exports.extractTar = exports.listTar = void 0;
exports.createTar = exports.extractTar = exports.listTar = exports.getCacheFileName = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const exec_1 = require("@actions/exec");
Expand Down Expand Up @@ -66,10 +66,20 @@ async function getTarPath() {
type: constants_1.ArchiveToolType.GNU,
};
}
async function getCacheFileName(compressionMethod, resolveTarPath = getTarPath()) {
const tarPath = await resolveTarPath;
const BSD_TAR_ZSTD = tarPath.type === constants_1.ArchiveToolType.BSD
&& compressionMethod !== constants_1.CompressionMethod.Gzip
&& IS_WINDOWS;
return BSD_TAR_ZSTD
? 'cache.tar'
: utils.getCacheFileName(compressionMethod);
}
exports.getCacheFileName = getCacheFileName;
// Return arguments for tar as per tarPath, compressionMethod, method type and os
function getTarArgs(tarPath, compressionMethod, type, archivePath = '') {
async function getTarArgs(tarPath, compressionMethod, type, archivePath = '') {
const args = [`"${tarPath.path}"`];
const cacheFileName = utils.getCacheFileName(compressionMethod);
const cacheFileName = path.join(archivePath, await getCacheFileName(compressionMethod, Promise.resolve(tarPath)));
const tarFile = 'cache.tar';
const workingDirectory = getWorkingDirectory();
// Speficic args for BSD tar on windows for workaround
Expand All @@ -79,11 +89,7 @@ function getTarArgs(tarPath, compressionMethod, type, archivePath = '') {
// Method specific args
switch (type) {
case 'create':
args.push('--posix', '-cf', BSD_TAR_ZSTD
? tarFile
: cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '--exclude', BSD_TAR_ZSTD
? tarFile
: cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '-P', '-C', workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '--files-from', constants_1.ManifestFilename);
args.push('--posix', '-cf', cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '-P', '-C', workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/'), '--files-from', constants_1.ManifestFilename);
break;
case 'extract':
args.push('-xf', BSD_TAR_ZSTD
Expand Down Expand Up @@ -239,7 +245,7 @@ exports.extractTar = extractTar;
async function createTar(archiveFolder, sourceDirectories, compressionMethod) {
// Write source directories to manifest.txt to avoid command length limits
(0, fs_1.writeFileSync)(path.join(archiveFolder, constants_1.ManifestFilename), sourceDirectories.join('\n'));
const commands = await getCommands(compressionMethod, 'create');
const commands = await getCommands(compressionMethod, 'create', archiveFolder);
await execCommands(commands, archiveFolder);
}
exports.createTar = createTar;
24 changes: 4 additions & 20 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as io from '@actions/io'
import * as utils from './cacheUtils'
import { createTar, extractTar, listTar } from './tar'
import { ValidationError, ReserveCacheError } from './errors'
import { getLocalCacheEntry, getLocalArchivePath } from './local'
import { getLocalCacheEntry, getLocalArchiveFolder } from './local'

function checkPaths(paths: string[]): void {
if (!paths || paths.length === 0) {
Expand Down Expand Up @@ -77,7 +77,7 @@ export async function restoreCache(
let archivePath = ''
try {
// path are needed to compute version
const cacheEntry = await getLocalCacheEntry(keys)
const cacheEntry = await getLocalCacheEntry(keys, compressionMethod)
if (!cacheEntry?.archiveLocation) {
// Cache not found
return undefined
Expand Down Expand Up @@ -113,13 +113,6 @@ export async function restoreCache(
// Supress all non-validation cache related errors because caching should be optional
core.warning(`Failed to restore: ${(error as Error).message}`)
}
} finally {
// Try to delete the archive to save space
try {
await utils.unlinkFile(archivePath)
} catch (error) {
core.debug(`Failed to delete archive: ${(error as Error).message}`)
}
}

return undefined
Expand Down Expand Up @@ -153,7 +146,8 @@ export async function saveCache(
)
}

const archiveFolder = await utils.createTempDirectory()
const archiveFolder = getLocalArchiveFolder(key)
await io.mkdirP(archiveFolder)
const archivePath = path.join(
archiveFolder,
utils.getCacheFileName(compressionMethod),
Expand All @@ -178,9 +172,6 @@ export async function saveCache(
)} MB (${archiveFileSize} B) is over the 10GB limit, not saving cache.`,
)
}

core.debug(`Saving Cache (ID: ${key})`)
await io.cp(archivePath, await getLocalArchivePath(key))
} catch (error) {
const typedError = error as Error
if (typedError.name === ValidationError.name) {
Expand All @@ -190,12 +181,5 @@ export async function saveCache(
} else {
core.warning(`Failed to save: ${typedError.message}`)
}
} finally {
// Try to delete the archive to save space
try {
await utils.unlinkFile(archivePath)
} catch (error) {
core.debug(`Failed to delete archive: ${(error as Error).message}`)
}
}
}
36 changes: 15 additions & 21 deletions src/cache/local.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import * as path from 'path'
import { mkdirP } from '@actions/io'
import { exists } from '@actions/io/lib/io-util'
import { CompressionMethod } from './constants'
import { getCacheFileName } from './tar'

interface CacheResult {
cacheKey: string,
archiveLocation: string,
}

const { GITHUB_REPOSITORY, RUNNER_TOOL_CACHE } = process.env

export async function getLocalCacheEntry(keys: string[]): Promise<CacheResult | undefined> {
if (!RUNNER_TOOL_CACHE) {
throw new TypeError('Expected RUNNER_TOOL_CACHE environment variable to be defined.')
}

if (!GITHUB_REPOSITORY) {
throw new TypeError('Expected GITHUB_REPOSITORY environment variable to be defined.')
}

const result = await keys.reduce<Promise<CacheResult | undefined>>(async (memo, key) => {
if (await memo) return memo
const cacheDir = path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key)
export async function getLocalCacheEntry(
keys: string[],
compressionMethod: CompressionMethod,
): Promise<CacheResult | undefined> {
const cacheFileName = await getCacheFileName(compressionMethod)
const result = await keys.reduce<Promise<CacheResult | undefined>>(async (asyncMemo, key) => {
const memo = await asyncMemo
if (memo) return memo
const cacheDir = getLocalArchiveFolder(key)
if (!await exists(cacheDir)) return undefined
const archiveLocation = path.join(cacheDir, 'cache.tgz')
const archiveLocation = path.join(cacheDir, cacheFileName)
if (!await exists(archiveLocation)) return undefined
return {
cacheKey: key,
Expand All @@ -32,7 +28,8 @@ export async function getLocalCacheEntry(keys: string[]): Promise<CacheResult |
return result
}

export async function getLocalArchivePath(key: string): Promise<string> {
export function getLocalArchiveFolder(key: string) {
const { GITHUB_REPOSITORY, RUNNER_TOOL_CACHE } = process.env
if (!RUNNER_TOOL_CACHE) {
throw new TypeError('Expected RUNNER_TOOL_CACHE environment variable to be defined.')
}
Expand All @@ -41,8 +38,5 @@ export async function getLocalArchivePath(key: string): Promise<string> {
throw new TypeError('Expected GITHUB_REPOSITORY environment variable to be defined.')
}

const cacheDir = path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key)
await mkdirP(cacheDir)
const archiveLocation = path.join(cacheDir, 'cache.tgz')
return archiveLocation
return path.join(RUNNER_TOOL_CACHE, GITHUB_REPOSITORY, key)
}
30 changes: 20 additions & 10 deletions src/cache/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,31 @@ async function getTarPath(): Promise<ArchiveTool> {
} as ArchiveTool
}

export async function getCacheFileName(
compressionMethod: CompressionMethod,
resolveTarPath = getTarPath(),
) {
const tarPath = await resolveTarPath
const BSD_TAR_ZSTD = tarPath.type === ArchiveToolType.BSD
&& compressionMethod !== CompressionMethod.Gzip
&& IS_WINDOWS
return BSD_TAR_ZSTD
? 'cache.tar'
: utils.getCacheFileName(compressionMethod)
}

// Return arguments for tar as per tarPath, compressionMethod, method type and os
function getTarArgs(
async function getTarArgs(
tarPath: ArchiveTool,
compressionMethod: CompressionMethod,
type: string,
archivePath = '',
): Promise<string[]> {
const args = [`"${tarPath.path}"`]
const cacheFileName = utils.getCacheFileName(compressionMethod)
const cacheFileName = path.join(archivePath, await getCacheFileName(
compressionMethod,
Promise.resolve(tarPath),
))
const tarFile = 'cache.tar'
const workingDirectory = getWorkingDirectory()
// Speficic args for BSD tar on windows for workaround
Expand All @@ -71,13 +87,7 @@ function getTarArgs(
args.push(
'--posix',
'-cf',
BSD_TAR_ZSTD
? tarFile
: cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'--exclude',
BSD_TAR_ZSTD
? tarFile
: cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'-P',
'-C',
workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
Expand Down Expand Up @@ -290,6 +300,6 @@ export async function createTar(
path.join(archiveFolder, ManifestFilename),
sourceDirectories.join('\n'),
)
const commands = await getCommands(compressionMethod, 'create')
const commands = await getCommands(compressionMethod, 'create', archiveFolder)
await execCommands(commands, archiveFolder)
}

0 comments on commit 1a87442

Please sign in to comment.