2021-12-07 14:29:37 -05:00
|
|
|
import * as core from '@actions/core'
|
2021-09-06 13:16:08 -04:00
|
|
|
import {GradleUserHomeCache} from './cache-gradle-user-home'
|
|
|
|
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
|
2021-10-30 09:15:20 -04:00
|
|
|
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
2021-12-07 14:29:37 -05:00
|
|
|
import {logCachingReport, CacheListener} from './cache-reporting'
|
2021-08-20 15:01:43 -04:00
|
|
|
|
2021-12-07 14:56:36 -05:00
|
|
|
const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED'
|
2021-12-07 18:52:53 -05:00
|
|
|
const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
|
2021-10-30 09:39:21 -04:00
|
|
|
const CACHE_LISTENER = 'CACHE_LISTENER'
|
2021-08-20 15:01:43 -04:00
|
|
|
|
2021-12-07 18:52:53 -05:00
|
|
|
export async function restore(gradleUserHome: string): Promise<void> {
|
2021-12-07 14:56:36 -05:00
|
|
|
if (!shouldRestoreCaches()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-07 18:52:53 -05:00
|
|
|
const gradleUserHomeCache = new GradleUserHomeCache(gradleUserHome)
|
2021-11-27 22:35:01 -05:00
|
|
|
gradleUserHomeCache.init()
|
|
|
|
|
2021-12-07 18:52:53 -05:00
|
|
|
const projectDotGradleCache = new ProjectDotGradleCache(gradleUserHome)
|
|
|
|
projectDotGradleCache.init()
|
|
|
|
|
2021-09-06 15:23:36 -04:00
|
|
|
await core.group('Restore Gradle state from cache', async () => {
|
2021-12-07 18:52:53 -05:00
|
|
|
core.saveState(GRADLE_USER_HOME, gradleUserHome)
|
2021-10-29 12:19:35 -04:00
|
|
|
|
2021-10-30 09:21:27 -04:00
|
|
|
const cacheListener = new CacheListener()
|
2021-11-27 22:35:01 -05:00
|
|
|
await gradleUserHomeCache.restore(cacheListener)
|
2021-10-29 10:44:08 -04:00
|
|
|
|
2021-10-30 09:21:27 -04:00
|
|
|
if (cacheListener.fullyRestored) {
|
2021-10-29 10:44:08 -04:00
|
|
|
// Only restore the configuration-cache if the Gradle Home is fully restored
|
2021-10-30 09:21:27 -04:00
|
|
|
await projectDotGradleCache.restore(cacheListener)
|
2021-10-29 10:44:08 -04:00
|
|
|
} else {
|
|
|
|
// Otherwise, prepare the cache key for later save()
|
2021-10-29 12:41:30 -04:00
|
|
|
core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
|
2021-10-29 10:44:08 -04:00
|
|
|
projectDotGradleCache.prepareCacheKey()
|
|
|
|
}
|
2021-10-29 12:41:30 -04:00
|
|
|
|
2021-10-30 09:39:21 -04:00
|
|
|
core.saveState(CACHE_LISTENER, cacheListener.stringify())
|
2021-09-06 15:23:36 -04:00
|
|
|
})
|
2021-08-20 15:01:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function save(): Promise<void> {
|
2021-12-07 14:56:36 -05:00
|
|
|
if (!shouldSaveCaches()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-30 09:39:21 -04:00
|
|
|
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
|
|
|
|
|
2021-09-12 16:26:38 -04:00
|
|
|
if (isCacheReadOnly()) {
|
2021-10-29 09:34:44 -04:00
|
|
|
core.info('Cache is read-only: will not save state for use in subsequent builds.')
|
2021-10-30 09:39:21 -04:00
|
|
|
logCachingReport(cacheListener)
|
2021-09-05 21:55:49 -04:00
|
|
|
return
|
|
|
|
}
|
2021-09-03 13:25:55 -04:00
|
|
|
|
2021-09-06 15:23:36 -04:00
|
|
|
await core.group('Caching Gradle state', async () => {
|
2021-12-07 18:52:53 -05:00
|
|
|
const gradleUserHome = core.getState(GRADLE_USER_HOME)
|
2021-09-06 15:23:36 -04:00
|
|
|
return Promise.all([
|
2021-12-07 18:52:53 -05:00
|
|
|
new GradleUserHomeCache(gradleUserHome).save(cacheListener),
|
|
|
|
new ProjectDotGradleCache(gradleUserHome).save(cacheListener)
|
2021-09-06 15:23:36 -04:00
|
|
|
])
|
|
|
|
})
|
2021-10-29 12:41:30 -04:00
|
|
|
|
2021-10-30 09:39:21 -04:00
|
|
|
logCachingReport(cacheListener)
|
2021-10-29 12:41:30 -04:00
|
|
|
}
|
2021-12-07 14:56:36 -05: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
|
|
|
|
}
|