Always initialize Gradle User Home with init script

The generated gradle.properties file and init script are not cached,
so need to be generated even when restoring from cache.
This commit is contained in:
Daz DeBoer 2021-11-27 20:35:01 -07:00
parent a72af0b6a6
commit 45ef022607
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
4 changed files with 55 additions and 61 deletions

View file

@ -1,41 +0,0 @@
import fs from 'fs'
export function writeBuildScanCaptureInitScript(initScript: string): void {
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
}
}
}
`
)
}

View file

@ -147,7 +147,6 @@ export abstract class AbstractCache {
if (!cacheResult) { if (!cacheResult) {
core.info(`${this.cacheDescription} cache not found. Will initialize empty.`) core.info(`${this.cacheDescription} cache not found. Will initialize empty.`)
await this.initializeState()
return return
} }
@ -182,8 +181,6 @@ export abstract class AbstractCache {
} }
} }
protected async initializeState(): Promise<void> {}
protected async afterRestore(_listener: CacheListener): Promise<void> {} protected async afterRestore(_listener: CacheListener): Promise<void> {}
async save(listener: CacheListener): Promise<void> { async save(listener: CacheListener): Promise<void> {

View file

@ -7,7 +7,6 @@ import * as exec from '@actions/exec'
import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base' import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base'
import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils' import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils'
import {writeBuildScanCaptureInitScript} from './build-scan-capture'
const META_FILE_DIR = '.gradle-build-action' const META_FILE_DIR = '.gradle-build-action'
@ -23,20 +22,9 @@ export class GradleUserHomeCache extends AbstractCache {
this.gradleUserHome = this.determineGradleUserHome(rootDir) this.gradleUserHome = this.determineGradleUserHome(rootDir)
} }
async initializeState(): Promise<void> { init(): void {
this.initializeGradleUserHome(this.gradleUserHome) this.debug(`Initializing Gradle User Home with properties and init script: ${this.gradleUserHome}`)
} initializeGradleUserHome(this.gradleUserHome)
private initializeGradleUserHome(gradleUserHome: string): void {
fs.mkdirSync(gradleUserHome, {recursive: true})
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
this.debug(`Initializing gradle.properties to disable daemon: ${propertiesFile}`)
fs.writeFileSync(propertiesFile, 'org.gradle.daemon=false')
const initScript = path.resolve(gradleUserHome, 'init.gradle')
this.debug(`Adding init script to capture build scans: ${initScript}`)
writeBuildScanCaptureInitScript(initScript)
} }
async afterRestore(listener: CacheListener): Promise<void> { async afterRestore(listener: CacheListener): Promise<void> {
@ -271,3 +259,49 @@ export class GradleUserHomeCache extends AbstractCache {
core.info('-----------------------') core.info('-----------------------')
} }
} }
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
}
}
}
`
)
}

View file

@ -8,6 +8,11 @@ const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
const CACHE_LISTENER = 'CACHE_LISTENER' const CACHE_LISTENER = 'CACHE_LISTENER'
export async function restore(buildRootDirectory: string): Promise<void> { export async function restore(buildRootDirectory: string): Promise<void> {
const gradleUserHomeCache = new GradleUserHomeCache(buildRootDirectory)
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
gradleUserHomeCache.init()
if (isCacheDisabled()) { if (isCacheDisabled()) {
core.info('Cache is disabled: will not restore state from previous builds.') core.info('Cache is disabled: will not restore state from previous builds.')
return return
@ -17,9 +22,8 @@ export async function restore(buildRootDirectory: string): Promise<void> {
core.saveState(BUILD_ROOT_DIR, buildRootDirectory) core.saveState(BUILD_ROOT_DIR, buildRootDirectory)
const cacheListener = new CacheListener() const cacheListener = new CacheListener()
await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener) await gradleUserHomeCache.restore(cacheListener)
const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
if (cacheListener.fullyRestored) { if (cacheListener.fullyRestored) {
// Only restore the configuration-cache if the Gradle Home is fully restored // Only restore the configuration-cache if the Gradle Home is fully restored
await projectDotGradleCache.restore(cacheListener) await projectDotGradleCache.restore(cacheListener)