Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
fix: pass target file name to state to avoid hash mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Jan 18, 2022
1 parent dbfa07c commit 7579cb0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 60 deletions.
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

58 changes: 26 additions & 32 deletions dist/post/index.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Inputs {
}

export function getInputs(): Inputs {
return {
const inputs = {
bucket: core.getInput('bucket', { required: true }),
path: core.getInput('path', { required: true }),
key: core.getInput('key', { required: true }),
Expand All @@ -17,4 +17,8 @@ export function getInputs(): Inputs {
.split(',')
.filter((path) => path),
};

core.debug(`Loaded inputs: ${JSON.stringify(inputs)}.`);

return inputs;
}
35 changes: 20 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { withFile as withTemporaryFile } from 'tmp-promise';

import { ObjectMetadata } from './gcs-utils';
import { getInputs } from './inputs';
import { CacheHitKindState, saveState, State } from './state';
import { CacheHitKindState, saveState } from './state';
import { extractTar } from './tar-utils';

async function getBestMatch(
Expand All @@ -18,7 +18,7 @@ async function getBestMatch(
const exactFile = bucket.file(`${folderPrefix}/${key}.tar`);
const [exactFileExists] = await exactFile.exists();

core.debug(exactFile.name);
core.debug(`Exact file name: ${exactFile.name}.`);

if (exactFileExists) {
console.log(`🙌 Found exact match from cache for key '${key}'.`);
Expand All @@ -41,12 +41,14 @@ async function getBestMatch(

if (core.isDebug()) {
core.debug(
JSON.stringify(
`Candidates: ${JSON.stringify(
bucketFiles.map((f) => ({
name: f.name,
metadata: f.metadata as ObjectMetadata,
metadata: {
updated: (f.metadata as ObjectMetadata).updated,
},
})),
),
)}.`,
);
}

Expand All @@ -72,6 +74,9 @@ async function main() {
const inputs = getInputs();
const bucket = new Storage().bucket(inputs.bucket);

const folderPrefix = `${github.context.repo.owner}/${github.context.repo.repo}`;
const exactFileName = `${folderPrefix}/${inputs.key}.tar`;

const [bestMatch, bestMatchKind] = await core
.group('🔍 Searching the best cache archive available', () =>
getBestMatch(bucket, inputs.key, inputs.restoreKeys),
Expand All @@ -81,34 +86,37 @@ async function main() {
throw err;
});

core.debug(bestMatchKind);
core.debug(`Best match kind: ${bestMatchKind}.`);

if (!bestMatch) {
saveState({
cacheHitKind: 'none',
targetFileName: exactFileName,
});
core.setOutput('cache-hit', 'false');
console.log('😢 No cache candidate found.');
return;
}

core.debug(bestMatch.name);
core.debug(`Best match name: ${bestMatch.name}.`);

const bestMatchMetadata = await bestMatch
.getMetadata()
.then(([metadata]) => metadata as ObjectMetadata);

core.debug(JSON.stringify(bestMatchMetadata));
core.debug(`Best match metadata: ${JSON.stringify(bestMatchMetadata)}.`);

const compressionMethod =
bestMatchMetadata?.metadata?.['Cache-Action-Compression-Method'];

core.debug(compressionMethod);
core.debug(`Best match compression method: ${compressionMethod}.`);

if (!bestMatchMetadata || !compressionMethod) {
saveState({
cacheHitKind: 'none',
targetFileName: exactFileName,
});

core.setOutput('cache-hit', 'false');
console.log('😢 No cache candidate found (missing metadata).');
return;
Expand All @@ -129,13 +137,10 @@ async function main() {
extractTar(tmpFile.path, compressionMethod, workspace),
);

const state: State = {
saveState({
cacheHitKind: bestMatchKind,
};

core.debug(compressionMethod);
saveState(state);

targetFileName: exactFileName,
});
core.setOutput('cache-hit', 'true');
console.log('✅ Successfully restored cache.');
});
Expand Down
14 changes: 4 additions & 10 deletions src/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as glob from '@actions/glob';
import { Storage } from '@google-cloud/storage';
import * as path from 'path';
Expand All @@ -14,9 +13,6 @@ async function main() {
const inputs = getInputs();
const state = getState();

core.debug(JSON.stringify(inputs));
core.debug(JSON.stringify(state));

if (state.cacheHitKind === 'exact') {
console.log(
'🌀 Skipping uploading cache as the cache was hit by exact match.',
Expand All @@ -25,12 +21,10 @@ async function main() {
}

const bucket = new Storage().bucket(inputs.bucket);
const folderPrefix = `${github.context.repo.owner}/${github.context.repo.repo}`;

const targetFileName = `${folderPrefix}/${inputs.key}.tar`;
const targetFileName = state.targetFileName;
const [targetFileExists] = await bucket.file(targetFileName).exists();

core.debug(targetFileName);
core.debug(`Target file name: ${targetFileName}.`);

if (targetFileExists) {
console.log(
Expand All @@ -48,7 +42,7 @@ async function main() {
.glob()
.then((files) => files.map((file) => path.relative(workspace, file)));

core.debug(JSON.stringify(paths));
core.debug(`Paths: ${JSON.stringify(paths)}.`);

return withTemporaryFile(async (tmpFile) => {
const compressionMethod = await core.group(
Expand All @@ -60,7 +54,7 @@ async function main() {
'Cache-Action-Compression-Method': compressionMethod,
};

core.debug(JSON.stringify(customMetadata));
core.debug(`Metadata: ${JSON.stringify(customMetadata)}.`);

await core.group('🌐 Uploading cache archive to bucket', async () => {
console.log(`🔹 Uploading file '${targetFileName}'...`);
Expand Down
11 changes: 10 additions & 1 deletion src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ export type CacheHitKindState = 'exact' | 'partial' | 'none';

export interface State {
cacheHitKind: CacheHitKindState;
targetFileName: string;
}

export function saveState(state: State): void {
core.debug(`Saving state: ${JSON.stringify(state)}.`);

core.saveState('cache-hit-kind', state.cacheHitKind);
core.saveState('target-file-name', state.targetFileName);
}

export function getState(): State {
return {
const state = {
cacheHitKind: core.getState('cache-hit-kind') as CacheHitKindState,
targetFileName: core.getState('target-file-name'),
};

core.debug(`Loaded state: ${JSON.stringify(state)}.`);

return state;
}

0 comments on commit 7579cb0

Please sign in to comment.