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

Commit

Permalink
feat: add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Jan 18, 2022
1 parent 664ee0f commit dbfa07c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ when decompressing, the correct algorithm will be used.
## Terraform

Here is a little snippet allowing you to create your cache bucket with
**[Terraform](https://www.terraform.io/)**, which you should probably use :)
**[Terraform](https://www.terraform.io/)** _(which you should probably use)_:

```terraform
resource "google_storage_bucket" "ci_cache" {
Expand Down
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/gcs-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { CompressionMethod } from './tar-utils';

export interface ObjectMetadata {
updated: string;
metadata: CacheActionMetadata;
}

export interface CacheActionMetadata {
metadata: {
'Cache-Action-Compression-Method': CompressionMethod;
};
'Cache-Action-Compression-Method': CompressionMethod;
}
68 changes: 52 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as github from '@actions/github';
import { Storage, File, Bucket } from '@google-cloud/storage';
import { withFile as withTemporaryFile } from 'tmp-promise';

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

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

core.debug(exactFile.name);

if (exactFileExists) {
console.log(`🙌 Found exact match from cache: ${key}.`);
console.log(`🙌 Found exact match from cache for key '${key}'.`);
return [exactFile, 'exact'];
} else {
console.log(`🔸 No exact match found: ${key}.`);
console.log(`🔸 No exact match found for key '${key}'.`);
}

const [bucketFiles] = await bucket.getFiles({
prefix: `${folderPrefix}/${restoreKeys[restoreKeys.length - 1]}`,
});
const bucketFiles = await bucket
.getFiles({
prefix: `${folderPrefix}/${restoreKeys[restoreKeys.length - 1]}`,
})
.then(([files]) =>
files.sort(
(a, b) =>
new Date((b.metadata as ObjectMetadata).updated).getTime() -
new Date((a.metadata as ObjectMetadata).updated).getTime(),
),
);

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

for (const restoreKey of restoreKeys) {
const foundFile = bucketFiles.find((file) =>
file.name.startsWith(`${folderPrefix}/${restoreKey}`),
);

if (foundFile) {
console.log(`🤝 Found restore key match from cache: ${restoreKey}.`);
console.log(`🤝 Found match from cache for restore key '${restoreKey}'.`);
return [foundFile, 'partial'];
} else {
console.log(
`🔸 No cache candidate found for restore key: ${restoreKey}.`,
`🔸 No cache candidate found for restore key '${restoreKey}'.`,
);
}
}
Expand All @@ -60,6 +81,8 @@ async function main() {
throw err;
});

core.debug(bestMatchKind);

if (!bestMatch) {
saveState({
cacheHitKind: 'none',
Expand All @@ -69,13 +92,19 @@ async function main() {
return;
}

core.debug(bestMatch.name);

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

core.debug(JSON.stringify(bestMatchMetadata));

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

core.debug(compressionMethod);

if (!bestMatchMetadata || !compressionMethod) {
saveState({
cacheHitKind: 'none',
Expand All @@ -88,18 +117,25 @@ async function main() {
const workspace = process.env.GITHUB_WORKSPACE ?? process.cwd();

return withTemporaryFile(async (tmpFile) => {
console.log('🌐 Downloading cache archive from bucket...');
await bestMatch.download({
destination: tmpFile.path,
await core.group('🌐 Downloading cache archive from bucket', async () => {
console.log(`🔹 Downloading file '${bestMatch.name}'...`);

return bestMatch.download({
destination: tmpFile.path,
});
});

await core.group('🗜️ Extracting cache archive...', () =>
await core.group('🗜️ Extracting cache archive', () =>
extractTar(tmpFile.path, compressionMethod, workspace),
);

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

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

core.setOutput('cache-hit', 'true');
console.log('✅ Successfully restored cache.');
});
Expand Down
33 changes: 23 additions & 10 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ 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 @@ -27,6 +30,8 @@ async function main() {
const targetFileName = `${folderPrefix}/${inputs.key}.tar`;
const [targetFileExists] = await bucket.file(targetFileName).exists();

core.debug(targetFileName);

if (targetFileExists) {
console.log(
'🌀 Skipping uploading cache as it already exists (probably due to another job).',
Expand All @@ -43,24 +48,32 @@ async function main() {
.glob()
.then((files) => files.map((file) => path.relative(workspace, file)));

core.debug(JSON.stringify(paths));

return withTemporaryFile(async (tmpFile) => {
const compressionMethod = await core.group(
'🗜️ Creating cache archive...',
'🗜️ Creating cache archive',
() => createTar(tmpFile.path, paths, workspace),
);

console.log('🌐 Uploading cache archive to bucket...');

const metadata: CacheActionMetadata = {
metadata: {
'Cache-Action-Compression-Method': compressionMethod,
},
const customMetadata: CacheActionMetadata = {
'Cache-Action-Compression-Method': compressionMethod,
};

await bucket.upload(tmpFile.path, {
destination: targetFileName,
metadata,
core.debug(JSON.stringify(customMetadata));

await core.group('🌐 Uploading cache archive to bucket', async () => {
console.log(`🔹 Uploading file '${targetFileName}'...`);

await bucket.upload(tmpFile.path, {
destination: targetFileName,
metadata: {
metadata: customMetadata,
},
});
});

console.log('✅ Successfully saved cache.');
});
}

Expand Down

0 comments on commit dbfa07c

Please sign in to comment.