mirror of
				https://github.com/gradle/gradle-build-action.git
				synced 2025-10-31 15:18:57 +08:00 
			
		
		
		
	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:
		| @@ -9,7 +9,6 @@ import {ConfigurationCacheEntryExtractor, GradleHomeEntryExtractor} from './cach | ||||
| const RESTORED_CACHE_KEY_KEY = 'restored-cache-key' | ||||
|  | ||||
| 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 EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes' | ||||
|  | ||||
| @@ -170,12 +169,7 @@ export class GradleStateCache { | ||||
|         const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties') | ||||
|         fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false') | ||||
|  | ||||
|         const initScriptFilenames = [ | ||||
|             'build-result-capture.init.gradle', | ||||
|             'build-result-capture-service.plugin.groovy', | ||||
|             'project-root-capture.init.gradle', | ||||
|             'project-root-capture.plugin.groovy' | ||||
|         ] | ||||
|         const initScriptFilenames = ['build-result-capture.init.gradle', 'build-result-capture-service.plugin.groovy'] | ||||
|         for (const initScriptFilename of initScriptFilenames) { | ||||
|             const initScriptContent = this.readResourceAsString(initScriptFilename) | ||||
|             const initScriptPath = path.resolve(initScriptsDir, initScriptFilename) | ||||
|   | ||||
| @@ -3,7 +3,7 @@ import fs from 'fs' | ||||
| import * as core from '@actions/core' | ||||
| 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 { | ||||
|     cacheDebug, | ||||
| @@ -14,6 +14,7 @@ import { | ||||
|     saveCache, | ||||
|     tryDelete | ||||
| } from './cache-utils' | ||||
| import {loadBuildResults} from './job-summary' | ||||
|  | ||||
| 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. | ||||
|      */ | ||||
|     private getProjectRoots(): string[] { | ||||
|         const projectList = path.resolve(process.env['RUNNER_TEMP']!, PROJECT_ROOTS_FILE) | ||||
|         if (!fs.existsSync(projectList)) { | ||||
|             core.info(`Missing project list file ${projectList}`) | ||||
|             return [] | ||||
|         } | ||||
|         const projectRoots = fs.readFileSync(projectList, 'utf-8') | ||||
|         core.info(`Found project roots '${projectRoots}' in ${projectList}`) | ||||
|         return projectRoots.trim().split('\n') | ||||
|         const buildResults = loadBuildResults() | ||||
|         const projectRootDirs = buildResults.map(x => x.rootProjectDir) | ||||
|         return [...new Set(projectRootDirs)] // Remove duplicates | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -3,7 +3,7 @@ import fs from 'fs' | ||||
| import path from 'path' | ||||
| import {logCachingReport, CacheListener} from './cache-reporting' | ||||
|  | ||||
| interface BuildResult { | ||||
| export interface BuildResult { | ||||
|     get rootProjectName(): string | ||||
|     get rootProjectDir(): string | ||||
|     get requestedTasks(): string | ||||
| @@ -28,7 +28,7 @@ export function writeJobSummary(cacheListener: CacheListener): void { | ||||
|     core.summary.write() | ||||
| } | ||||
|  | ||||
| function loadBuildResults(): BuildResult[] { | ||||
| export function loadBuildResults(): BuildResult[] { | ||||
|     const buildResultsDir = path.resolve(process.env['RUNNER_TEMP']!, '.build-results') | ||||
|     if (!fs.existsSync(buildResultsDir)) { | ||||
|         return [] | ||||
|   | ||||
| @@ -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' | ||||
| } | ||||
| @@ -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 | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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') | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user