Only cache the wrapper ZIP, not the exploded dir

Prior to this change, the wrapper cache contained both the downloaded zip
file as well as the exploded wrapper dir. Only the zip file is required,
as Gradle will automatically detect and unpack.
This commit is contained in:
Daz DeBoer 2021-06-30 14:13:41 -06:00
parent 15a8123fbc
commit e4ec586f46
3 changed files with 55 additions and 45 deletions

2
dist/main/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/post/index.js vendored

File diff suppressed because one or more lines are too long

View file

@ -7,9 +7,7 @@ import * as cache from '@actions/cache'
import * as github from './github-utils' import * as github from './github-utils'
const WRAPPER_CACHE_KEY = 'WRAPPER_CACHE_KEY' const WRAPPER_SLUG = 'WRAPPER_SLUG'
const WRAPPER_CACHE_PATH = 'WRAPPER_CACHE_PATH'
const WRAPPER_CACHE_RESULT = 'WRAPPER_CACHE_RESULT'
export async function restoreCachedWrapperDist( export async function restoreCachedWrapperDist(
gradlewDirectory: string | null gradlewDirectory: string | null
@ -17,68 +15,62 @@ export async function restoreCachedWrapperDist(
if (isWrapperCacheDisabled()) return if (isWrapperCacheDisabled()) return
if (gradlewDirectory == null) return if (gradlewDirectory == null) return
const wrapperSlug = extractGradleWrapperSlugFrom( const wrapperProperties = path.join(
path.join( path.resolve(gradlewDirectory),
path.resolve(gradlewDirectory), 'gradle/wrapper/gradle-wrapper.properties'
'gradle/wrapper/gradle-wrapper.properties' )
const wrapperSlug = extractGradleWrapperSlugFrom(wrapperProperties)
if (!wrapperSlug) {
core.warning(
`Could not calculate wrapper version from ${wrapperProperties}`
) )
) return
if (!wrapperSlug) return }
const wrapperCacheKey = `wrapper-${wrapperSlug}` const wrapperDir = getWrapperDir(wrapperSlug)
const wrapperCachePath = path.resolve( const cacheKey = getCacheKey(wrapperSlug)
os.homedir(), const cachePath = getCachePath(wrapperSlug)
`.gradle/wrapper/dists/gradle-${wrapperSlug}`
)
if (fs.existsSync(wrapperCachePath)) return
core.saveState(WRAPPER_CACHE_KEY, wrapperCacheKey) // Check if the wrapper has already been downloaded to Gradle User Home
core.saveState(WRAPPER_CACHE_PATH, wrapperCachePath) if (fs.existsSync(wrapperDir)) return
try { try {
const restoredKey = await cache.restoreCache( const restoredKey = await cache.restoreCache([cachePath], cacheKey)
[wrapperCachePath],
wrapperCacheKey
)
if (!restoredKey) { if (restoredKey) {
core.info( core.info(
'Wrapper installation cache not found, expect a Gradle distribution download.' `Wrapper installation restored from cache key: ${restoredKey}`
) )
return } else {
core.info(
`Wrapper installation cache not found. Will download and cache with key: ${cacheKey}.`
)
// Save the slug to trigger caching of the downloaded wrapper
core.saveState(WRAPPER_SLUG, wrapperSlug)
} }
core.saveState(WRAPPER_CACHE_RESULT, restoredKey)
core.info(
`Wrapper installation restored from cache key: ${restoredKey}`
)
return
} catch (error) { } catch (error) {
core.info( core.info(
`Wrapper installation cache restore failed, expect a Gradle distribution download\n ${error}` `Wrapper installation cache restore failed for key: ${cacheKey}.\n ${error}`
) )
return
} }
} }
export async function cacheWrapperDist(): Promise<void> { export async function cacheWrapperDist(): Promise<void> {
if (isWrapperCacheDisabled()) return if (isWrapperCacheDisabled()) return
const cacheKey = core.getState(WRAPPER_CACHE_KEY) const wrapperSlug = core.getState(WRAPPER_SLUG)
const cachePath = core.getState(WRAPPER_CACHE_PATH) if (!wrapperSlug) return
const cacheResult = core.getState(WRAPPER_CACHE_RESULT)
if (!cachePath || !fs.existsSync(cachePath)) { const wrapperDir = getWrapperDir(wrapperSlug)
core.debug('No wrapper installation to cache.') const cacheKey = getCacheKey(wrapperSlug)
const cachePath = getCachePath(wrapperSlug)
if (!fs.existsSync(wrapperDir)) {
core.warning(`No wrapper installation to cache at ${wrapperDir}`)
return return
} }
if (cacheResult && cacheKey === cacheResult) { core.info(`Will cache wrapper zip ${cachePath} with key ${cacheKey}`)
core.info(
`Wrapper installation cache hit occurred on the cache key ${cacheKey}, not saving cache.`
)
return
}
try { try {
await cache.saveCache([cachePath], cacheKey) await cache.saveCache([cachePath], cacheKey)
@ -117,3 +109,21 @@ export function extractGradleWrapperSlugFromDistUri(
function isWrapperCacheDisabled(): boolean { function isWrapperCacheDisabled(): boolean {
return !github.inputBoolean('wrapper-cache-enabled', true) return !github.inputBoolean('wrapper-cache-enabled', true)
} }
function getCacheKey(wrapperSlug: string): string {
return `wrapper-v1-${wrapperSlug}`
}
function getWrapperDir(wrapperSlug: string): string {
return path.resolve(
os.homedir(),
`.gradle/wrapper/dists/gradle-${wrapperSlug}`
)
}
function getCachePath(wrapperSlug: string): string {
return path.resolve(
os.homedir(),
`.gradle/wrapper/dists/gradle-${wrapperSlug}/*/gradle-${wrapperSlug}.zip`
)
}