2021-08-22 20:14:47 -06:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import os from 'os'
|
2021-09-11 09:10:44 -06:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as glob from '@actions/glob'
|
2021-09-11 12:08:18 -06:00
|
|
|
import * as exec from '@actions/exec'
|
2021-08-22 20:14:47 -06:00
|
|
|
|
2021-10-04 23:59:08 +02:00
|
|
|
import {AbstractCache, hashFileNames, tryDelete} from './cache-utils'
|
2021-09-12 10:08:34 -06:00
|
|
|
|
|
|
|
// Which paths under Gradle User Home should be cached
|
2021-09-27 20:57:47 -06:00
|
|
|
const CACHE_PATH = ['caches', 'notifications']
|
2021-09-15 06:36:54 -06:00
|
|
|
|
|
|
|
const COMMON_ARTIFACT_CACHES = new Map([
|
2021-09-27 20:57:47 -06:00
|
|
|
['generated-gradle-jars', 'caches/*/generated-gradle-jars/*.jar'],
|
|
|
|
['wrapper-zips', 'wrapper/dists/*/*/*.zip'],
|
|
|
|
['dependency-jars', 'caches/modules-*/files-*/**/*.jar'],
|
|
|
|
['instrumented-jars', 'caches/jars-*/*/*.jar']
|
2021-09-15 06:36:54 -06:00
|
|
|
])
|
2021-09-11 14:50:34 -06:00
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
export class GradleUserHomeCache extends AbstractCache {
|
2021-09-27 20:57:47 -06:00
|
|
|
private gradleUserHome: string
|
|
|
|
|
|
|
|
constructor(rootDir: string) {
|
2021-09-06 11:16:08 -06:00
|
|
|
super('gradle', 'Gradle User Home')
|
2021-09-27 20:57:47 -06:00
|
|
|
this.gradleUserHome = this.determineGradleUserHome(rootDir)
|
2021-08-22 20:14:47 -06:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:20:33 -06:00
|
|
|
async afterRestore(): Promise<void> {
|
2021-10-15 11:11:05 -06:00
|
|
|
await this.reportGradleUserHomeSize('as restored from cache')
|
2021-09-12 10:08:34 -06:00
|
|
|
await this.restoreCommonArtifacts()
|
2021-10-15 11:11:05 -06:00
|
|
|
await this.reportGradleUserHomeSize('after restoring common artifacts')
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
2021-09-11 09:10:44 -06:00
|
|
|
|
2021-09-12 10:08:34 -06:00
|
|
|
private async restoreCommonArtifacts(): Promise<void> {
|
2021-09-11 20:56:40 -06:00
|
|
|
const processes: Promise<void>[] = []
|
2021-09-27 20:57:47 -06:00
|
|
|
for (const [bundle, pattern] of this.getCommonArtifactPaths()) {
|
2021-09-15 06:36:54 -06:00
|
|
|
const p = this.restoreCommonArtifactBundle(bundle, pattern)
|
2021-09-12 14:08:22 -06:00
|
|
|
// Run sequentially when debugging enabled
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
await p
|
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
processes.push(p)
|
|
|
|
}
|
2021-09-12 14:08:22 -06:00
|
|
|
|
2021-09-11 20:56:40 -06:00
|
|
|
await Promise.all(processes)
|
|
|
|
}
|
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
private async restoreCommonArtifactBundle(
|
|
|
|
bundle: string,
|
2021-09-27 20:57:47 -06:00
|
|
|
artifactPath: string
|
2021-09-15 06:36:54 -06:00
|
|
|
): Promise<void> {
|
|
|
|
const cacheMetaFile = this.getCacheMetaFile(bundle)
|
|
|
|
if (fs.existsSync(cacheMetaFile)) {
|
|
|
|
const cacheKey = fs.readFileSync(cacheMetaFile, 'utf-8').trim()
|
2021-09-27 20:57:47 -06:00
|
|
|
const restoreKey = await this.restoreCache([artifactPath], cacheKey)
|
2021-09-14 13:30:23 -06:00
|
|
|
if (restoreKey) {
|
2021-09-15 15:48:55 -06:00
|
|
|
core.info(
|
2021-09-27 20:57:47 -06:00
|
|
|
`Restored ${bundle} with key ${cacheKey} to ${artifactPath}`
|
2021-09-15 06:36:54 -06:00
|
|
|
)
|
2021-09-14 13:30:23 -06:00
|
|
|
} else {
|
|
|
|
this.debug(
|
2021-10-15 11:11:05 -06:00
|
|
|
`Did not restore ${bundle} with key ${cacheKey} to ${artifactPath}`
|
2021-09-12 10:08:34 -06:00
|
|
|
)
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
} else {
|
2021-09-12 14:08:22 -06:00
|
|
|
this.debug(
|
2021-09-15 06:36:54 -06:00
|
|
|
`No metafile found to restore ${bundle}: ${cacheMetaFile}`
|
2021-09-12 10:08:34 -06:00
|
|
|
)
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
private getCacheMetaFile(name: string): string {
|
|
|
|
return path.resolve(
|
2021-09-27 20:57:47 -06:00
|
|
|
this.gradleUserHome,
|
2021-09-15 06:36:54 -06:00
|
|
|
'caches',
|
|
|
|
`.gradle-build-action.${name}.cache`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-15 11:20:33 -06:00
|
|
|
async beforeSave(): Promise<void> {
|
2021-10-15 11:11:05 -06:00
|
|
|
await this.reportGradleUserHomeSize('before saving common artifacts')
|
2021-09-12 10:08:34 -06:00
|
|
|
await this.saveCommonArtifacts()
|
2021-10-15 11:11:05 -06:00
|
|
|
await this.reportGradleUserHomeSize('after saving common artifacts')
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
|
|
|
|
2021-09-12 10:08:34 -06:00
|
|
|
private async saveCommonArtifacts(): Promise<void> {
|
2021-09-11 20:56:40 -06:00
|
|
|
const processes: Promise<void>[] = []
|
2021-09-27 20:57:47 -06:00
|
|
|
for (const [bundle, pattern] of this.getCommonArtifactPaths()) {
|
2021-09-15 06:36:54 -06:00
|
|
|
const p = this.saveCommonArtifactBundle(bundle, pattern)
|
2021-09-12 14:08:22 -06:00
|
|
|
// Run sequentially when debugging enabled
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
await p
|
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
processes.push(p)
|
|
|
|
}
|
2021-09-12 14:08:22 -06:00
|
|
|
|
2021-09-11 20:56:40 -06:00
|
|
|
await Promise.all(processes)
|
|
|
|
}
|
2021-09-11 09:10:44 -06:00
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
private async saveCommonArtifactBundle(
|
|
|
|
bundle: string,
|
2021-09-27 20:57:47 -06:00
|
|
|
artifactPath: string
|
2021-09-15 06:36:54 -06:00
|
|
|
): Promise<void> {
|
|
|
|
const cacheMetaFile = this.getCacheMetaFile(bundle)
|
2021-09-11 20:56:40 -06:00
|
|
|
|
2021-09-27 20:57:47 -06:00
|
|
|
const globber = await glob.create(artifactPath)
|
2021-09-15 06:36:54 -06:00
|
|
|
const commonArtifactFiles = await globber.glob()
|
2021-09-12 14:08:22 -06:00
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
// Handle no matching files
|
|
|
|
if (commonArtifactFiles.length === 0) {
|
|
|
|
this.debug(`No files found to cache for ${bundle}`)
|
|
|
|
if (fs.existsSync(cacheMetaFile)) {
|
2021-10-04 23:59:08 +02:00
|
|
|
tryDelete(cacheMetaFile)
|
2021-09-15 06:36:54 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 12:08:18 -06:00
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
const previouslyRestoredKey = fs.existsSync(cacheMetaFile)
|
|
|
|
? fs.readFileSync(cacheMetaFile, 'utf-8').trim()
|
|
|
|
: ''
|
2021-09-27 21:05:17 -06:00
|
|
|
const cacheKey = this.createCacheKey(bundle, commonArtifactFiles)
|
2021-09-15 06:36:54 -06:00
|
|
|
|
|
|
|
if (previouslyRestoredKey === cacheKey) {
|
2021-09-12 14:08:22 -06:00
|
|
|
this.debug(
|
2021-09-15 06:36:54 -06:00
|
|
|
`No change to previously restored ${bundle}. Not caching.`
|
2021-09-12 10:08:34 -06:00
|
|
|
)
|
2021-09-15 06:36:54 -06:00
|
|
|
} else {
|
2021-09-15 15:48:55 -06:00
|
|
|
core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
|
2021-09-27 20:57:47 -06:00
|
|
|
await this.saveCache([artifactPath], cacheKey)
|
2021-09-15 06:36:54 -06:00
|
|
|
|
|
|
|
this.debug(`Writing cache metafile: ${cacheMetaFile}`)
|
|
|
|
fs.writeFileSync(cacheMetaFile, cacheKey)
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
for (const file of commonArtifactFiles) {
|
2021-10-04 23:59:08 +02:00
|
|
|
tryDelete(file)
|
2021-09-15 06:36:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:05:17 -06:00
|
|
|
protected createCacheKey(bundle: string, files: string[]): string {
|
2021-09-15 06:36:54 -06:00
|
|
|
const cacheKeyPrefix = process.env['CACHE_KEY_PREFIX'] || ''
|
2021-09-27 21:05:17 -06:00
|
|
|
const relativeFiles = files.map(x =>
|
|
|
|
path.relative(this.gradleUserHome, x)
|
|
|
|
)
|
|
|
|
const key = hashFileNames(relativeFiles)
|
|
|
|
|
2021-10-15 11:11:05 -06:00
|
|
|
this.debug(
|
|
|
|
`Generating cache key for ${bundle} from files: ${relativeFiles}`
|
|
|
|
)
|
|
|
|
|
2021-09-15 15:48:55 -06:00
|
|
|
return `${cacheKeyPrefix}${bundle}-${key}`
|
2021-09-12 10:08:34 -06:00
|
|
|
}
|
|
|
|
|
2021-09-27 20:57:47 -06:00
|
|
|
protected determineGradleUserHome(rootDir: string): string {
|
|
|
|
const customGradleUserHome = process.env['GRADLE_USER_HOME']
|
|
|
|
if (customGradleUserHome) {
|
|
|
|
return path.resolve(rootDir, customGradleUserHome)
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:08:34 -06:00
|
|
|
return path.resolve(os.homedir(), '.gradle')
|
2021-09-11 09:10:44 -06:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
protected cacheOutputExists(): boolean {
|
|
|
|
// Need to check for 'caches' directory to avoid incorrect detection on MacOS agents
|
2021-09-27 20:57:47 -06:00
|
|
|
const dir = path.resolve(this.gradleUserHome, 'caches')
|
2021-09-06 11:16:08 -06:00
|
|
|
return fs.existsSync(dir)
|
2021-08-22 20:14:47 -06:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:16:08 -06:00
|
|
|
protected getCachePath(): string[] {
|
2021-09-27 20:57:47 -06:00
|
|
|
return CACHE_PATH.map(x => path.resolve(this.gradleUserHome, x))
|
|
|
|
}
|
|
|
|
|
|
|
|
private getCommonArtifactPaths(): Map<string, string> {
|
|
|
|
return new Map(
|
|
|
|
Array.from(COMMON_ARTIFACT_CACHES, ([key, value]) => [
|
|
|
|
key,
|
|
|
|
path.resolve(this.gradleUserHome, value)
|
|
|
|
])
|
|
|
|
)
|
2021-08-22 20:14:47 -06:00
|
|
|
}
|
2021-10-15 11:11:05 -06: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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
core.info(`Gradle User Home cache entry (directories >5M): ${label}`)
|
|
|
|
|
|
|
|
core.info(
|
|
|
|
result.stdout
|
|
|
|
.trimEnd()
|
|
|
|
.replace(/\t/g, ' ')
|
|
|
|
.split('\n')
|
|
|
|
.map(it => {
|
|
|
|
return ` ${it}`
|
|
|
|
})
|
|
|
|
.join('\n')
|
|
|
|
)
|
|
|
|
|
|
|
|
core.info('-----------------------')
|
|
|
|
}
|
2021-08-22 20:14:47 -06:00
|
|
|
}
|