cli/commands/build-cache: make max entries configurable and set to 2 in CI
This commit is contained in:
@@ -15,6 +15,7 @@ jobs:
|
||||
env:
|
||||
CI: true
|
||||
BACKSTAGE_CACHE_DIR: <repoRoot>/.backstage-build-cache
|
||||
BACKSTAGE_CACHE_MAX_ENTRIES: 2
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
@@ -15,6 +15,7 @@ jobs:
|
||||
env:
|
||||
CI: true
|
||||
BACKSTAGE_CACHE_DIR: <repoRoot>/.backstage-build-cache
|
||||
BACKSTAGE_CACHE_MAX_ENTRIES: 2
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
@@ -20,13 +20,12 @@ import { runPlain, runCheck } from '../../helpers/run';
|
||||
import { Options } from './options';
|
||||
import { extractArchive, createArchive } from './archive';
|
||||
|
||||
const MAX_ENTRIES = 10;
|
||||
const INFO_FILE = '.backstage-build-cache';
|
||||
|
||||
export type CacheQueryResult = {
|
||||
hit: boolean;
|
||||
copy?: (outputDir: string) => Promise<void>;
|
||||
archive?: (outputDir: string) => Promise<void>;
|
||||
archive?: (outputDir: string, maxEntries: number) => Promise<void>;
|
||||
};
|
||||
|
||||
export type CacheKey = string[];
|
||||
@@ -83,7 +82,7 @@ export class Cache {
|
||||
return { hit: false };
|
||||
}
|
||||
|
||||
const archive = async (outputDir: string) => {
|
||||
const archive = async (outputDir: string, maxEntries: number) => {
|
||||
await writeCacheInfo(outputDir, { key });
|
||||
|
||||
const timestamp = new Date().toISOString().replace(/-|:|\..*/g, '');
|
||||
@@ -111,7 +110,7 @@ export class Cache {
|
||||
entries.unshift({ key, path: archiveName });
|
||||
|
||||
// Remove old cache entries
|
||||
const removedEntries = entries.splice(MAX_ENTRIES);
|
||||
const removedEntries = entries.splice(maxEntries);
|
||||
for (const entry of removedEntries) {
|
||||
try {
|
||||
await fs.remove(resolvePath(location, entry.path));
|
||||
|
||||
@@ -48,7 +48,7 @@ export default async (cmd: Command, args: string[]) => {
|
||||
|
||||
if (cacheResult.archive) {
|
||||
print('caching build output');
|
||||
await cacheResult.archive(options.output);
|
||||
await cacheResult.archive(options.output, options.maxCacheEntries);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -18,10 +18,13 @@ import { resolve as resolvePath } from 'path';
|
||||
import { Command } from 'commander';
|
||||
import { runPlain } from '../../helpers/run';
|
||||
|
||||
const DEFAULT_MAX_ENTRIES = 10;
|
||||
|
||||
export type Options = {
|
||||
inputs: string[];
|
||||
output: string;
|
||||
cacheDir: string;
|
||||
maxCacheEntries: number;
|
||||
repoRoot: string;
|
||||
};
|
||||
|
||||
@@ -38,5 +41,7 @@ export async function parseOptions(cmd: Command): Promise<Options> {
|
||||
const cacheDir = argTransformer(
|
||||
process.env.BACKSTAGE_CACHE_DIR || cmd.cacheDir,
|
||||
);
|
||||
return { inputs, output, cacheDir, repoRoot };
|
||||
const maxCacheEntries =
|
||||
Number(process.env.BACKSTAGE_CACHE_MAX_ENTRIES) || DEFAULT_MAX_ENTRIES;
|
||||
return { inputs, output, cacheDir, repoRoot, maxCacheEntries };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user