2021-12-07 19:29:37 +00:00
|
|
|
import * as core from '@actions/core'
|
2021-09-06 17:16:08 +00:00
|
|
|
import {GradleUserHomeCache} from './cache-gradle-user-home'
|
|
|
|
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
|
2021-10-30 13:15:20 +00:00
|
|
|
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
2021-12-07 19:29:37 +00:00
|
|
|
import {logCachingReport, CacheListener} from './cache-reporting'
|
2021-08-20 19:01:43 +00:00
|
|
|
|
2021-12-07 19:56:36 +00:00
|
|
|
const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED'
|
2021-08-20 19:01:43 +00:00
|
|
|
const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
|
2021-10-30 13:39:21 +00:00
|
|
|
const CACHE_LISTENER = 'CACHE_LISTENER'
|
2021-08-20 19:01:43 +00:00
|
|
|
|
|
|
|
export async function restore(buildRootDirectory: string): Promise<void> {
|
2021-12-07 19:56:36 +00:00
|
|
|
if (!shouldRestoreCaches()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 03:35:01 +00:00
|
|
|
const gradleUserHomeCache = new GradleUserHomeCache(buildRootDirectory)
|
|
|
|
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
|
|
|
|
|
|
|
|
gradleUserHomeCache.init()
|
|
|
|
|
2021-09-06 19:23:36 +00:00
|
|
|
await core.group('Restore Gradle state from cache', async () => {
|
|
|
|
core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
|
2021-10-29 16:19:35 +00:00
|
|
|
|
2021-10-30 13:21:27 +00:00
|
|
|
const cacheListener = new CacheListener()
|
2021-11-28 03:35:01 +00:00
|
|
|
await gradleUserHomeCache.restore(cacheListener)
|
2021-10-29 14:44:08 +00:00
|
|
|
|
2021-10-30 13:21:27 +00:00
|
|
|
if (cacheListener.fullyRestored) {
|
2021-10-29 14:44:08 +00:00
|
|
|
// Only restore the configuration-cache if the Gradle Home is fully restored
|
2021-10-30 13:21:27 +00:00
|
|
|
await projectDotGradleCache.restore(cacheListener)
|
2021-10-29 14:44:08 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, prepare the cache key for later save()
|
2021-10-29 16:41:30 +00:00
|
|
|
core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
|
2021-10-29 14:44:08 +00:00
|
|
|
projectDotGradleCache.prepareCacheKey()
|
|
|
|
}
|
2021-10-29 16:41:30 +00:00
|
|
|
|
2021-10-30 13:39:21 +00:00
|
|
|
core.saveState(CACHE_LISTENER, cacheListener.stringify())
|
2021-09-06 19:23:36 +00:00
|
|
|
})
|
2021-08-20 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function save(): Promise<void> {
|
2021-12-07 19:56:36 +00:00
|
|
|
if (!shouldSaveCaches()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:39:21 +00:00
|
|
|
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
|
|
|
|
|
2021-09-12 20:26:38 +00:00
|
|
|
if (isCacheReadOnly()) {
|
2021-10-29 13:34:44 +00:00
|
|
|
core.info('Cache is read-only: will not save state for use in subsequent builds.')
|
2021-10-30 13:39:21 +00:00
|
|
|
logCachingReport(cacheListener)
|
2021-09-06 01:55:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-03 17:25:55 +00:00
|
|
|
|
2021-09-06 19:23:36 +00:00
|
|
|
await core.group('Caching Gradle state', async () => {
|
|
|
|
const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
|
|
|
|
return Promise.all([
|
2021-10-30 13:39:21 +00:00
|
|
|
new GradleUserHomeCache(buildRootDirectory).save(cacheListener),
|
|
|
|
new ProjectDotGradleCache(buildRootDirectory).save(cacheListener)
|
2021-09-06 19:23:36 +00:00
|
|
|
])
|
|
|
|
})
|
2021-10-29 16:41:30 +00:00
|
|
|
|
2021-10-30 13:39:21 +00:00
|
|
|
logCachingReport(cacheListener)
|
2021-10-29 16:41:30 +00:00
|
|
|
}
|
2021-12-07 19:56:36 +00:00
|
|
|
|
|
|
|
function shouldRestoreCaches(): boolean {
|
|
|
|
if (isCacheDisabled()) {
|
|
|
|
core.info('Cache is disabled: will not restore state from previous builds.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env[CACHE_RESTORED_VAR]) {
|
|
|
|
core.info('Cache only restored on first action step.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export var that is detected in all later restore steps
|
|
|
|
core.exportVariable(CACHE_RESTORED_VAR, true)
|
|
|
|
// Export state that is detected in corresponding post-action step
|
|
|
|
core.saveState(CACHE_RESTORED_VAR, true)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldSaveCaches(): boolean {
|
|
|
|
if (isCacheDisabled()) {
|
|
|
|
core.info('Cache is disabled: will not save state for later builds.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!core.getState(CACHE_RESTORED_VAR)) {
|
|
|
|
core.info('Cache will only be saved in final post-action step.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|