Restore/save configuration-cache data in first action

Previously, the action was restoring/saving the configuration-cache data for each
step that applied the action. In order to support Gradle invocations that are _not_
managed by the action, the configuration-cache restore is now performed in the initial
action step, and save is performed in the final post-action step.

The build root directories are recorded for each invocation via an init script.
This commit is contained in:
Daz DeBoer 2021-12-07 16:52:53 -07:00
parent 1041604f29
commit e1f84aa44d
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
6 changed files with 107 additions and 75 deletions

View file

@ -1,11 +1,15 @@
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import * as github from '@actions/github'
import path from 'path'
import fs from 'fs'
import {CacheListener} from './cache-reporting'
import {isCacheDebuggingEnabled, getCacheKeyPrefix, hashStrings, handleCacheFailure} from './cache-utils'
import {isCacheDebuggingEnabled, getCacheKeyPrefix, determineJobContext, handleCacheFailure} from './cache-utils'
const CACHE_PROTOCOL_VERSION = 'v5-'
const JOB_CONTEXT_PARAMETER = 'workflow-job-context'
export const META_FILE_DIR = '.gradle-build-action'
export const PROJECT_ROOTS_FILE = 'project-roots.txt'
/**
* Represents a key used to restore a cache entry.
@ -58,22 +62,17 @@ function generateCacheKey(cacheName: string): CacheKey {
return new CacheKey(cacheKey, [cacheKeyForJobContext, cacheKeyForJob, cacheKeyForOs])
}
function determineJobContext(): string {
// By default, we hash the full `matrix` data for the run, to uniquely identify this job invocation
// The only way we can obtain the `matrix` data is via the `workflow-job-context` parameter in action.yml.
const workflowJobContext = core.getInput(JOB_CONTEXT_PARAMETER)
return hashStrings([workflowJobContext])
}
export abstract class AbstractCache {
private cacheName: string
private cacheDescription: string
private cacheKeyStateKey: string
private cacheResultStateKey: string
protected readonly gradleUserHome: string
protected readonly cacheDebuggingEnabled: boolean
constructor(cacheName: string, cacheDescription: string) {
constructor(gradleUserHome: string, cacheName: string, cacheDescription: string) {
this.gradleUserHome = gradleUserHome
this.cacheName = cacheName
this.cacheDescription = cacheDescription
this.cacheKeyStateKey = `CACHE_KEY_${cacheName}`
@ -81,6 +80,16 @@ export abstract class AbstractCache {
this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
}
init(): void {
const actionCacheDir = path.resolve(this.gradleUserHome, '.gradle-build-action')
fs.mkdirSync(actionCacheDir, {recursive: true})
const initScriptsDir = path.resolve(this.gradleUserHome, 'init.d')
fs.mkdirSync(initScriptsDir, {recursive: true})
this.initializeGradleUserHome(this.gradleUserHome, initScriptsDir)
}
/**
* Restores the cache entry, finding the closest match to the currently running job.
*/
@ -191,4 +200,5 @@ export abstract class AbstractCache {
}
protected abstract getCachePath(): string[]
protected abstract initializeGradleUserHome(gradleUserHome: string, initScriptsDir: string): void
}