2021-12-07 14:29:37 -05:00
|
|
|
import * as core from '@actions/core'
|
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-12-29 18:07:33 -05:00
|
|
|
import {GradleStateCache} from './cache-base'
|
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> {
|
2022-01-17 15:50:55 -05:00
|
|
|
// Bypass restore cache on all but first action step in workflow.
|
2022-01-17 14:20:31 -05:00
|
|
|
if (process.env[CACHE_RESTORED_VAR]) {
|
|
|
|
core.info('Cache only restored on first action step.')
|
2021-12-07 14:56:36 -05:00
|
|
|
return
|
|
|
|
}
|
2022-01-17 14:20:31 -05:00
|
|
|
core.exportVariable(CACHE_RESTORED_VAR, true)
|
2021-12-07 14:56:36 -05:00
|
|
|
|
2022-01-17 14:20:31 -05:00
|
|
|
// Initialize the Gradle User Home even when caching is disabled.
|
2021-12-29 18:07:33 -05:00
|
|
|
const gradleStateCache = new GradleStateCache(gradleUserHome)
|
|
|
|
gradleStateCache.init()
|
2021-12-07 18:52:53 -05:00
|
|
|
|
2022-01-17 14:20:31 -05:00
|
|
|
if (isCacheDisabled()) {
|
|
|
|
core.info('Cache is disabled: will not restore state from previous builds.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-17 15:50:55 -05:00
|
|
|
if (gradleStateCache.cacheOutputExists()) {
|
|
|
|
core.info('Gradle User Home already exists: will not restore from cache.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-12-29 18:07:33 -05:00
|
|
|
await gradleStateCache.restore(cacheListener)
|
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-12-29 18:07:33 -05:00
|
|
|
|
|
|
|
// Export state that is detected in corresponding post-action step
|
|
|
|
core.saveState(CACHE_RESTORED_VAR, true)
|
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-12-29 18:07:33 -05:00
|
|
|
return new GradleStateCache(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 shouldSaveCaches(): boolean {
|
|
|
|
if (isCacheDisabled()) {
|
|
|
|
core.info('Cache is disabled: will not save state for later builds.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!core.getState(CACHE_RESTORED_VAR)) {
|
2022-01-17 15:50:55 -05:00
|
|
|
core.info('Cache will not be saved: not restored in main action step.')
|
2021-12-07 14:56:36 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|