Loading src/cache-base.ts +2 −19 Changes for src/cache-base.ts: 2 added lines, 19 removed lines. Original line number Diff line number Diff line Loading @@ -83,13 +83,8 @@ export abstract class AbstractCache { /** * Restores the cache entry, finding the closest match to the currently running job. * If the target output already exists, caching will be skipped. */ async restore(listener: CacheListener): Promise<void> { if (this.cacheOutputExists()) { core.info(`${this.cacheDescription} already exists. Not restoring from cache.`) return } const entryListener = listener.entry(this.cacheDescription) const cacheKey = this.prepareCacheKey() Loading Loading @@ -142,28 +137,17 @@ export abstract class AbstractCache { protected async afterRestore(_listener: CacheListener): Promise<void> {} /** * Saves the cache entry based on the current cache key, unless: * - If the cache output existed before restore, then it is not saved. * - If the cache was restored with the exact key, we cannot overwrite it. * Saves the cache entry based on the current cache key unless the cache was restored with the exact key, * in which case we cannot overwrite it. * * If the cache entry was restored with a partial match on a restore key, then * it is saved with the exact key. */ async save(listener: CacheListener): Promise<void> { if (!this.cacheOutputExists()) { core.info(`No ${this.cacheDescription} to cache.`) return } // Retrieve the state set in the previous 'restore' step. const cacheKeyFromRestore = core.getState(this.cacheKeyStateKey) const cacheResultFromRestore = core.getState(this.cacheResultStateKey) if (!cacheKeyFromRestore) { core.info(`${this.cacheDescription} existed prior to cache restore. Not saving.`) return } if (cacheResultFromRestore && cacheKeyFromRestore === cacheResultFromRestore) { core.info(`Cache hit occurred on the cache key ${cacheKeyFromRestore}, not saving cache.`) return Loading Loading @@ -206,6 +190,5 @@ export abstract class AbstractCache { } } protected abstract cacheOutputExists(): boolean protected abstract getCachePath(): string[] } src/cache-gradle-user-home.ts −6 Changes for src/cache-gradle-user-home.ts: 0 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -294,12 +294,6 @@ export class GradleUserHomeCache extends AbstractCache { return path.resolve(os.homedir(), '.gradle') } protected cacheOutputExists(): boolean { // Need to check for 'caches' directory to avoid incorrect detection on MacOS agents const dir = path.resolve(this.gradleUserHome, 'caches') return fs.existsSync(dir) } /** * Determines the paths within Gradle User Home to cache. * By default, this is the 'caches' and 'notifications' directories, Loading src/cache-project-dot-gradle.ts −6 Changes for src/cache-project-dot-gradle.ts: 0 added lines, 6 removed lines. Original line number Diff line number Diff line import path from 'path' import fs from 'fs' import {AbstractCache} from './cache-base' // TODO: Maybe allow the user to override / tweak this set Loading @@ -17,11 +16,6 @@ export class ProjectDotGradleCache extends AbstractCache { this.rootDir = rootDir } protected cacheOutputExists(): boolean { const dir = this.getProjectDotGradleDir() return fs.existsSync(dir) } protected getCachePath(): string[] { const dir = this.getProjectDotGradleDir() return PATHS_TO_CACHE.map(x => path.resolve(dir, x)) Loading src/caches.ts +41 −5 Changes for src/caches.ts: 41 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -4,20 +4,20 @@ import {ProjectDotGradleCache} from './cache-project-dot-gradle' import {isCacheDisabled, isCacheReadOnly} from './cache-utils' import {logCachingReport, CacheListener} from './cache-reporting' const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED' const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR' const CACHE_LISTENER = 'CACHE_LISTENER' export async function restore(buildRootDirectory: string): Promise<void> { if (!shouldRestoreCaches()) { return } const gradleUserHomeCache = new GradleUserHomeCache(buildRootDirectory) const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory) gradleUserHomeCache.init() if (isCacheDisabled()) { core.info('Cache is disabled: will not restore state from previous builds.') return } await core.group('Restore Gradle state from cache', async () => { core.saveState(BUILD_ROOT_DIR, buildRootDirectory) Loading @@ -38,6 +38,10 @@ export async function restore(buildRootDirectory: string): Promise<void> { } export async function save(): Promise<void> { if (!shouldSaveCaches()) { return } const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER)) if (isCacheReadOnly()) { Loading @@ -56,3 +60,35 @@ export async function save(): Promise<void> { logCachingReport(cacheListener) } 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 } Loading
src/cache-base.ts +2 −19 Changes for src/cache-base.ts: 2 added lines, 19 removed lines. Original line number Diff line number Diff line Loading @@ -83,13 +83,8 @@ export abstract class AbstractCache { /** * Restores the cache entry, finding the closest match to the currently running job. * If the target output already exists, caching will be skipped. */ async restore(listener: CacheListener): Promise<void> { if (this.cacheOutputExists()) { core.info(`${this.cacheDescription} already exists. Not restoring from cache.`) return } const entryListener = listener.entry(this.cacheDescription) const cacheKey = this.prepareCacheKey() Loading Loading @@ -142,28 +137,17 @@ export abstract class AbstractCache { protected async afterRestore(_listener: CacheListener): Promise<void> {} /** * Saves the cache entry based on the current cache key, unless: * - If the cache output existed before restore, then it is not saved. * - If the cache was restored with the exact key, we cannot overwrite it. * Saves the cache entry based on the current cache key unless the cache was restored with the exact key, * in which case we cannot overwrite it. * * If the cache entry was restored with a partial match on a restore key, then * it is saved with the exact key. */ async save(listener: CacheListener): Promise<void> { if (!this.cacheOutputExists()) { core.info(`No ${this.cacheDescription} to cache.`) return } // Retrieve the state set in the previous 'restore' step. const cacheKeyFromRestore = core.getState(this.cacheKeyStateKey) const cacheResultFromRestore = core.getState(this.cacheResultStateKey) if (!cacheKeyFromRestore) { core.info(`${this.cacheDescription} existed prior to cache restore. Not saving.`) return } if (cacheResultFromRestore && cacheKeyFromRestore === cacheResultFromRestore) { core.info(`Cache hit occurred on the cache key ${cacheKeyFromRestore}, not saving cache.`) return Loading Loading @@ -206,6 +190,5 @@ export abstract class AbstractCache { } } protected abstract cacheOutputExists(): boolean protected abstract getCachePath(): string[] }
src/cache-gradle-user-home.ts −6 Changes for src/cache-gradle-user-home.ts: 0 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -294,12 +294,6 @@ export class GradleUserHomeCache extends AbstractCache { return path.resolve(os.homedir(), '.gradle') } protected cacheOutputExists(): boolean { // Need to check for 'caches' directory to avoid incorrect detection on MacOS agents const dir = path.resolve(this.gradleUserHome, 'caches') return fs.existsSync(dir) } /** * Determines the paths within Gradle User Home to cache. * By default, this is the 'caches' and 'notifications' directories, Loading
src/cache-project-dot-gradle.ts −6 Changes for src/cache-project-dot-gradle.ts: 0 added lines, 6 removed lines. Original line number Diff line number Diff line import path from 'path' import fs from 'fs' import {AbstractCache} from './cache-base' // TODO: Maybe allow the user to override / tweak this set Loading @@ -17,11 +16,6 @@ export class ProjectDotGradleCache extends AbstractCache { this.rootDir = rootDir } protected cacheOutputExists(): boolean { const dir = this.getProjectDotGradleDir() return fs.existsSync(dir) } protected getCachePath(): string[] { const dir = this.getProjectDotGradleDir() return PATHS_TO_CACHE.map(x => path.resolve(dir, x)) Loading
src/caches.ts +41 −5 Changes for src/caches.ts: 41 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -4,20 +4,20 @@ import {ProjectDotGradleCache} from './cache-project-dot-gradle' import {isCacheDisabled, isCacheReadOnly} from './cache-utils' import {logCachingReport, CacheListener} from './cache-reporting' const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED' const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR' const CACHE_LISTENER = 'CACHE_LISTENER' export async function restore(buildRootDirectory: string): Promise<void> { if (!shouldRestoreCaches()) { return } const gradleUserHomeCache = new GradleUserHomeCache(buildRootDirectory) const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory) gradleUserHomeCache.init() if (isCacheDisabled()) { core.info('Cache is disabled: will not restore state from previous builds.') return } await core.group('Restore Gradle state from cache', async () => { core.saveState(BUILD_ROOT_DIR, buildRootDirectory) Loading @@ -38,6 +38,10 @@ export async function restore(buildRootDirectory: string): Promise<void> { } export async function save(): Promise<void> { if (!shouldSaveCaches()) { return } const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER)) if (isCacheReadOnly()) { Loading @@ -56,3 +60,35 @@ export async function save(): Promise<void> { logCachingReport(cacheListener) } 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 }