Use build-results file for root project dirs

Instead of using a separate mechanism and init script, reuse the information
captured in the build-results file.
This commit is contained in:
Daz DeBoer 2022-06-05 08:34:07 -06:00
parent e234151ec9
commit e644288a42
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
6 changed files with 8 additions and 145 deletions

View file

@ -9,7 +9,6 @@ import {ConfigurationCacheEntryExtractor, GradleHomeEntryExtractor} from './cach
const RESTORED_CACHE_KEY_KEY = 'restored-cache-key' const RESTORED_CACHE_KEY_KEY = 'restored-cache-key'
export const META_FILE_DIR = '.gradle-build-action' export const META_FILE_DIR = '.gradle-build-action'
export const PROJECT_ROOTS_FILE = 'project-roots.txt'
const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes' const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes'
const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes' const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes'
@ -170,12 +169,7 @@ export class GradleStateCache {
const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties') const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties')
fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false') fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false')
const initScriptFilenames = [ const initScriptFilenames = ['build-result-capture.init.gradle', 'build-result-capture-service.plugin.groovy']
'build-result-capture.init.gradle',
'build-result-capture-service.plugin.groovy',
'project-root-capture.init.gradle',
'project-root-capture.plugin.groovy'
]
for (const initScriptFilename of initScriptFilenames) { for (const initScriptFilename of initScriptFilenames) {
const initScriptContent = this.readResourceAsString(initScriptFilename) const initScriptContent = this.readResourceAsString(initScriptFilename)
const initScriptPath = path.resolve(initScriptsDir, initScriptFilename) const initScriptPath = path.resolve(initScriptsDir, initScriptFilename)

View file

@ -3,7 +3,7 @@ import fs from 'fs'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as glob from '@actions/glob' import * as glob from '@actions/glob'
import {META_FILE_DIR, PROJECT_ROOTS_FILE} from './cache-base' import {META_FILE_DIR} from './cache-base'
import {CacheEntryListener, CacheListener} from './cache-reporting' import {CacheEntryListener, CacheListener} from './cache-reporting'
import { import {
cacheDebug, cacheDebug,
@ -14,6 +14,7 @@ import {
saveCache, saveCache,
tryDelete tryDelete
} from './cache-utils' } from './cache-utils'
import {loadBuildResults} from './job-summary'
const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE' const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE'
@ -387,13 +388,8 @@ export class ConfigurationCacheEntryExtractor extends AbstractEntryExtractor {
* set of project roots, to allow saving of configuration-cache entries for each. * set of project roots, to allow saving of configuration-cache entries for each.
*/ */
private getProjectRoots(): string[] { private getProjectRoots(): string[] {
const projectList = path.resolve(process.env['RUNNER_TEMP']!, PROJECT_ROOTS_FILE) const buildResults = loadBuildResults()
if (!fs.existsSync(projectList)) { const projectRootDirs = buildResults.map(x => x.rootProjectDir)
core.info(`Missing project list file ${projectList}`) return [...new Set(projectRootDirs)] // Remove duplicates
return []
}
const projectRoots = fs.readFileSync(projectList, 'utf-8')
core.info(`Found project roots '${projectRoots}' in ${projectList}`)
return projectRoots.trim().split('\n')
} }
} }

View file

@ -3,7 +3,7 @@ import fs from 'fs'
import path from 'path' import path from 'path'
import {logCachingReport, CacheListener} from './cache-reporting' import {logCachingReport, CacheListener} from './cache-reporting'
interface BuildResult { export interface BuildResult {
get rootProjectName(): string get rootProjectName(): string
get rootProjectDir(): string get rootProjectDir(): string
get requestedTasks(): string get requestedTasks(): string
@ -28,7 +28,7 @@ export function writeJobSummary(cacheListener: CacheListener): void {
core.summary.write() core.summary.write()
} }
function loadBuildResults(): BuildResult[] { export function loadBuildResults(): BuildResult[] {
const buildResultsDir = path.resolve(process.env['RUNNER_TEMP']!, '.build-results') const buildResultsDir = path.resolve(process.env['RUNNER_TEMP']!, '.build-results')
if (!fs.existsSync(buildResultsDir)) { if (!fs.existsSync(buildResultsDir)) {
return [] return []

View file

@ -1,10 +0,0 @@
import org.gradle.util.GradleVersion
// Only run against root build. Do not run against included builds.
def isTopLevelBuild = gradle.getParent() == null
// Only record configuration-cache entries for Gradle 7+
def isAtLeastGradle7 = GradleVersion.current() >= GradleVersion.version('7.0')
if (isTopLevelBuild && isAtLeastGradle7) {
apply from: 'project-root-capture.plugin.groovy'
}

View file

@ -1,40 +0,0 @@
/*
* Capture the build root directory for each executed Gradle build.
* This is used to save/restore configuration-cache files, so:
* - The implementation only makes sense if it's config-cache compatible
* - We only need to support Gradle 7+
*/
import org.gradle.tooling.events.*
settingsEvaluated { settings ->
def rootDir = settings.rootDir.absolutePath
def rootListLocation = new File(System.getenv("RUNNER_TEMP"), "project-roots.txt").absolutePath
def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-build-action-projectRootTracker", ProjectTracker, { spec ->
spec.getParameters().getRootDir().set(rootDir);
spec.getParameters().getRootListLocation().set(rootListLocation);
})
gradle.services.get(BuildEventsListenerRegistry).onTaskCompletion(projectTracker)
}
abstract class ProjectTracker implements BuildService<ProjectTracker.Params>, OperationCompletionListener, AutoCloseable {
interface Params extends BuildServiceParameters {
Property<String> getRootDir();
Property<String> getRootListLocation();
}
public void onFinish(FinishEvent finishEvent) {}
@Override
public void close() {
def rootDir = getParameters().getRootDir().get()
def rootDirEntry = rootDir + '\n'
def rootListFile = new File(getParameters().getRootListLocation().get())
if (!rootListFile.exists() || !rootListFile.text.contains(rootDirEntry)) {
rootListFile << rootDirEntry
}
}
}

View file

@ -1,77 +0,0 @@
package com.gradle.gradlebuildaction
import static org.junit.Assume.assumeTrue
class TestProjectRootCapture extends BaseInitScriptTest {
def initScript = 'project-root-capture.init.gradle'
def "captures project root on #testGradleVersion"() {
assumeTrue testGradleVersion.compatibleWithCurrentJvm
when:
run(['help'], initScript, testGradleVersion.gradleVersion)
then:
assertCapturesProjectRoot()
where:
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
}
def "captures project root on #testGradleVersion when build fails"() {
assumeTrue testGradleVersion.compatibleWithCurrentJvm
addFailingTaskToBuild()
when:
runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion)
then:
assertCapturesProjectRoot()
where:
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
}
def "captures project root on #testGradleVersion with --configuration-cache"() {
assumeTrue testGradleVersion.compatibleWithCurrentJvm
when:
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
then:
assertCapturesProjectRoot()
assert projectRootList.delete()
when:
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
then:
assertCapturesProjectRoot()
where:
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
}
def "has no effect on #testVersion"() {
assumeTrue testVersion.compatibleWithCurrentJvm
when:
run(['help'], initScript, testVersion.gradleVersion)
then:
assert !projectRootList.exists()
where:
testVersion << (ALL_VERSIONS - CONFIGURATION_CACHE_VERSIONS)
}
private void assertCapturesProjectRoot() {
assert projectRootList.exists()
assert new File(projectRootList.text.trim()).canonicalPath == testProjectDir.canonicalPath
}
private File getProjectRootList() {
new File(testProjectDir, 'project-roots.txt')
}
}