2021-12-07 19:29:37 +00:00
|
|
|
import * as core from '@actions/core'
|
2022-01-20 16:36:57 +00:00
|
|
|
import {isCacheDisabled, isCacheReadOnly, isCacheWriteOnly} from './cache-utils'
|
2022-06-03 04:39:09 +00:00
|
|
|
import {CacheListener} from './cache-reporting'
|
2021-12-29 23:07:33 +00:00
|
|
|
import {GradleStateCache} from './cache-base'
|
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
|
|
|
|
2022-06-03 04:39:09 +00:00
|
|
|
export async function restore(gradleUserHome: string, cacheListener: CacheListener): Promise<void> {
|
2022-01-17 20:50:55 +00:00
|
|
|
// Bypass restore cache on all but first action step in workflow.
|
2022-01-17 19:20:31 +00:00
|
|
|
if (process.env[CACHE_RESTORED_VAR]) {
|
|
|
|
core.info('Cache only restored on first action step.')
|
2021-12-07 19:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-17 19:20:31 +00:00
|
|
|
core.exportVariable(CACHE_RESTORED_VAR, true)
|
2021-12-07 19:56:36 +00:00
|
|
|
|
2021-12-29 23:07:33 +00:00
|
|
|
const gradleStateCache = new GradleStateCache(gradleUserHome)
|
2021-12-07 23:52:53 +00:00
|
|
|
|
2022-01-17 19:20:31 +00:00
|
|
|
if (isCacheDisabled()) {
|
|
|
|
core.info('Cache is disabled: will not restore state from previous builds.')
|
2022-01-17 21:06:56 +00:00
|
|
|
// Initialize the Gradle User Home even when caching is disabled.
|
|
|
|
gradleStateCache.init()
|
2022-01-17 19:20:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:50:55 +00:00
|
|
|
if (gradleStateCache.cacheOutputExists()) {
|
|
|
|
core.info('Gradle User Home already exists: will not restore from cache.')
|
2022-01-17 21:06:56 +00:00
|
|
|
// Initialize pre-existing Gradle User Home.
|
|
|
|
gradleStateCache.init()
|
2022-01-17 20:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:06:56 +00:00
|
|
|
gradleStateCache.init()
|
2022-01-20 16:36:57 +00:00
|
|
|
// Mark the state as restored so that post-action will perform save.
|
|
|
|
core.saveState(CACHE_RESTORED_VAR, true)
|
2022-01-17 21:06:56 +00:00
|
|
|
|
2022-01-20 16:36:57 +00:00
|
|
|
if (isCacheWriteOnly()) {
|
|
|
|
core.info('Cache is write-only: will not restore from cache.')
|
2022-06-03 04:44:08 +00:00
|
|
|
cacheListener.isCacheWriteOnly = true
|
2022-01-20 16:36:57 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-29 16:19:35 +00:00
|
|
|
|
2022-01-20 16:36:57 +00:00
|
|
|
await core.group('Restore Gradle state from cache', async () => {
|
2021-12-29 23:07:33 +00:00
|
|
|
await gradleStateCache.restore(cacheListener)
|
2021-09-06 19:23:36 +00:00
|
|
|
})
|
2021-08-20 19:01:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:39:09 +00:00
|
|
|
export async function save(gradleUserHome: string, cacheListener: CacheListener): Promise<void> {
|
2021-12-07 19:56:36 +00:00
|
|
|
if (!shouldSaveCaches()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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.')
|
2022-06-03 04:44:08 +00:00
|
|
|
cacheListener.isCacheReadOnly = true
|
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 () => {
|
2021-12-29 23:07:33 +00:00
|
|
|
return new GradleStateCache(gradleUserHome).save(cacheListener)
|
2021-09-06 19:23:36 +00:00
|
|
|
})
|
2021-10-29 16:41:30 +00:00
|
|
|
}
|
2021-12-07 19:56:36 +00: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 20:50:55 +00:00
|
|
|
core.info('Cache will not be saved: not restored in main action step.')
|
2021-12-07 19:56:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|