From bb63765b687cc17bddd4a04e1f5b424303381577 Mon Sep 17 00:00:00 2001 From: Rares Matei Date: Thu, 19 Dec 2024 14:23:23 +0000 Subject: [PATCH] use keys in pre step as well --- workflow-steps/cache/main.ts | 14 +++++++---- workflow-steps/cache/output/main.js | 38 ++++++++++++++++++++++++++--- workflow-steps/cache/output/post.js | 3 --- workflow-steps/cache/post.ts | 3 --- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/workflow-steps/cache/main.ts b/workflow-steps/cache/main.ts index 6bdb2e4..c5b494f 100644 --- a/workflow-steps/cache/main.ts +++ b/workflow-steps/cache/main.ts @@ -2,10 +2,11 @@ import { createPromiseClient } from '@bufbuild/connect'; import { createConnectTransport } from '@bufbuild/connect-web'; import { CacheService } from './generated_protos/cache_connect'; import { RestoreRequest, RestoreResponse } from './generated_protos/cache_pb'; -import { hashKey } from './hashing-utils'; +import { buildCachePaths, hashKey } from './hashing-utils'; import { appendFileSync, writeFileSync, existsSync } from 'fs'; -const input_key = process.env.NX_CLOUD_INPUT_key; +const inputKey = process.env.NX_CLOUD_INPUT_key; +const inputPaths = process.env.NX_CLOUD_INPUT_paths; const baseBranch = process.env.NX_CLOUD_INPUT_base_branch || process.env['NX_CLOUD_INPUT_base-branch']; @@ -19,10 +20,13 @@ export const cacheClient = createPromiseClient( const currentBranch = process.env.NX_BRANCH; -if (!input_key) { - throw new Error('No cache restore key provided.'); +if (!inputKey || !inputPaths) { + throw new Error('No cache restore key or paths provided.'); } -const key = `${hashKey(input_key)}`; + +const paths = buildCachePaths(inputPaths); +const stringifiedPaths = paths.join(','); +const key = hashKey(`${inputKey}|${stringifiedPaths}`); const currentBranchKeys = [key].map((k) => `${currentBranch}-${k}`); const baseBranchKeys = baseBranch ? [key].map((k) => `${baseBranch}-${k}`) : []; diff --git a/workflow-steps/cache/output/main.js b/workflow-steps/cache/output/main.js index 2cb8dc4..e34d68d 100644 --- a/workflow-steps/cache/output/main.js +++ b/workflow-steps/cache/output/main.js @@ -5987,10 +5987,38 @@ function hashKey(key2) { function hash(input) { return crypto.createHash("sha256").update(input).digest("hex"); } +function buildCachePaths(inputPaths2) { + const directories = Array.from( + new Set( + inputPaths2.split("\n").filter((p) => p).map((p) => p.replace(/^~/, "..")).reduce( + (allPaths, currPath) => [...allPaths, ...expandPath(currPath)], + [] + ) + ) + ); + const invalidDirectories = directories.filter((dir) => !fs.existsSync(dir)); + if (invalidDirectories.length > 0) { + console.warn( + `The following paths are not valid or empty: +${invalidDirectories.join( + "\n" + )}` + ); + } + return directories; +} +function expandPath(pattern) { + const globExpandedPaths = import_glob.glob.sync(pattern); + if (globExpandedPaths.length == 0) { + return [pattern]; + } + return globExpandedPaths; +} // main.ts var import_fs = require("fs"); -var input_key = process.env.NX_CLOUD_INPUT_key; +var inputKey = process.env.NX_CLOUD_INPUT_key; +var inputPaths = process.env.NX_CLOUD_INPUT_paths; var baseBranch = process.env.NX_CLOUD_INPUT_base_branch || process.env["NX_CLOUD_INPUT_base-branch"]; var cacheClient = createPromiseClient( CacheService, @@ -5999,10 +6027,12 @@ var cacheClient = createPromiseClient( }) ); var currentBranch = process.env.NX_BRANCH; -if (!input_key) { - throw new Error("No cache restore key provided."); +if (!inputKey || !inputPaths) { + throw new Error("No cache restore key or paths provided."); } -var key = `${hashKey(input_key)}`; +var paths = buildCachePaths(inputPaths); +var stringifiedPaths = paths.join(","); +var key = hashKey(`${inputKey}|${stringifiedPaths}`); var currentBranchKeys = [key].map((k) => `${currentBranch}-${k}`); var baseBranchKeys = baseBranch ? [key].map((k) => `${baseBranch}-${k}`) : []; cacheClient.restore( diff --git a/workflow-steps/cache/output/post.js b/workflow-steps/cache/output/post.js index 21991fd..b532436 100644 --- a/workflow-steps/cache/output/post.js +++ b/workflow-steps/cache/output/post.js @@ -6017,9 +6017,6 @@ if (!!cacheWasHit) { baseUrl: "http://127.0.0.1:9000" }) ); - if (!inputKey || !inputPaths) { - throw new Error("No cache restore key or paths provided."); - } const paths = buildCachePaths(inputPaths); const stringifiedPaths = paths.join(","); const key = hashKey(`${inputKey}|${stringifiedPaths}`); diff --git a/workflow-steps/cache/post.ts b/workflow-steps/cache/post.ts index 574e1d2..35b5bc9 100644 --- a/workflow-steps/cache/post.ts +++ b/workflow-steps/cache/post.ts @@ -22,9 +22,6 @@ if (!!cacheWasHit) { }), ); - if (!inputKey || !inputPaths) { - throw new Error('No cache restore key or paths provided.'); - } const paths = buildCachePaths(inputPaths); const stringifiedPaths = paths.join(','); const key = hashKey(`${inputKey}|${stringifiedPaths}`);