gradle-build-action/src/cache-gradle-user-home.ts

88 lines
2.4 KiB
TypeScript
Raw Normal View History

import path from 'path'
import fs from 'fs'
import os from 'os'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
2021-09-05 21:55:49 -04:00
import {generateCacheKey} from './cache-utils'
const CACHE_PATH = [
'~/.gradle/caches/*', // All directories in 'caches'
'~/.gradle/notifications/*', // Prevent the re-rendering of first-use message for version
'~/.gradle/wrapper/dists/*/*/*.zip' // Only wrapper zips are required : Gradle will expand these on demand
]
const CACHE_KEY = 'GUH_CACHE_KEY'
const CACHE_RESULT = 'GUH_CACHE_RESULT'
export async function restore(): Promise<void> {
if (gradleUserHomeExists()) {
2021-09-05 21:55:49 -04:00
core.info('Gradle User Home already exists. Not restoring from cache.')
return
}
const cacheKey = generateCacheKey('gradle')
core.saveState(CACHE_KEY, cacheKey.key)
const cacheResult = await cache.restoreCache(
CACHE_PATH,
cacheKey.key,
cacheKey.restoreKeys
)
if (!cacheResult) {
core.info(
'Gradle User Home cache not found. Will start with empty home.'
)
return
}
core.info(`Gradle User Home restored from cache key: ${cacheResult}`)
return
}
export async function save(): Promise<void> {
if (!gradleUserHomeExists()) {
core.debug('No Gradle User Home to cache.')
return
}
const cacheKey = core.getState(CACHE_KEY)
const cacheResult = core.getState(CACHE_RESULT)
if (!cacheKey) {
core.info(
'Gradle User Home existed prior to cache restore. Not saving.'
)
return
}
if (cacheResult && cacheKey === cacheResult) {
core.info(
`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`
)
return
}
core.info(`Caching Gradle User Home with cache key: ${cacheKey}`)
try {
await cache.saveCache(CACHE_PATH, cacheKey)
} catch (error) {
if (error.name === cache.ValidationError.name) {
throw error
} else if (error.name === cache.ReserveCacheError.name) {
core.info(error.message)
} else {
core.info(`[warning] ${error.message}`)
}
}
return
}
function gradleUserHomeExists(): boolean {
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
const dir = path.resolve(os.homedir(), '.gradle/caches')
return fs.existsSync(dir)
}