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-30 07:21:27 -06:00
|
|
|
import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base'
|
2021-10-30 07:15:20 -06:00
|
|
|
import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils'
|
2021-09-12 10:08:34 -06:00
|
|
|
|
2021-10-21 11:11:55 -06:00
|
|
|
const META_FILE_DIR = '.gradle-build-action'
|
|
|
|
|
2021-10-27 16:05:07 -06:00
|
|
|
const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes'
|
|
|
|
const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes'
|
|
|
|
const ARTIFACT_BUNDLES_PARAMETER = 'gradle-home-cache-artifact-bundles'
|
|
|
|
|
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-11-27 20:35:01 -07:00
|
|
|
init(): void {
|
|
|
|
this.debug(`Initializing Gradle User Home with properties and init script: ${this.gradleUserHome}`)
|
|
|
|
initializeGradleUserHome(this.gradleUserHome)
|
2021-11-27 16:07:07 -07:00
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
async afterRestore(listener: CacheListener): Promise<void> {
|
2021-10-15 11:11:05 -06:00
|
|
|
await this.reportGradleUserHomeSize('as restored from cache')
|
2021-10-30 07:21:27 -06:00
|
|
|
await this.restoreArtifactBundles(listener)
|
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-10-30 07:21:27 -06:00
|
|
|
private async restoreArtifactBundles(listener: CacheListener): Promise<void> {
|
2021-10-29 10:19:35 -06:00
|
|
|
const processes: Promise<void>[] = []
|
|
|
|
|
2021-10-30 06:24:34 -06:00
|
|
|
const bundleMetaFiles = await this.getBundleMetaFiles()
|
|
|
|
const bundlePatterns = this.getArtifactBundles()
|
2021-10-29 10:19:35 -06:00
|
|
|
|
2021-10-30 06:24:34 -06:00
|
|
|
// Iterate over all bundle meta files and try to restore
|
|
|
|
for (const bundleMetaFile of bundleMetaFiles) {
|
|
|
|
const bundle = path.basename(bundleMetaFile, '.cache')
|
2021-10-30 07:21:27 -06:00
|
|
|
const entryListener = listener.entry(bundle)
|
2021-10-30 06:24:34 -06:00
|
|
|
const bundlePattern = bundlePatterns.get(bundle)
|
|
|
|
|
|
|
|
// Handle case where the 'artifactBundlePatterns' have been changed
|
|
|
|
if (bundlePattern === undefined) {
|
|
|
|
core.info(`Found bundle metafile for ${bundle} but no such bundle defined`)
|
2021-10-30 07:21:27 -06:00
|
|
|
entryListener.markRequested('BUNDLE_NOT_CONFIGURED')
|
2021-10-30 06:24:34 -06:00
|
|
|
tryDelete(bundleMetaFile)
|
|
|
|
} else {
|
2021-10-30 07:21:27 -06:00
|
|
|
const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, entryListener)
|
2021-10-30 06:24:34 -06:00
|
|
|
// Run sequentially when debugging enabled
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
await p
|
|
|
|
}
|
|
|
|
processes.push(p)
|
2021-09-12 14:08:22 -06:00
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
}
|
2021-09-12 14:08:22 -06:00
|
|
|
|
2021-10-29 10:19:35 -06:00
|
|
|
await Promise.all(processes)
|
2021-09-11 20:56:40 -06:00
|
|
|
}
|
|
|
|
|
2021-10-30 06:24:34 -06:00
|
|
|
private async restoreArtifactBundle(
|
|
|
|
bundle: string,
|
|
|
|
bundlePattern: string,
|
|
|
|
bundleMetaFile: string,
|
2021-10-30 07:21:27 -06:00
|
|
|
listener: CacheEntryListener
|
2021-10-30 06:24:34 -06:00
|
|
|
): Promise<void> {
|
|
|
|
const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
|
2021-10-30 07:21:27 -06:00
|
|
|
listener.markRequested(cacheKey)
|
2021-10-30 06:24:34 -06:00
|
|
|
|
2021-10-30 12:17:41 -06:00
|
|
|
const restoredEntry = await this.restoreCache([bundlePattern], cacheKey)
|
|
|
|
if (restoredEntry) {
|
2021-10-30 06:24:34 -06:00
|
|
|
core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`)
|
2021-10-30 12:17:41 -06:00
|
|
|
listener.markRestored(restoredEntry.key, restoredEntry.size)
|
2021-09-11 20:56:40 -06:00
|
|
|
} else {
|
2021-10-30 06:24:34 -06:00
|
|
|
core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
|
|
|
|
tryDelete(bundleMetaFile)
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 12:34:38 -06:00
|
|
|
private getBundleMetaFile(name: string): string {
|
2021-10-21 11:11:55 -06:00
|
|
|
return path.resolve(this.gradleUserHome, META_FILE_DIR, `${name}.cache`)
|
2021-09-15 06:36:54 -06:00
|
|
|
}
|
|
|
|
|
2021-10-29 10:19:35 -06:00
|
|
|
private async getBundleMetaFiles(): Promise<string[]> {
|
|
|
|
const metaFiles = path.resolve(this.gradleUserHome, META_FILE_DIR, '*.cache')
|
|
|
|
const globber = await glob.create(metaFiles)
|
|
|
|
const bundleFiles = await globber.glob()
|
|
|
|
return bundleFiles
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
async beforeSave(listener: CacheListener): Promise<void> {
|
2021-10-15 11:11:05 -06:00
|
|
|
await this.reportGradleUserHomeSize('before saving common artifacts')
|
2021-10-20 15:01:36 -06:00
|
|
|
this.removeExcludedPaths()
|
2021-10-30 07:21:27 -06:00
|
|
|
await this.saveArtifactBundles(listener)
|
2021-10-16 10:15:40 -06:00
|
|
|
await this.reportGradleUserHomeSize(
|
2021-10-20 15:01:36 -06:00
|
|
|
"after saving common artifacts (only 'caches' and 'notifications' will be stored)"
|
2021-10-16 10:15:40 -06:00
|
|
|
)
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
|
|
|
|
2021-10-20 15:01:36 -06:00
|
|
|
private removeExcludedPaths(): void {
|
2021-10-29 07:34:44 -06:00
|
|
|
const rawPaths: string[] = core.getMultilineInput(EXCLUDE_PATHS_PARAMETER)
|
|
|
|
const resolvedPaths = rawPaths.map(x => path.resolve(this.gradleUserHome, x))
|
2021-10-20 15:01:36 -06:00
|
|
|
|
|
|
|
for (const p of resolvedPaths) {
|
|
|
|
this.debug(`Deleting excluded path: ${p}`)
|
|
|
|
tryDelete(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
private async saveArtifactBundles(listener: CacheListener): Promise<void> {
|
2021-09-11 20:56:40 -06:00
|
|
|
const processes: Promise<void>[] = []
|
2021-10-15 12:34:38 -06:00
|
|
|
for (const [bundle, pattern] of this.getArtifactBundles()) {
|
2021-10-30 07:21:27 -06:00
|
|
|
const entryListener = listener.entry(bundle)
|
2021-10-29 10:41:30 -06:00
|
|
|
|
2021-10-30 07:21:27 -06:00
|
|
|
const p = this.saveArtifactBundle(bundle, pattern, entryListener)
|
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-10-30 07:21:27 -06:00
|
|
|
private async saveArtifactBundle(
|
|
|
|
bundle: string,
|
|
|
|
artifactPath: string,
|
|
|
|
listener: CacheEntryListener
|
|
|
|
): Promise<void> {
|
2021-10-15 12:34:38 -06:00
|
|
|
const bundleMetaFile = this.getBundleMetaFile(bundle)
|
2021-09-11 20:56:40 -06:00
|
|
|
|
2021-10-16 09:46:17 -06:00
|
|
|
const globber = await glob.create(artifactPath, {
|
|
|
|
implicitDescendants: false,
|
|
|
|
followSymbolicLinks: false
|
|
|
|
})
|
2021-10-15 12:34:38 -06:00
|
|
|
const bundleFiles = await globber.glob()
|
2021-09-12 14:08:22 -06:00
|
|
|
|
2021-09-15 06:36:54 -06:00
|
|
|
// Handle no matching files
|
2021-10-15 12:34:38 -06:00
|
|
|
if (bundleFiles.length === 0) {
|
2021-09-15 06:36:54 -06:00
|
|
|
this.debug(`No files found to cache for ${bundle}`)
|
2021-10-15 12:34:38 -06:00
|
|
|
if (fs.existsSync(bundleMetaFile)) {
|
|
|
|
tryDelete(bundleMetaFile)
|
2021-09-15 06:36:54 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 12:08:18 -06:00
|
|
|
|
2021-10-15 12:34:38 -06:00
|
|
|
const previouslyRestoredKey = fs.existsSync(bundleMetaFile)
|
|
|
|
? fs.readFileSync(bundleMetaFile, 'utf-8').trim()
|
2021-09-15 06:36:54 -06:00
|
|
|
: ''
|
2021-10-15 12:34:38 -06:00
|
|
|
const cacheKey = this.createCacheKey(bundle, bundleFiles)
|
2021-09-15 06:36:54 -06:00
|
|
|
|
|
|
|
if (previouslyRestoredKey === cacheKey) {
|
2021-10-29 07:34:44 -06:00
|
|
|
this.debug(`No change to previously restored ${bundle}. Not caching.`)
|
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-10-30 12:17:41 -06:00
|
|
|
const savedEntry = await this.saveCache([artifactPath], cacheKey)
|
|
|
|
if (savedEntry !== undefined) {
|
|
|
|
this.writeBundleMetaFile(bundleMetaFile, cacheKey)
|
|
|
|
listener.markSaved(savedEntry.key, savedEntry.size)
|
|
|
|
}
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
|
2021-10-15 12:34:38 -06:00
|
|
|
for (const file of bundleFiles) {
|
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-10-16 09:44:35 -06:00
|
|
|
const cacheKeyPrefix = getCacheKeyPrefix()
|
2021-10-29 07:34:44 -06:00
|
|
|
const relativeFiles = files.map(x => path.relative(this.gradleUserHome, x))
|
2021-09-27 21:05:17 -06:00
|
|
|
const key = hashFileNames(relativeFiles)
|
|
|
|
|
2021-10-29 07:34:44 -06:00
|
|
|
this.debug(`Generating cache key for ${bundle} from files: ${relativeFiles}`)
|
2021-10-15 11:11:05 -06:00
|
|
|
|
2021-09-15 15:48:55 -06:00
|
|
|
return `${cacheKeyPrefix}${bundle}-${key}`
|
2021-09-12 10:08:34 -06:00
|
|
|
}
|
|
|
|
|
2021-10-21 11:11:55 -06:00
|
|
|
private writeBundleMetaFile(metaFile: string, cacheKey: string): void {
|
|
|
|
this.debug(`Writing bundle metafile: ${metaFile}`)
|
|
|
|
|
|
|
|
const dirName = path.dirname(metaFile)
|
|
|
|
if (!fs.existsSync(dirName)) {
|
|
|
|
fs.mkdirSync(dirName)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(metaFile, cacheKey)
|
|
|
|
}
|
|
|
|
|
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-10-29 07:34:44 -06:00
|
|
|
const rawPaths: string[] = core.getMultilineInput(INCLUDE_PATHS_PARAMETER)
|
2021-10-20 09:52:04 -06:00
|
|
|
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))
|
2021-10-20 15:01:36 -06:00
|
|
|
return `!${resolved}`
|
2021-10-20 09:52:04 -06:00
|
|
|
}
|
|
|
|
return path.resolve(this.gradleUserHome, rawPath)
|
2021-09-27 20:57:47 -06:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:34:38 -06:00
|
|
|
private getArtifactBundles(): Map<string, string> {
|
2021-10-29 07:34:44 -06:00
|
|
|
const artifactBundleDefinition = core.getInput(ARTIFACT_BUNDLES_PARAMETER)
|
|
|
|
this.debug(`Using artifact bundle definition: ${artifactBundleDefinition}`)
|
2021-10-15 12:45:15 -06:00
|
|
|
const artifactBundles = JSON.parse(artifactBundleDefinition)
|
2021-10-29 07:34:44 -06:00
|
|
|
return new Map(Array.from(artifactBundles, ([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
|
|
|
|
}
|
2021-10-29 07:34:44 -06:00
|
|
|
const result = await exec.getExecOutput('du', ['-h', '-c', '-t', '5M'], {
|
|
|
|
cwd: this.gradleUserHome,
|
|
|
|
silent: true,
|
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
2021-10-15 11:11:05 -06:00
|
|
|
|
2021-10-16 10:15:40 -06:00
|
|
|
core.info(`Gradle User Home (directories >5M): ${label}`)
|
2021-10-15 11:11:05 -06:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-11-27 20:35:01 -07:00
|
|
|
|
|
|
|
function initializeGradleUserHome(gradleUserHome: string): void {
|
|
|
|
fs.mkdirSync(gradleUserHome, {recursive: true})
|
|
|
|
|
|
|
|
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
|
|
|
|
fs.writeFileSync(propertiesFile, 'org.gradle.daemon=false')
|
|
|
|
|
|
|
|
const initScript = path.resolve(gradleUserHome, 'init.gradle')
|
|
|
|
fs.writeFileSync(
|
|
|
|
initScript,
|
|
|
|
`
|
|
|
|
import org.gradle.util.GradleVersion
|
|
|
|
|
|
|
|
// Don't run against the included builds (if the main build has any).
|
|
|
|
def isTopLevelBuild = gradle.getParent() == null
|
|
|
|
if (isTopLevelBuild) {
|
|
|
|
def version = GradleVersion.current().baseVersion
|
|
|
|
def atLeastGradle4 = version >= GradleVersion.version("4.0")
|
|
|
|
def atLeastGradle6 = version >= GradleVersion.version("6.0")
|
|
|
|
|
|
|
|
if (atLeastGradle6) {
|
|
|
|
settingsEvaluated { settings ->
|
|
|
|
if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) {
|
|
|
|
registerCallbacks(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (atLeastGradle4) {
|
|
|
|
projectsEvaluated { gradle ->
|
|
|
|
if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) {
|
|
|
|
registerCallbacks(gradle.rootProject.extensions["buildScan"], gradle.rootProject.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def registerCallbacks(buildScanExtension, rootProjectName) {
|
|
|
|
buildScanExtension.with {
|
|
|
|
def scanFile = new File("gradle-build-scan.txt")
|
|
|
|
buildScanPublished { buildScan ->
|
|
|
|
scanFile.text = buildScan.buildScanUri
|
2021-11-27 20:49:10 -07:00
|
|
|
|
|
|
|
println('::notice title=build-scan-url::' + buildScan.buildScanUri)
|
|
|
|
println('::set-output name=build-scan-url::' + buildScan.buildScanUri)
|
2021-11-27 20:35:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
}
|