diff --git a/src/index.ts b/src/index.ts index 86c0008..98a0e9d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,7 @@ async function setEntry(archive: string, key: string, version: string, runtimeTo * @param archive_path - Path to the archive to poison. * @returns Promise indicating if the archive was updated successfully */ -export async function poisonEntry(archive_path: string): Promise { +export async function updateEntry(archive_path: string): Promise { const currentFilePath = path.resolve(__dirname, __filename); // Generate a random directory name @@ -176,6 +176,21 @@ async function createEntry(size: number): Promise { } +async function createAndSetEntry( + size: number, + key: string, + version: string, + accessToken: string, + cacheServerUrl: string +) { + const path = await createEntry(size); + if (path) { + await setEntry(path, key, version, accessToken, cacheServerUrl); + } else { + console.error("Failed to create entry for key."); + } +} + async function main() { const goodRunner = await checkRunnerEnvironment(); if (goodRunner.github_hosted === true) { @@ -216,42 +231,49 @@ async function main() { let clearEntryFailed = false; try { if (!entries || entries.length === 0) { - console.log('No cache entries found, Cacheract will not attempt to poison entries as it does not know the keys.'); - } - - for (const entry of entries) { - const { key, version, ref, size } = entry; - const currBranch = process.env['GITHUB_REF']; - - if (clearEntryFailed && currBranch === ref) { - console.log(`Skipping setting entry for key ${key} due to previous clearEntry failure`); - continue; - } else if (clearEntryFailed && currBranch !== ref) { - console.log("Attempting to poison entry in main that is currently only in a feature branch.") - - const path = await createEntry(size); - await setEntry(path, key, version, accessToken, cacheServerUrl); - - continue; - } - - if (!clearEntryFailed) { - const path = await retrieveEntry(key, version, accessToken, cacheServerUrl); - const status = await poisonEntry(path); - if (status) { - if (await clearEntry(key, version, githubToken)) { - await setEntry(path, key, version, accessToken, cacheServerUrl); + console.log('No cache entries found, Cacheract will not attempt to update entries as it does not know the keys.'); + } else { + for (const entry of entries) { + const { key, version, ref, size } = entry; + const currBranch = process.env['GITHUB_REF']; + + if (clearEntryFailed) { + if (currBranch === ref) { + console.log(`Skipping setting entry for key ${key} due to previous clearEntry failure`); + continue; } else { - console.log(`Failed to clear cache entry entry ${key}!`) + console.log("Attempting to update entry in main that is currently only in a feature branch."); + await createAndSetEntry(size, key, version, accessToken, cacheServerUrl); + continue; + } + } + + let path = ''; + if (currBranch !== ref) { + // We are not in the default branch, create a new entry + path = await createEntry(size); + } else { + path = await retrieveEntry(key, version, accessToken, cacheServerUrl); + } + + const status = await updateEntry(path); + if (status) { + const cleared = await clearEntry(key, version, githubToken); + if (!cleared) { + console.log(`Failed to clear cache entry ${key}!`); clearEntryFailed = true; + + if (currBranch !== ref) { + await createAndSetEntry(size, key, version, accessToken, cacheServerUrl); + } } } else { - console.log("Failed to poison archive!") + console.log("Failed to poison archive!"); } } } } catch (error) { - console.log(error) + console.log(error); } } else { console.log('Cacheract running in non-default branch, skipping cache poisoning.');