mirror of
https://github.com/gradle/gradle-build-action.git
synced 2025-06-05 07:46:11 +02:00
Remove old caching code
This commit is contained in:
parent
5340f6e816
commit
d9cc0aeccf
8 changed files with 0 additions and 493 deletions
|
@ -1,113 +0,0 @@
|
|||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
import * as core from '@actions/core'
|
||||
import * as cache from '@actions/cache'
|
||||
|
||||
import * as cacheUtils from './cache-utils'
|
||||
|
||||
import {
|
||||
inputCacheKeyGlobs,
|
||||
tryDeleteFiles,
|
||||
isDependenciesCacheDisabled
|
||||
} from './cache-dependencies'
|
||||
|
||||
const CONFIGURATION_CACHE_PATH = 'CONFIGURATION_CACHE_PATH'
|
||||
const CONFIGURATION_CACHE_KEY = 'CONFIGURATION_CACHE_KEY'
|
||||
const CONFIGURATION_CACHE_RESULT = 'CONFIGURATION_CACHE_RESULT'
|
||||
|
||||
export async function restoreCachedConfiguration(
|
||||
rootDir: string
|
||||
): Promise<void> {
|
||||
if (isConfigurationCacheDisabled()) return
|
||||
|
||||
if (isDependenciesCacheDisabled()) {
|
||||
throw new Error(
|
||||
`Must enable dependencies-cache when configuration-cache is enabled`
|
||||
)
|
||||
}
|
||||
|
||||
const cachePath = path.resolve(rootDir, '.gradle/configuration-cache')
|
||||
if (fs.existsSync(cachePath)) return
|
||||
core.saveState(CONFIGURATION_CACHE_PATH, cachePath)
|
||||
|
||||
const inputCacheExact = core.getBooleanInput('configuration-cache-exact')
|
||||
const cacheKeyPrefix = 'configuration|'
|
||||
|
||||
const args = core.getInput('arguments')
|
||||
const argsKey = cacheUtils.truncateArgs(args)
|
||||
const cacheKeyWithArgs = `${cacheKeyPrefix}${argsKey}|`
|
||||
|
||||
const cacheKeyGlobs = inputCacheKeyGlobs('configuration-cache-key')
|
||||
const hash = await cacheUtils.hashFiles(rootDir, cacheKeyGlobs)
|
||||
const cacheKey = `${cacheKeyWithArgs}${hash}`
|
||||
|
||||
core.saveState(CONFIGURATION_CACHE_KEY, cacheKey)
|
||||
|
||||
const cacheResult = await cache.restoreCache(
|
||||
[cachePath],
|
||||
cacheKey,
|
||||
inputCacheExact ? [] : [cacheKeyWithArgs, cacheKeyPrefix]
|
||||
)
|
||||
|
||||
if (!cacheResult) {
|
||||
core.info(
|
||||
'Configuration cache not found, expect task graph calculation.'
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
core.saveState(CONFIGURATION_CACHE_RESULT, cacheResult)
|
||||
core.info(`Configuration restored from cache key: ${cacheResult}`)
|
||||
return
|
||||
}
|
||||
|
||||
export async function cacheConfiguration(): Promise<void> {
|
||||
if (isConfigurationCacheDisabled()) return
|
||||
|
||||
const cachePath = core.getState(CONFIGURATION_CACHE_PATH)
|
||||
const cacheKey = core.getState(CONFIGURATION_CACHE_KEY)
|
||||
const cacheResult = core.getState(CONFIGURATION_CACHE_RESULT)
|
||||
|
||||
if (!cachePath || !fs.existsSync(cachePath)) {
|
||||
core.debug('No configuration to cache.')
|
||||
return
|
||||
}
|
||||
|
||||
if (cacheResult && cacheKey === cacheResult) {
|
||||
core.info(
|
||||
`Configuration cache hit occurred on the cache key ${cacheKey}, not saving cache.`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const locksDeleted = tryDeleteFiles([
|
||||
path.resolve(cachePath, 'configuration-cache.lock')
|
||||
])
|
||||
if (!locksDeleted) {
|
||||
core.warning(
|
||||
'Unable to delete configuration lock files, try using --no-daemon or stopping the daemon last if you have several Gradle steps, not saving cache.'
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
core.info(`Will cache configuration with key ${cacheKey}`)
|
||||
|
||||
try {
|
||||
await cache.saveCache([cachePath], 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 isConfigurationCacheDisabled(): boolean {
|
||||
return !core.getBooleanInput('configuration-cache-enabled')
|
||||
}
|
|
@ -1,126 +0,0 @@
|
|||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
|
||||
import * as core from '@actions/core'
|
||||
import * as cache from '@actions/cache'
|
||||
|
||||
import * as cacheUtils from './cache-utils'
|
||||
|
||||
const DEPENDENCIES_CACHE_PATH = 'DEPENDENCIES_CACHE_PATH'
|
||||
const DEPENDENCIES_CACHE_KEY = 'DEPENDENCIES_CACHE_KEY'
|
||||
const DEPENDENCIES_CACHE_RESULT = 'DEPENDENCIES_CACHE_RESULT'
|
||||
|
||||
export async function restoreCachedDependencies(
|
||||
rootDir: string
|
||||
): Promise<void> {
|
||||
if (isDependenciesCacheDisabled()) return
|
||||
|
||||
const cachePath = path.resolve(os.homedir(), '.gradle/caches/modules-2')
|
||||
if (fs.existsSync(cachePath)) return
|
||||
core.saveState(DEPENDENCIES_CACHE_PATH, cachePath)
|
||||
|
||||
const inputCacheExact = core.getBooleanInput('dependencies-cache-exact')
|
||||
const cacheKeyPrefix = 'dependencies|'
|
||||
|
||||
const args = core.getInput('arguments')
|
||||
const argsKey = cacheUtils.truncateArgs(args)
|
||||
const cacheKeyWithArgs = `${cacheKeyPrefix}${argsKey}|`
|
||||
|
||||
const cacheKeyGlobs = inputCacheKeyGlobs('dependencies-cache-key')
|
||||
const hash = await cacheUtils.hashFiles(rootDir, cacheKeyGlobs)
|
||||
const cacheKey = `${cacheKeyWithArgs}${hash}`
|
||||
|
||||
core.saveState(DEPENDENCIES_CACHE_KEY, cacheKey)
|
||||
|
||||
const cacheResult = await cache.restoreCache(
|
||||
[cachePath],
|
||||
cacheKey,
|
||||
inputCacheExact ? [] : [cacheKeyWithArgs, cacheKeyPrefix]
|
||||
)
|
||||
|
||||
if (!cacheResult) {
|
||||
core.info('Dependencies cache not found, expect dependencies download.')
|
||||
return
|
||||
}
|
||||
|
||||
core.saveState(DEPENDENCIES_CACHE_RESULT, cacheResult)
|
||||
core.info(`Dependencies restored from cache key: ${cacheResult}`)
|
||||
return
|
||||
}
|
||||
|
||||
export async function cacheDependencies(): Promise<void> {
|
||||
if (isDependenciesCacheDisabled()) return
|
||||
|
||||
const cachePath = core.getState(DEPENDENCIES_CACHE_PATH)
|
||||
const cacheKey = core.getState(DEPENDENCIES_CACHE_KEY)
|
||||
const cacheResult = core.getState(DEPENDENCIES_CACHE_RESULT)
|
||||
|
||||
if (!cachePath || !fs.existsSync(cachePath)) {
|
||||
core.debug('No dependencies to cache.')
|
||||
return
|
||||
}
|
||||
|
||||
if (cacheResult && cacheKey === cacheResult) {
|
||||
core.info(
|
||||
`Dependencies cache hit occurred on the cache key ${cacheKey}, not saving cache.`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const locksDeleted = tryDeleteFiles([
|
||||
path.resolve(cachePath, 'modules-2.lock')
|
||||
])
|
||||
if (!locksDeleted) {
|
||||
core.warning(
|
||||
'Unable to delete dependencies lock files, try using --no-daemon or stopping the daemon last if you have several Gradle steps, not saving cache.'
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
core.info(`Will cache dependencies with key ${cacheKey}`)
|
||||
|
||||
try {
|
||||
await cache.saveCache([cachePath], 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
|
||||
}
|
||||
|
||||
export function tryDeleteFiles(filePaths: string[]): boolean {
|
||||
let failure = false
|
||||
for (const filePath of filePaths) {
|
||||
if (fs.existsSync(filePath)) {
|
||||
try {
|
||||
fs.unlinkSync(filePath)
|
||||
} catch (error) {
|
||||
failure = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return !failure
|
||||
}
|
||||
|
||||
export function isDependenciesCacheDisabled(): boolean {
|
||||
return !core.getBooleanInput('dependencies-cache-enabled')
|
||||
}
|
||||
|
||||
export function inputCacheKeyGlobs(input: string): string[] {
|
||||
const inputValue = core.getMultilineInput(input)
|
||||
return inputValue.length > 0
|
||||
? inputValue
|
||||
: [
|
||||
'**/*.gradle',
|
||||
'**/*.gradle.kts',
|
||||
'**/gradle.properties',
|
||||
'gradle/**'
|
||||
]
|
||||
}
|
|
@ -1,17 +1,3 @@
|
|||
import * as path from 'path'
|
||||
import * as glob from '@actions/glob'
|
||||
|
||||
export async function hashFiles(
|
||||
baseDir: string,
|
||||
patterns: string[] = ['**'],
|
||||
followSymbolicLinks = false
|
||||
): Promise<string | null> {
|
||||
const combinedPatterns = patterns
|
||||
.map(pattern => `${baseDir}${path.sep}${pattern}`)
|
||||
.join('\n')
|
||||
return glob.hashFiles(combinedPatterns, {followSymbolicLinks})
|
||||
}
|
||||
|
||||
export function truncateArgs(args: string): string {
|
||||
return args.trim().replace(/\s+/g, ' ').substr(0, 400)
|
||||
}
|
||||
|
|
|
@ -1,132 +0,0 @@
|
|||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
import * as os from 'os'
|
||||
|
||||
import * as core from '@actions/core'
|
||||
import * as cache from '@actions/cache'
|
||||
|
||||
const WRAPPER_SLUG = 'WRAPPER_SLUG'
|
||||
|
||||
export async function restoreCachedWrapperDist(
|
||||
gradlewDirectory: string | null
|
||||
): Promise<void> {
|
||||
if (isWrapperCacheDisabled()) return
|
||||
if (gradlewDirectory == null) return
|
||||
|
||||
const wrapperProperties = path.join(
|
||||
path.resolve(gradlewDirectory),
|
||||
'gradle/wrapper/gradle-wrapper.properties'
|
||||
)
|
||||
const wrapperSlug = extractGradleWrapperSlugFrom(wrapperProperties)
|
||||
if (!wrapperSlug) {
|
||||
core.warning(
|
||||
`Could not calculate wrapper version from ${wrapperProperties}`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const wrapperDir = getWrapperDir(wrapperSlug)
|
||||
const cacheKey = getCacheKey(wrapperSlug)
|
||||
const cachePath = getCachePath(wrapperSlug)
|
||||
|
||||
// Check if the wrapper has already been downloaded to Gradle User Home
|
||||
if (fs.existsSync(wrapperDir)) return
|
||||
|
||||
try {
|
||||
const restoredKey = await cache.restoreCache([cachePath], cacheKey)
|
||||
|
||||
if (restoredKey) {
|
||||
core.info(
|
||||
`Wrapper installation restored from cache key: ${restoredKey}`
|
||||
)
|
||||
} 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)
|
||||
}
|
||||
} catch (error) {
|
||||
core.info(
|
||||
`Wrapper installation cache restore failed for key: ${cacheKey}.\n ${error}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function cacheWrapperDist(): Promise<void> {
|
||||
if (isWrapperCacheDisabled()) return
|
||||
|
||||
const wrapperSlug = core.getState(WRAPPER_SLUG)
|
||||
if (!wrapperSlug) return
|
||||
|
||||
const wrapperDir = getWrapperDir(wrapperSlug)
|
||||
const cacheKey = getCacheKey(wrapperSlug)
|
||||
const cachePath = getCachePath(wrapperSlug)
|
||||
|
||||
if (!fs.existsSync(wrapperDir)) {
|
||||
core.warning(`No wrapper installation to cache at ${wrapperDir}`)
|
||||
return
|
||||
}
|
||||
|
||||
core.info(`Will cache wrapper zip ${cachePath} with key ${cacheKey}`)
|
||||
|
||||
try {
|
||||
await cache.saveCache([cachePath], 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
|
||||
}
|
||||
|
||||
export function extractGradleWrapperSlugFrom(
|
||||
wrapperProperties: string
|
||||
): string | null {
|
||||
const props = fs.readFileSync(wrapperProperties, {encoding: 'utf8'})
|
||||
const distUrlLine = props
|
||||
.split('\n')
|
||||
.find(line => line.startsWith('distributionUrl'))
|
||||
if (!distUrlLine) return null
|
||||
return extractGradleWrapperSlugFromDistUri(distUrlLine.substr(16).trim())
|
||||
}
|
||||
|
||||
export function extractGradleWrapperSlugFromDistUri(
|
||||
distUri: string
|
||||
): string | null {
|
||||
const regex = /.*gradle-(.*-(bin|all))\.zip/
|
||||
const match = distUri.match(regex)
|
||||
return match ? match[1] : null
|
||||
}
|
||||
|
||||
function isWrapperCacheDisabled(): boolean {
|
||||
// Check if either 'distributions' or 'wrapper' cache has been disabled
|
||||
const wrapperCacheEnabled = core.getBooleanInput('wrapper-cache-enabled')
|
||||
const distributionsCacheEnabled = core.getBooleanInput(
|
||||
'distributions-cache-enabled'
|
||||
)
|
||||
return !wrapperCacheEnabled || !distributionsCacheEnabled
|
||||
}
|
||||
|
||||
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`
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue