diff --git a/.github/workflows/frontend.yml b/.github/workflows/frontend.yml index 05fcbd78d4..478ff65744 100644 --- a/.github/workflows/frontend.yml +++ b/.github/workflows/frontend.yml @@ -15,6 +15,7 @@ jobs: env: CI: true BACKSTAGE_CACHE_DIR: /.backstage-build-cache + BACKSTAGE_CACHE_MAX_ENTRIES: 2 steps: - uses: actions/checkout@v2 diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 1b3b3e3900..f36154aa48 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -15,6 +15,7 @@ jobs: env: CI: true BACKSTAGE_CACHE_DIR: /.backstage-build-cache + BACKSTAGE_CACHE_MAX_ENTRIES: 2 steps: - uses: actions/checkout@v2 diff --git a/packages/cli/src/commands/build-cache/cache.ts b/packages/cli/src/commands/build-cache/cache.ts index da653a0331..df91598e1e 100644 --- a/packages/cli/src/commands/build-cache/cache.ts +++ b/packages/cli/src/commands/build-cache/cache.ts @@ -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; - archive?: (outputDir: string) => Promise; + archive?: (outputDir: string, maxEntries: number) => Promise; }; 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)); diff --git a/packages/cli/src/commands/build-cache/index.ts b/packages/cli/src/commands/build-cache/index.ts index 2aa2911116..8760d944af 100644 --- a/packages/cli/src/commands/build-cache/index.ts +++ b/packages/cli/src/commands/build-cache/index.ts @@ -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); } } }; diff --git a/packages/cli/src/commands/build-cache/options.ts b/packages/cli/src/commands/build-cache/options.ts index f70131ff0a..bad7d6c27e 100644 --- a/packages/cli/src/commands/build-cache/options.ts +++ b/packages/cli/src/commands/build-cache/options.ts @@ -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 { 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 }; }