Restore/save configuration-cache data in first action

Previously, the action was restoring/saving the configuration-cache data for each
step that applied the action. In order to support Gradle invocations that are _not_
managed by the action, the configuration-cache restore is now performed in the initial
action step, and save is performed in the final post-action step.

The build root directories are recorded for each invocation via an init script.
This commit is contained in:
Daz DeBoer 2021-12-07 16:52:53 -07:00
parent 1041604f29
commit e1f84aa44d
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
6 changed files with 107 additions and 75 deletions

View file

@ -1,15 +1,13 @@
import path from 'path'
import fs from 'fs'
import os from 'os'
import * as core from '@actions/core'
import * as glob from '@actions/glob'
import * as exec from '@actions/exec'
import {AbstractCache} from './cache-base'
import {AbstractCache, META_FILE_DIR} from './cache-base'
import {CacheEntryListener, CacheListener} from './cache-reporting'
import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils'
const META_FILE_DIR = '.gradle-build-action'
const META_FILE = 'cache-metadata.json'
const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes'
@ -47,16 +45,8 @@ class ExtractedCacheEntryMetadata {
* for more efficient storage.
*/
export class GradleUserHomeCache extends AbstractCache {
private gradleUserHome: string
constructor(rootDir: string) {
super('gradle', 'Gradle User Home')
this.gradleUserHome = this.determineGradleUserHome(rootDir)
}
init(): void {
this.debug(`Initializing Gradle User Home with properties and init script: ${this.gradleUserHome}`)
initializeGradleUserHome(this.gradleUserHome)
constructor(gradleUserHome: string) {
super(gradleUserHome, 'gradle', 'Gradle User Home')
}
/**
@ -285,15 +275,6 @@ export class GradleUserHomeCache extends AbstractCache {
fs.writeFileSync(cacheMetadataFile, filedata, 'utf-8')
}
protected determineGradleUserHome(rootDir: string): string {
const customGradleUserHome = process.env['GRADLE_USER_HOME']
if (customGradleUserHome) {
return path.resolve(rootDir, customGradleUserHome)
}
return path.resolve(os.homedir(), '.gradle')
}
/**
* Determines the paths within Gradle User Home to cache.
* By default, this is the 'caches' and 'notifications' directories,
@ -358,21 +339,17 @@ export class GradleUserHomeCache extends AbstractCache {
core.info('-----------------------')
}
}
function initializeGradleUserHome(gradleUserHome: string): void {
fs.mkdirSync(gradleUserHome, {recursive: true})
protected initializeGradleUserHome(gradleUserHome: string, initScriptsDir: string): void {
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
fs.writeFileSync(propertiesFile, 'org.gradle.daemon=false')
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
fs.writeFileSync(propertiesFile, 'org.gradle.daemon=false')
const buildScanCapture = path.resolve(initScriptsDir, 'build-scan-capture.init.gradle')
fs.writeFileSync(
buildScanCapture,
`import org.gradle.util.GradleVersion
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).
// Only run again root build. Do not run against included builds.
def isTopLevelBuild = gradle.getParent() == null
if (isTopLevelBuild) {
def version = GradleVersion.current().baseVersion
@ -412,7 +389,7 @@ def registerCallbacks(buildScanExtension, rootProjectName) {
println("::set-output name=build-scan-url::\${buildScan.buildScanUri}")
}
}
}
`
)
}`
)
}
}