Skip to content

Commit

Permalink
use keys in pre step as well
Browse files Browse the repository at this point in the history
  • Loading branch information
rarmatei committed Dec 19, 2024
1 parent 9eaf9a6 commit bb63765
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
14 changes: 9 additions & 5 deletions workflow-steps/cache/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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}`) : [];

Expand Down
38 changes: 34 additions & 4 deletions workflow-steps/cache/output/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions workflow-steps/cache/output/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
3 changes: 0 additions & 3 deletions workflow-steps/cache/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down

0 comments on commit bb63765

Please sign in to comment.