mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-11-22 09:02:50 +00:00
Add caching of Gradle build configuration cache
This commit is contained in:
parent
806543fb3a
commit
4c7d97cca4
7 changed files with 125 additions and 13 deletions
|
@ -32,6 +32,15 @@ inputs:
|
|||
dependencies-cache-exact:
|
||||
description: Whether to restore only if exact match, default to 'false'
|
||||
required: false
|
||||
configuration-cache-enabled:
|
||||
description: Whether caching build configuration is enabled or not, default to 'false'
|
||||
required: false
|
||||
configuration-cache-key:
|
||||
description: Globs of files to hash in the build root directory, separated by new lines, use best-effort if unset
|
||||
required: false
|
||||
configuration-cache-exact:
|
||||
description: Whether to restore only if exact match, default to 'false'
|
||||
required: false
|
||||
|
||||
outputs:
|
||||
build-scan-url:
|
||||
|
|
2
dist/main/index.js
vendored
2
dist/main/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/post/index.js
vendored
2
dist/post/index.js
vendored
File diff suppressed because one or more lines are too long
96
src/cache-configuration.ts
Normal file
96
src/cache-configuration.ts
Normal file
|
@ -0,0 +1,96 @@
|
|||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
import * as core from '@actions/core'
|
||||
import * as cache from '@actions/cache'
|
||||
|
||||
import * as github from './github-utils'
|
||||
import * as crypto from './crypto-utils'
|
||||
|
||||
import {inputCacheKeyGlobs, tryDeleteFiles} 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
|
||||
|
||||
const cachePath = path.resolve(rootDir, '.gradle/configuration-cache')
|
||||
core.saveState(CONFIGURATION_CACHE_PATH, cachePath)
|
||||
|
||||
const inputCacheExact = github.inputBoolean('configuration-cache-exact')
|
||||
const cacheKeyGlobs = inputCacheKeyGlobs('configuration-cache-key')
|
||||
|
||||
const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
|
||||
const cacheKeyPrefix = 'configuration-'
|
||||
const cacheKey = `${cacheKeyPrefix}${hash}`
|
||||
core.saveState(CONFIGURATION_CACHE_KEY, cacheKey)
|
||||
|
||||
const cacheResult = await cache.restoreCache(
|
||||
[cachePath],
|
||||
cacheKey,
|
||||
inputCacheExact ? [] : [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
|
||||
}
|
||||
|
||||
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 !github.inputBoolean('configuration-cache-enabled', false)
|
||||
}
|
|
@ -21,16 +21,7 @@ export async function restoreCachedDependencies(
|
|||
core.saveState(DEPENDENCIES_CACHE_PATH, cachePath)
|
||||
|
||||
const inputCacheExact = github.inputBoolean('dependencies-cache-exact')
|
||||
|
||||
const inputCacheKeyGlobs = github.inputArrayOrNull('dependencies-cache-key')
|
||||
const cacheKeyGlobs = inputCacheKeyGlobs
|
||||
? inputCacheKeyGlobs
|
||||
: [
|
||||
'**/*.gradle',
|
||||
'**/*.gradle.kts',
|
||||
'**/gradle.properties',
|
||||
'gradle/**'
|
||||
]
|
||||
const cacheKeyGlobs = inputCacheKeyGlobs('dependencies-cache-key')
|
||||
|
||||
const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
|
||||
const cacheKeyPrefix = 'dependencies-'
|
||||
|
@ -97,7 +88,7 @@ export async function cacheDependencies(): Promise<void> {
|
|||
return
|
||||
}
|
||||
|
||||
function tryDeleteFiles(filePaths: string[]): boolean {
|
||||
export function tryDeleteFiles(filePaths: string[]): boolean {
|
||||
let failure = false
|
||||
for (const filePath of filePaths) {
|
||||
if (fs.existsSync(filePath)) {
|
||||
|
@ -114,3 +105,15 @@ function tryDeleteFiles(filePaths: string[]): boolean {
|
|||
function isDependenciesCacheDisabled(): boolean {
|
||||
return !github.inputBoolean('dependencies-cache-enabled', false)
|
||||
}
|
||||
|
||||
export function inputCacheKeyGlobs(input: string): string[] {
|
||||
const inputValue = github.inputArrayOrNull(input)
|
||||
return inputValue
|
||||
? inputValue
|
||||
: [
|
||||
'**/*.gradle',
|
||||
'**/*.gradle.kts',
|
||||
'**/gradle.properties',
|
||||
'gradle/**'
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import * as exec from '@actions/exec'
|
||||
import * as cacheDependencies from './cache-dependencies'
|
||||
import * as cacheConfiguration from './cache-configuration'
|
||||
|
||||
export async function execute(
|
||||
executable: string,
|
||||
|
@ -7,6 +8,7 @@ export async function execute(
|
|||
argv: string[]
|
||||
): Promise<BuildResult> {
|
||||
await cacheDependencies.restoreCachedDependencies(root)
|
||||
await cacheConfiguration.restoreCachedConfiguration(root)
|
||||
|
||||
let publishing = false
|
||||
let buildScanUrl: string | undefined
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
import * as cacheWrapper from './cache-wrapper'
|
||||
import * as cacheDependencies from './cache-dependencies'
|
||||
import * as cacheConfiguration from './cache-configuration'
|
||||
|
||||
// Invoked by GitHub Actions
|
||||
export async function run(): Promise<void> {
|
||||
await cacheWrapper.cacheWrapperDist()
|
||||
await cacheDependencies.cacheDependencies()
|
||||
await cacheConfiguration.cacheConfiguration()
|
||||
}
|
||||
|
||||
run()
|
||||
|
|
Loading…
Reference in a new issue