Add experimental support for cache-cleanup

- Includes basic implementation as `CacheCleaner`
- Integration test that checks unused files are removed:
  - Downloaded dependencies
  - Local build cache entries
  - Wrapper distributions
This commit is contained in:
Daz DeBoer 2022-06-12 09:53:04 -06:00
parent ef638c00fd
commit 915123c493
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
14 changed files with 522 additions and 1 deletions

52
src/cache-cleaner.ts Normal file
View file

@ -0,0 +1,52 @@
import * as exec from '@actions/exec'
import fs from 'fs'
import path from 'path'
export class CacheCleaner {
private readonly gradleUserHome: string
private readonly tmpDir: string
constructor(gradleUserHome: string, tmpDir: string) {
this.gradleUserHome = gradleUserHome
this.tmpDir = tmpDir
}
async prepare(): Promise<void> {
fs.rmSync(path.resolve(this.gradleUserHome, 'caches/journal-1'), {recursive: true, force: true})
fs.mkdirSync(path.resolve(this.gradleUserHome, 'caches/journal-1'), {recursive: true})
fs.writeFileSync(
path.resolve(this.gradleUserHome, 'caches/journal-1/file-access.properties'),
'inceptionTimestamp=0'
)
await this.ageAllFiles()
await this.touchAllFiles('gc.properties')
}
async forceCleanup(): Promise<void> {
await this.ageAllFiles('gc.properties')
const cleanupProjectDir = path.resolve(this.tmpDir, 'dummy-cleanup-project')
fs.mkdirSync(cleanupProjectDir, {recursive: true})
fs.writeFileSync(
path.resolve(cleanupProjectDir, 'settings.gradle'),
'rootProject.name = "dummy-cleanup-project"'
)
fs.writeFileSync(path.resolve(cleanupProjectDir, 'build.gradle'), 'task("noop") {}')
await exec.exec(`gradle -g ${this.gradleUserHome} --no-daemon --build-cache --no-scan --quiet noop`, [], {
cwd: cleanupProjectDir
})
}
private async ageAllFiles(fileName = '*'): Promise<void> {
await exec.exec(
'find',
[this.gradleUserHome, '-name', fileName, '-exec', 'touch', '-m', '-d', '1970-01-01', '{}', '+'],
{}
)
}
private async touchAllFiles(fileName = '*'): Promise<void> {
await exec.exec('find', [this.gradleUserHome, '-name', fileName, '-exec', 'touch', '-m', '{}', '+'], {})
}
}

View file

@ -16,6 +16,7 @@ const CACHE_DISABLED_PARAMETER = 'cache-disabled'
const CACHE_READONLY_PARAMETER = 'cache-read-only'
const CACHE_WRITEONLY_PARAMETER = 'cache-write-only'
const STRICT_CACHE_MATCH_PARAMETER = 'gradle-home-cache-strict-match'
const CACHE_CLEANUP_ENABLED_PARAMETER = 'gradle-home-cache-cleanup'
const CACHE_DEBUG_VAR = 'GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'
const CACHE_KEY_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'
@ -46,6 +47,10 @@ export function isCacheDebuggingEnabled(): boolean {
return process.env[CACHE_DEBUG_VAR] ? true : false
}
export function isCacheCleanupEnabled(): boolean {
return core.getBooleanInput(CACHE_CLEANUP_ENABLED_PARAMETER)
}
/**
* Represents a key used to restore a cache entry.
* The Github Actions cache will first try for an exact match on the key.

View file

@ -1,8 +1,9 @@
import * as core from '@actions/core'
import {isCacheDisabled, isCacheReadOnly, isCacheWriteOnly} from './cache-utils'
import {isCacheCleanupEnabled, isCacheDisabled, isCacheReadOnly, isCacheWriteOnly} from './cache-utils'
import {CacheListener} from './cache-reporting'
import {DaemonController} from './daemon-controller'
import {GradleStateCache} from './cache-base'
import {CacheCleaner} from './cache-cleaner'
const CACHE_RESTORED_VAR = 'GRADLE_BUILD_ACTION_CACHE_RESTORED'
@ -44,6 +45,10 @@ export async function restore(gradleUserHome: string, cacheListener: CacheListen
await core.group('Restore Gradle state from cache', async () => {
await gradleStateCache.restore(cacheListener)
})
if (isCacheCleanupEnabled() && !isCacheReadOnly()) {
new CacheCleaner(gradleUserHome, process.env['RUNNER_TEMP']!).prepare()
}
}
export async function save(
@ -69,6 +74,10 @@ export async function save(
await daemonController.stopAllDaemons()
if (isCacheCleanupEnabled()) {
new CacheCleaner(gradleUserHome, process.env['RUNNER_TEMP']!).forceCleanup()
}
await core.group('Caching Gradle state', async () => {
return new GradleStateCache(gradleUserHome).save(cacheListener)
})