2021-08-23 02:14:47 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import os from 'os'
|
2021-09-11 15:10:44 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as glob from '@actions/glob'
|
2021-09-11 18:08:18 +00:00
|
|
|
import * as exec from '@actions/exec'
|
2021-08-23 02:14:47 +00:00
|
|
|
|
2021-10-16 15:44:35 +00:00
|
|
|
import {
|
|
|
|
AbstractCache,
|
|
|
|
getCacheKeyPrefix,
|
|
|
|
hashFileNames,
|
|
|
|
tryDelete
|
|
|
|
} from './cache-utils'
|
2021-09-12 16:08:34 +00:00
|
|
|
|
2021-09-06 17:16:08 +00:00
|
|
|
export class GradleUserHomeCache extends AbstractCache {
|
2021-09-28 02:57:47 +00:00
|
|
|
private gradleUserHome: string
|
|
|
|
|
|
|
|
constructor(rootDir: string) {
|
2021-09-06 17:16:08 +00:00
|
|
|
super('gradle', 'Gradle User Home')
|
2021-09-28 02:57:47 +00:00
|
|
|
this.gradleUserHome = this.determineGradleUserHome(rootDir)
|
2021-08-23 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 17:20:33 +00:00
|
|
|
async afterRestore(): Promise<void> {
|
2021-10-15 17:11:05 +00:00
|
|
|
await this.reportGradleUserHomeSize('as restored from cache')
|
2021-10-15 18:34:38 +00:00
|
|
|
await this.restoreArtifactBundles()
|
2021-10-15 17:11:05 +00:00
|
|
|
await this.reportGradleUserHomeSize('after restoring common artifacts')
|
2021-09-11 18:08:18 +00:00
|
|
|
}
|
2021-09-11 15:10:44 +00:00
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
private async restoreArtifactBundles(): Promise<void> {
|
2021-09-12 02:56:40 +00:00
|
|
|
const processes: Promise<void>[] = []
|
2021-10-15 18:34:38 +00:00
|
|
|
for (const [bundle, pattern] of this.getArtifactBundles()) {
|
|
|
|
const p = this.restoreArtifactBundle(bundle, pattern)
|
2021-09-12 20:08:22 +00:00
|
|
|
// Run sequentially when debugging enabled
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
await p
|
|
|
|
}
|
2021-09-12 02:56:40 +00:00
|
|
|
processes.push(p)
|
|
|
|
}
|
2021-09-12 20:08:22 +00:00
|
|
|
|
2021-09-12 02:56:40 +00:00
|
|
|
await Promise.all(processes)
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
private async restoreArtifactBundle(
|
2021-09-15 12:36:54 +00:00
|
|
|
bundle: string,
|
2021-09-28 02:57:47 +00:00
|
|
|
artifactPath: string
|
2021-09-15 12:36:54 +00:00
|
|
|
): Promise<void> {
|
2021-10-15 18:34:38 +00:00
|
|
|
const bundleMetaFile = this.getBundleMetaFile(bundle)
|
|
|
|
if (fs.existsSync(bundleMetaFile)) {
|
|
|
|
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
|
2021-09-28 02:57:47 +00:00
|
|
|
const restoreKey = await this.restoreCache([artifactPath], cacheKey)
|
2021-09-14 19:30:23 +00:00
|
|
|
if (restoreKey) {
|
2021-09-15 21:48:55 +00:00
|
|
|
core.info(
|
2021-09-28 02:57:47 +00:00
|
|
|
`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`
|
2021-09-15 12:36:54 +00:00
|
|
|
)
|
2021-09-14 19:30:23 +00:00
|
|
|
} else {
|
|
|
|
this.debug(
|
2021-10-15 17:11:05 +00:00
|
|
|
`Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}`
|
2021-09-12 16:08:34 +00:00
|
|
|
)
|
2021-09-11 18:08:18 +00:00
|
|
|
}
|
2021-09-12 02:56:40 +00:00
|
|
|
} else {
|
2021-09-12 20:08:22 +00:00
|
|
|
this.debug(
|
2021-10-15 18:34:38 +00:00
|
|
|
`No metafile found to restore ${bundle}: ${bundleMetaFile}`
|
2021-09-12 16:08:34 +00:00
|
|
|
)
|
2021-09-11 18:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
private getBundleMetaFile(name: string): string {
|
2021-09-15 12:36:54 +00:00
|
|
|
return path.resolve(
|
2021-09-28 02:57:47 +00:00
|
|
|
this.gradleUserHome,
|
2021-09-15 12:36:54 +00:00
|
|
|
'caches',
|
|
|
|
`.gradle-build-action.${name}.cache`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:20:33 +00:00
|
|
|
async beforeSave(): Promise<void> {
|
2021-10-15 17:11:05 +00:00
|
|
|
await this.reportGradleUserHomeSize('before saving common artifacts')
|
2021-10-15 18:34:38 +00:00
|
|
|
await this.saveArtifactBundles()
|
2021-10-16 16:15:40 +00:00
|
|
|
await this.reportGradleUserHomeSize(
|
|
|
|
'after saving common artifacts (./wrapper dir is not cached)'
|
|
|
|
)
|
2021-09-11 18:08:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
private async saveArtifactBundles(): Promise<void> {
|
2021-09-12 02:56:40 +00:00
|
|
|
const processes: Promise<void>[] = []
|
2021-10-15 18:34:38 +00:00
|
|
|
for (const [bundle, pattern] of this.getArtifactBundles()) {
|
|
|
|
const p = this.saveArtifactBundle(bundle, pattern)
|
2021-09-12 20:08:22 +00:00
|
|
|
// Run sequentially when debugging enabled
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
await p
|
|
|
|
}
|
2021-09-12 02:56:40 +00:00
|
|
|
processes.push(p)
|
|
|
|
}
|
2021-09-12 20:08:22 +00:00
|
|
|
|
2021-09-12 02:56:40 +00:00
|
|
|
await Promise.all(processes)
|
|
|
|
}
|
2021-09-11 15:10:44 +00:00
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
private async saveArtifactBundle(
|
2021-09-15 12:36:54 +00:00
|
|
|
bundle: string,
|
2021-09-28 02:57:47 +00:00
|
|
|
artifactPath: string
|
2021-09-15 12:36:54 +00:00
|
|
|
): Promise<void> {
|
2021-10-15 18:34:38 +00:00
|
|
|
const bundleMetaFile = this.getBundleMetaFile(bundle)
|
2021-09-12 02:56:40 +00:00
|
|
|
|
2021-10-16 15:46:17 +00:00
|
|
|
const globber = await glob.create(artifactPath, {
|
|
|
|
implicitDescendants: false,
|
|
|
|
followSymbolicLinks: false
|
|
|
|
})
|
2021-10-15 18:34:38 +00:00
|
|
|
const bundleFiles = await globber.glob()
|
2021-09-12 20:08:22 +00:00
|
|
|
|
2021-09-15 12:36:54 +00:00
|
|
|
// Handle no matching files
|
2021-10-15 18:34:38 +00:00
|
|
|
if (bundleFiles.length === 0) {
|
2021-09-15 12:36:54 +00:00
|
|
|
this.debug(`No files found to cache for ${bundle}`)
|
2021-10-15 18:34:38 +00:00
|
|
|
if (fs.existsSync(bundleMetaFile)) {
|
|
|
|
tryDelete(bundleMetaFile)
|
2021-09-15 12:36:54 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 18:08:18 +00:00
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
const previouslyRestoredKey = fs.existsSync(bundleMetaFile)
|
|
|
|
? fs.readFileSync(bundleMetaFile, 'utf-8').trim()
|
2021-09-15 12:36:54 +00:00
|
|
|
: ''
|
2021-10-15 18:34:38 +00:00
|
|
|
const cacheKey = this.createCacheKey(bundle, bundleFiles)
|
2021-09-15 12:36:54 +00:00
|
|
|
|
|
|
|
if (previouslyRestoredKey === cacheKey) {
|
2021-09-12 20:08:22 +00:00
|
|
|
this.debug(
|
2021-09-15 12:36:54 +00:00
|
|
|
`No change to previously restored ${bundle}. Not caching.`
|
2021-09-12 16:08:34 +00:00
|
|
|
)
|
2021-09-15 12:36:54 +00:00
|
|
|
} else {
|
2021-09-15 21:48:55 +00:00
|
|
|
core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
|
2021-09-28 02:57:47 +00:00
|
|
|
await this.saveCache([artifactPath], cacheKey)
|
2021-09-15 12:36:54 +00:00
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
this.debug(`Writing cache metafile: ${bundleMetaFile}`)
|
|
|
|
fs.writeFileSync(bundleMetaFile, cacheKey)
|
2021-09-11 18:08:18 +00:00
|
|
|
}
|
2021-09-12 02:56:40 +00:00
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
for (const file of bundleFiles) {
|
2021-10-04 21:59:08 +00:00
|
|
|
tryDelete(file)
|
2021-09-15 12:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 03:05:17 +00:00
|
|
|
protected createCacheKey(bundle: string, files: string[]): string {
|
2021-10-16 15:44:35 +00:00
|
|
|
const cacheKeyPrefix = getCacheKeyPrefix()
|
2021-09-28 03:05:17 +00:00
|
|
|
const relativeFiles = files.map(x =>
|
|
|
|
path.relative(this.gradleUserHome, x)
|
|
|
|
)
|
|
|
|
const key = hashFileNames(relativeFiles)
|
|
|
|
|
2021-10-15 17:11:05 +00:00
|
|
|
this.debug(
|
|
|
|
`Generating cache key for ${bundle} from files: ${relativeFiles}`
|
|
|
|
)
|
|
|
|
|
2021-09-15 21:48:55 +00:00
|
|
|
return `${cacheKeyPrefix}${bundle}-${key}`
|
2021-09-12 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 02:57:47 +00:00
|
|
|
protected determineGradleUserHome(rootDir: string): string {
|
|
|
|
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
|
|
|
if (customGradleUserHome) {
|
|
|
|
return path.resolve(rootDir, customGradleUserHome)
|
|
|
|
}
|
|
|
|
|
2021-09-12 16:08:34 +00:00
|
|
|
return path.resolve(os.homedir(), '.gradle')
|
2021-09-11 15:10:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 17:16:08 +00:00
|
|
|
protected cacheOutputExists(): boolean {
|
|
|
|
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
|
2021-09-28 02:57:47 +00:00
|
|
|
const dir = path.resolve(this.gradleUserHome, 'caches')
|
2021-09-06 17:16:08 +00:00
|
|
|
return fs.existsSync(dir)
|
2021-08-23 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 17:16:08 +00:00
|
|
|
protected getCachePath(): string[] {
|
2021-10-20 15:52:04 +00:00
|
|
|
const rawPaths: string[] = JSON.parse(core.getInput('cache-paths'))
|
|
|
|
rawPaths.push(META_FILE_DIR)
|
|
|
|
const resolvedPaths = rawPaths.map(x => this.resolveCachePath(x))
|
|
|
|
this.debug(`Using cache paths: ${resolvedPaths}`)
|
|
|
|
return resolvedPaths
|
|
|
|
}
|
|
|
|
|
|
|
|
private resolveCachePath(rawPath: string): string {
|
|
|
|
if (rawPath.startsWith('!')) {
|
|
|
|
const resolved = this.resolveCachePath(rawPath.substring(1))
|
|
|
|
const negated = `!${resolved}`
|
|
|
|
this.debug(`Negate cache path: ${negated}`)
|
|
|
|
return negated
|
|
|
|
}
|
|
|
|
return path.resolve(this.gradleUserHome, rawPath)
|
2021-09-28 02:57:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:34:38 +00:00
|
|
|
private getArtifactBundles(): Map<string, string> {
|
2021-10-15 18:45:15 +00:00
|
|
|
const artifactBundleDefinition = core.getInput('cache-artifact-bundles')
|
|
|
|
this.debug(
|
|
|
|
`Using artifact bundle definition: ${artifactBundleDefinition}`
|
|
|
|
)
|
|
|
|
const artifactBundles = JSON.parse(artifactBundleDefinition)
|
2021-09-28 02:57:47 +00:00
|
|
|
return new Map(
|
2021-10-15 18:45:15 +00:00
|
|
|
Array.from(artifactBundles, ([key, value]) => [
|
2021-09-28 02:57:47 +00:00
|
|
|
key,
|
|
|
|
path.resolve(this.gradleUserHome, value)
|
|
|
|
])
|
|
|
|
)
|
2021-08-23 02:14:47 +00:00
|
|
|
}
|
2021-10-15 17:11:05 +00:00
|
|
|
|
|
|
|
private async reportGradleUserHomeSize(label: string): Promise<void> {
|
|
|
|
if (!this.cacheDebuggingEnabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(this.gradleUserHome)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const result = await exec.getExecOutput(
|
|
|
|
'du',
|
|
|
|
['-h', '-c', '-t', '5M'],
|
|
|
|
{
|
|
|
|
cwd: this.gradleUserHome,
|
|
|
|
silent: true,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-16 16:15:40 +00:00
|
|
|
core.info(`Gradle User Home (directories >5M): ${label}`)
|
2021-10-15 17:11:05 +00:00
|
|
|
|
|
|
|
core.info(
|
|
|
|
result.stdout
|
|
|
|
.trimEnd()
|
|
|
|
.replace(/\t/g, ' ')
|
|
|
|
.split('\n')
|
|
|
|
.map(it => {
|
|
|
|
return ` ${it}`
|
|
|
|
})
|
|
|
|
.join('\n')
|
|
|
|
)
|
|
|
|
|
|
|
|
core.info('-----------------------')
|
|
|
|
}
|
2021-08-23 02:14:47 +00:00
|
|
|
}
|