cli/commands/build-cache: make max entries configurable and set to 2 in CI

This commit is contained in:
Patrik Oldsberg
2020-03-27 16:59:40 +01:00
parent 56a7969eac
commit edd21b984e
5 changed files with 12 additions and 6 deletions
+1
View File
@@ -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
+1
View File
@@ -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 };
}