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