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-12-07 12:29:37 -07:00
|
|
|
import {AbstractCache} from './cache-base'
|
|
|
|
import {CacheEntryListener, CacheListener} from './cache-reporting'
|
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-11-28 10:19:56 -07:00
|
|
|
const META_FILE = 'cache-metadata.json'
|
2021-10-21 11:11:55 -06:00
|
|
|
|
2021-10-27 16:05:07 -06:00
|
|
|
const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes'
|
|
|
|
const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes'
|
2021-12-02 14:38:23 -07:00
|
|
|
const EXTRACTED_CACHE_ENTRIES_PARAMETER = 'gradle-home-extracted-cache-entries'
|
2021-10-27 16:05:07 -06:00
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Represents the result of attempting to load or store an extracted cache entry.
|
2021-11-28 10:19:56 -07:00
|
|
|
* An undefined cacheKey indicates that the operation did not succeed.
|
|
|
|
* The collected results are then used to populate the `cache-metadata.json` file for later use.
|
|
|
|
*/
|
2021-12-02 14:38:23 -07:00
|
|
|
class ExtractedCacheEntry {
|
|
|
|
artifactType: string
|
|
|
|
pattern: string
|
|
|
|
cacheKey: string | undefined
|
|
|
|
|
|
|
|
constructor(artifactType: string, pattern: string, cacheKey: string | undefined) {
|
|
|
|
this.artifactType = artifactType
|
|
|
|
this.pattern = pattern
|
2021-11-28 10:19:56 -07:00
|
|
|
this.cacheKey = cacheKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Representation of all of the extracted cache entries for this Gradle User Home.
|
|
|
|
* This object is persisted to JSON file in the Gradle User Home directory for storing,
|
|
|
|
* and subsequently used to restore the Gradle User Home.
|
|
|
|
*/
|
|
|
|
class ExtractedCacheEntryMetadata {
|
|
|
|
entries: ExtractedCacheEntry[] = []
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches and restores the entire Gradle User Home directory, extracting entries containing common artifacts
|
2021-11-28 10:19:56 -07:00
|
|
|
* for more efficient storage.
|
|
|
|
*/
|
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-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Restore any extracted cache entries after the main Gradle User Home entry is restored.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-10-30 07:21:27 -06:00
|
|
|
async afterRestore(listener: CacheListener): Promise<void> {
|
2021-11-28 10:19:56 -07:00
|
|
|
await this.debugReportGradleUserHomeSize('as restored from cache')
|
2021-12-02 14:38:23 -07:00
|
|
|
await this.restoreExtractedCacheEntries(listener)
|
2021-11-28 10:19:56 -07:00
|
|
|
await this.debugReportGradleUserHomeSize('after restoring common artifacts')
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
2021-09-11 09:10:44 -06:00
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
|
|
|
* Restores any artifacts that were cached separately, based on the information in the `cache-metadata.json` file.
|
2021-12-02 14:38:23 -07:00
|
|
|
* Each extracted cache entry is restored in parallel, except when debugging is enabled.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-12-02 14:38:23 -07:00
|
|
|
private async restoreExtractedCacheEntries(listener: CacheListener): Promise<void> {
|
|
|
|
const extractedCacheEntryDefinitions = this.getExtractedCacheEntryDefinitions()
|
|
|
|
const previouslyExtractedCacheEntries = this.loadExtractedCacheEntries()
|
2021-10-29 10:19:35 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
const processes: Promise<ExtractedCacheEntry>[] = []
|
2021-10-29 10:19:35 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
for (const cacheEntry of previouslyExtractedCacheEntries) {
|
|
|
|
const artifactType = cacheEntry.artifactType
|
|
|
|
const entryListener = listener.entry(cacheEntry.pattern)
|
2021-10-30 06:24:34 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
// Handle case where the extracted-cache-entry definitions have been changed
|
|
|
|
if (extractedCacheEntryDefinitions.get(artifactType) === undefined) {
|
|
|
|
core.info(`Found extracted cache entry for ${artifactType} but no such entry defined`)
|
|
|
|
entryListener.markRequested('EXTRACTED_ENTRY_NOT_DEFINED')
|
2021-10-30 06:24:34 -06:00
|
|
|
} else {
|
2021-12-02 14:38:23 -07:00
|
|
|
processes.push(
|
|
|
|
this.restoreExtractedCacheEntry(
|
|
|
|
artifactType,
|
|
|
|
cacheEntry.cacheKey!,
|
|
|
|
cacheEntry.pattern,
|
|
|
|
entryListener
|
|
|
|
)
|
|
|
|
)
|
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-12-02 14:38:23 -07:00
|
|
|
this.saveMetadataForCacheResults(await this.collectCacheResults(processes))
|
2021-09-11 20:56:40 -06:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
private async restoreExtractedCacheEntry(
|
|
|
|
artifactType: string,
|
2021-11-28 10:19:56 -07:00
|
|
|
cacheKey: string,
|
2021-12-02 14:38:23 -07:00
|
|
|
pattern: string,
|
2021-10-30 07:21:27 -06:00
|
|
|
listener: CacheEntryListener
|
2021-12-02 14:38:23 -07:00
|
|
|
): Promise<ExtractedCacheEntry> {
|
2021-10-30 07:21:27 -06:00
|
|
|
listener.markRequested(cacheKey)
|
2021-10-30 06:24:34 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
const restoredEntry = await this.restoreCache([pattern], cacheKey)
|
2021-10-30 12:17:41 -06:00
|
|
|
if (restoredEntry) {
|
2021-12-02 14:38:23 -07:00
|
|
|
core.info(`Restored ${artifactType} with key ${cacheKey} to ${pattern}`)
|
2021-10-30 12:17:41 -06:00
|
|
|
listener.markRestored(restoredEntry.key, restoredEntry.size)
|
2021-12-02 14:38:23 -07:00
|
|
|
return new ExtractedCacheEntry(artifactType, pattern, cacheKey)
|
2021-09-11 20:56:40 -06:00
|
|
|
} else {
|
2021-12-02 14:38:23 -07:00
|
|
|
core.info(`Did not restore ${artifactType} with key ${cacheKey} to ${pattern}`)
|
|
|
|
return new ExtractedCacheEntry(artifactType, pattern, undefined)
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Extract and save any defined extracted cache entries prior to the main Gradle User Home entry being saved.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-10-30 07:21:27 -06:00
|
|
|
async beforeSave(listener: CacheListener): Promise<void> {
|
2021-11-28 10:19:56 -07:00
|
|
|
await this.debugReportGradleUserHomeSize('before saving common artifacts')
|
2021-10-20 15:01:36 -06:00
|
|
|
this.removeExcludedPaths()
|
2021-12-02 14:38:23 -07:00
|
|
|
await this.saveExtractedCacheEntries(listener)
|
2021-11-28 10:19:56 -07:00
|
|
|
await this.debugReportGradleUserHomeSize(
|
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-11-28 10:19:56 -07:00
|
|
|
/**
|
|
|
|
* Delete any file paths that are excluded by the `gradle-home-cache-excludes` parameter.
|
|
|
|
*/
|
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-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Saves any artifacts that are configured to be cached separately, based on the extracted cache entry definitions.
|
|
|
|
* These definitions are normally fixed, but can be overridden by the `gradle-home-extracted-cache-entries` parameter.
|
|
|
|
* Each entry is extracted and saved in parallel, except when debugging is enabled.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-12-02 14:38:23 -07:00
|
|
|
private async saveExtractedCacheEntries(listener: CacheListener): Promise<void> {
|
|
|
|
// Load the cache entry definitions (from config) and the previously restored entries (from filesystem)
|
|
|
|
const cacheEntryDefinitions = this.getExtractedCacheEntryDefinitions()
|
|
|
|
const previouslyRestoredEntries = this.loadExtractedCacheEntries()
|
|
|
|
const cacheActions: Promise<ExtractedCacheEntry>[] = []
|
|
|
|
|
|
|
|
for (const [artifactType, pattern] of cacheEntryDefinitions) {
|
|
|
|
// Find all matching files for this cache entry definition
|
|
|
|
const globber = await glob.create(pattern, {
|
|
|
|
implicitDescendants: false,
|
|
|
|
followSymbolicLinks: false
|
|
|
|
})
|
|
|
|
const matchingFiles = await globber.glob()
|
|
|
|
|
|
|
|
if (matchingFiles.length === 0) {
|
|
|
|
this.debug(`No files found to cache for ${artifactType}`)
|
|
|
|
continue
|
2021-09-12 14:08:22 -06:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
if (this.isBundlePattern(pattern)) {
|
|
|
|
// For an extracted "bundle", use the defined pattern and cache all matching files in a single entry.
|
|
|
|
cacheActions.push(
|
|
|
|
this.saveExtractedCacheEntry(
|
|
|
|
matchingFiles,
|
|
|
|
artifactType,
|
|
|
|
pattern,
|
|
|
|
previouslyRestoredEntries,
|
|
|
|
listener.entry(pattern)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Otherwise cache each matching file in a separate entry, using the complete file path as the cache pattern.
|
|
|
|
for (const cacheFile of matchingFiles) {
|
|
|
|
cacheActions.push(
|
|
|
|
this.saveExtractedCacheEntry(
|
|
|
|
[cacheFile],
|
|
|
|
artifactType,
|
|
|
|
cacheFile,
|
|
|
|
previouslyRestoredEntries,
|
|
|
|
listener.entry(cacheFile)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 10:19:56 -07:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
this.saveMetadataForCacheResults(await this.collectCacheResults(cacheActions))
|
2021-09-11 20:56:40 -06:00
|
|
|
}
|
2021-09-11 09:10:44 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
private async saveExtractedCacheEntry(
|
|
|
|
matchingFiles: string[],
|
|
|
|
artifactType: string,
|
|
|
|
pattern: string,
|
|
|
|
previouslyRestoredEntries: ExtractedCacheEntry[],
|
|
|
|
entryListener: CacheEntryListener
|
|
|
|
): Promise<ExtractedCacheEntry> {
|
|
|
|
const cacheKey = this.createCacheKeyForArtifacts(artifactType, matchingFiles)
|
|
|
|
const previouslyRestoredKey = previouslyRestoredEntries.find(
|
|
|
|
x => x.artifactType === artifactType && x.pattern === pattern
|
|
|
|
)?.cacheKey
|
2021-09-15 06:36:54 -06:00
|
|
|
|
|
|
|
if (previouslyRestoredKey === cacheKey) {
|
2021-12-02 14:38:23 -07:00
|
|
|
this.debug(`No change to previously restored ${artifactType}. Not saving.`)
|
2021-09-15 06:36:54 -06:00
|
|
|
} else {
|
2021-12-02 14:38:23 -07:00
|
|
|
core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
|
|
|
|
const savedEntry = await this.saveCache([pattern], cacheKey)
|
2021-10-30 12:17:41 -06:00
|
|
|
if (savedEntry !== undefined) {
|
2021-12-02 14:38:23 -07:00
|
|
|
entryListener.markSaved(savedEntry.key, savedEntry.size)
|
2021-10-30 12:17:41 -06:00
|
|
|
}
|
2021-09-11 12:08:18 -06:00
|
|
|
}
|
2021-09-11 20:56:40 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
for (const file of matchingFiles) {
|
2021-10-04 23:59:08 +02:00
|
|
|
tryDelete(file)
|
2021-09-15 06:36:54 -06:00
|
|
|
}
|
2021-11-28 10:19:56 -07:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
return new ExtractedCacheEntry(artifactType, pattern, cacheKey)
|
2021-09-15 06:36:54 -06:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
protected createCacheKeyForArtifacts(artifactType: 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-12-02 14:38:23 -07:00
|
|
|
this.debug(`Generating cache key for ${artifactType} from files: ${relativeFiles}`)
|
|
|
|
|
|
|
|
return `${cacheKeyPrefix}${artifactType}-${key}`
|
|
|
|
}
|
2021-10-15 11:11:05 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
private isBundlePattern(pattern: string): boolean {
|
|
|
|
return pattern.endsWith('*')
|
|
|
|
}
|
|
|
|
|
|
|
|
private async collectCacheResults(processes: Promise<ExtractedCacheEntry>[]): Promise<ExtractedCacheEntry[]> {
|
|
|
|
// Run cache actions sequentially when debugging enabled
|
|
|
|
if (this.cacheDebuggingEnabled) {
|
|
|
|
for (const p of processes) {
|
|
|
|
await p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return await Promise.all(processes)
|
2021-09-12 10:08:34 -06:00
|
|
|
}
|
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Load information about the extracted cache entries previously restored/saved. This is loaded from the 'cache-metadata.json' file.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-12-02 14:38:23 -07:00
|
|
|
private loadExtractedCacheEntries(): ExtractedCacheEntry[] {
|
|
|
|
const cacheMetadataFile = path.resolve(this.gradleUserHome, META_FILE_DIR, META_FILE)
|
|
|
|
if (!fs.existsSync(cacheMetadataFile)) {
|
|
|
|
return []
|
2021-11-28 10:19:56 -07:00
|
|
|
}
|
2021-12-02 14:38:23 -07:00
|
|
|
|
|
|
|
const filedata = fs.readFileSync(cacheMetadataFile, 'utf-8')
|
|
|
|
core.debug(`Loaded cache metadata: ${filedata}`)
|
|
|
|
const extractedCacheEntryMetadata = JSON.parse(filedata) as ExtractedCacheEntryMetadata
|
|
|
|
return extractedCacheEntryMetadata.entries
|
2021-11-28 10:19:56 -07:00
|
|
|
}
|
2021-10-21 11:11:55 -06:00
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Saves information about the extracted cache entries into the 'cache-metadata.json' file.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-12-02 14:38:23 -07:00
|
|
|
private saveMetadataForCacheResults(results: ExtractedCacheEntry[]): void {
|
|
|
|
const extractedCacheEntryMetadata = new ExtractedCacheEntryMetadata()
|
|
|
|
extractedCacheEntryMetadata.entries = results.filter(x => x.cacheKey !== undefined)
|
2021-10-21 11:11:55 -06:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
const filedata = JSON.stringify(extractedCacheEntryMetadata)
|
|
|
|
core.debug(`Saving cache metadata: ${filedata}`)
|
2021-11-28 10:19:56 -07:00
|
|
|
|
2021-12-02 14:38:23 -07:00
|
|
|
const actionMetadataDirectory = path.resolve(this.gradleUserHome, META_FILE_DIR)
|
|
|
|
const cacheMetadataFile = path.resolve(actionMetadataDirectory, META_FILE)
|
|
|
|
|
|
|
|
fs.mkdirSync(actionMetadataDirectory, {recursive: true})
|
|
|
|
fs.writeFileSync(cacheMetadataFile, filedata, 'utf-8')
|
2021-10-21 11:11:55 -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-11-28 10:19:56 -07:00
|
|
|
/**
|
|
|
|
* Determines the paths within Gradle User Home to cache.
|
|
|
|
* By default, this is the 'caches' and 'notifications' directories,
|
|
|
|
* but this can be overridden by the `gradle-home-cache-includes` parameter.
|
|
|
|
*/
|
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-11-28 10:19:56 -07:00
|
|
|
/**
|
2021-12-02 14:38:23 -07:00
|
|
|
* Return the extracted cache entry definitions, which determine which artifacts will be cached
|
2021-11-28 10:19:56 -07:00
|
|
|
* separately from the rest of the Gradle User Home cache entry.
|
2021-12-02 14:38:23 -07:00
|
|
|
* This is normally a fixed set, but can be overridden by the `gradle-home-extracted-cache-entries` parameter.
|
2021-11-28 10:19:56 -07:00
|
|
|
*/
|
2021-12-02 14:38:23 -07:00
|
|
|
private getExtractedCacheEntryDefinitions(): Map<string, string> {
|
|
|
|
const rawDefinitions = core.getInput(EXTRACTED_CACHE_ENTRIES_PARAMETER)
|
|
|
|
const parsedDefinitions = JSON.parse(rawDefinitions)
|
|
|
|
return new Map(Array.from(parsedDefinitions, ([key, value]) => [key, path.resolve(this.gradleUserHome, value)]))
|
2021-08-22 20:14:47 -06:00
|
|
|
}
|
2021-10-15 11:11:05 -06:00
|
|
|
|
2021-11-28 10:19:56 -07:00
|
|
|
/**
|
|
|
|
* When cache debugging is enabled, this method will give a detailed report
|
|
|
|
* of the Gradle User Home contents.
|
|
|
|
*/
|
|
|
|
private async debugReportGradleUserHomeSize(label: string): Promise<void> {
|
2021-10-15 11:11:05 -06:00
|
|
|
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 {
|
2021-11-28 08:03:42 -07:00
|
|
|
def buildOutcome = ""
|
2021-11-27 20:35:01 -07:00
|
|
|
def scanFile = new File("gradle-build-scan.txt")
|
2021-11-28 08:03:42 -07:00
|
|
|
|
|
|
|
buildFinished { result ->
|
|
|
|
buildOutcome = result.failure == null ? " succeeded" : " failed"
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:35:01 -07:00
|
|
|
buildScanPublished { buildScan ->
|
|
|
|
scanFile.text = buildScan.buildScanUri
|
2021-11-27 20:49:10 -07:00
|
|
|
|
2021-11-28 08:03:42 -07:00
|
|
|
// Send commands directly to GitHub Actions via STDOUT.
|
2021-12-08 12:52:37 -07:00
|
|
|
def message = "Build '\${rootProjectName}'\${buildOutcome} - \${buildScan.buildScanUri}"
|
|
|
|
println("::notice ::\${message}")
|
2021-11-28 08:03:42 -07:00
|
|
|
println("::set-output name=build-scan-url::\${buildScan.buildScanUri}")
|
2021-11-27 20:35:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
}
|