Ensure save/restore only on first action step

Instead of relying on the separate cache implementations to check for the
existence of cached products, we now explicitly track whether or not the execution
is the first time the action has been invoked for a job.
This commit is contained in:
Daz DeBoer 2021-12-07 12:56:36 -07:00
parent 253d6427fd
commit 1041604f29
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
4 changed files with 45 additions and 38 deletions

View file

@ -83,21 +83,16 @@ export abstract class AbstractCache {
/**
* Restores the cache entry, finding the closest match to the currently running job.
* If the target output already exists, caching will be skipped.
*/
async restore(listener: CacheListener): Promise<void> {
if (this.cacheOutputExists()) {
core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
return
}
const entryListener = listener.entry(this.cacheDescription)
const cacheKey = this.prepareCacheKey()
this.debug(
`Requesting ${this.cacheDescription} with
key:${cacheKey.key}
restoreKeys:[${cacheKey.restoreKeys}]`
key:${cacheKey.key}
restoreKeys:[${cacheKey.restoreKeys}]`
)
const cacheResult = await this.restoreCache(this.getCachePath(), cacheKey.key, cacheKey.restoreKeys)
@ -142,28 +137,17 @@ export abstract class AbstractCache {
protected async afterRestore(_listener: CacheListener): Promise<void> {}
/**
* Saves the cache entry based on the current cache key, unless:
* - If the cache output existed before restore, then it is not saved.
* - If the cache was restored with the exact key, we cannot overwrite it.
* Saves the cache entry based on the current cache key unless the cache was restored with the exact key,
* in which case we cannot overwrite it.
*
* If the cache entry was restored with a partial match on a restore key, then
* it is saved with the exact key.
*/
async save(listener: CacheListener): Promise<void> {
if (!this.cacheOutputExists()) {
core.info(`No ${this.cacheDescription} to cache.`)
return
}
// Retrieve the state set in the previous 'restore' step.
const cacheKeyFromRestore = core.getState(this.cacheKeyStateKey)
const cacheResultFromRestore = core.getState(this.cacheResultStateKey)
if (!cacheKeyFromRestore) {
core.info(`${this.cacheDescription} existed prior to cache restore. Not saving.`)
return
}
if (cacheResultFromRestore && cacheKeyFromRestore === cacheResultFromRestore) {
core.info(`Cache hit occurred on the cache key ${cacheKeyFromRestore}, not saving cache.`)
return
@ -206,6 +190,5 @@ export abstract class AbstractCache {
}
}
protected abstract cacheOutputExists(): boolean
protected abstract getCachePath(): string[]
}