Add more information to captured build results

- Root project dir: will allow us to replace project-root-capture init script
- Gradle home dir: will allow us to stop all started daemons
This commit is contained in:
Daz DeBoer 2022-06-05 08:18:25 -06:00
parent 44e1837ae3
commit e234151ec9
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D
4 changed files with 37 additions and 20 deletions

View File

@ -4,9 +4,11 @@ import path from 'path'
import {logCachingReport, CacheListener} from './cache-reporting'
interface BuildResult {
get rootProject(): string
get rootProjectName(): string
get rootProjectDir(): string
get requestedTasks(): string
get gradleVersion(): string
get gradleHomeDir(): string
get buildFailed(): boolean
get buildScanUri(): string
}
@ -50,7 +52,7 @@ function writeSummaryTable(results: BuildResult[]): void {
{data: 'Outcome', header: true}
],
...results.map(result => [
result.rootProject,
result.rootProjectName,
result.requestedTasks,
result.gradleVersion,
renderOutcome(result)

View File

@ -6,8 +6,10 @@ import org.gradle.util.GradleVersion
// But projectsEvaluated is good enough, since the build service won't catch configuration failures anyway
projectsEvaluated {
def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-build-action-buildResultsRecorder", BuildResultsRecorder, { spec ->
spec.getParameters().getRootProject().set(gradle.rootProject.name)
spec.getParameters().getRootProjectName().set(gradle.rootProject.name)
spec.getParameters().getRootProjectDir().set(gradle.rootProject.rootDir.absolutePath)
spec.getParameters().getRequestedTasks().set(gradle.startParameter.taskNames.join(" "))
spec.getParameters().getGradleHomeDir().set(gradle.gradleHomeDir.absolutePath)
spec.getParameters().getInvocationId().set(gradle.ext.invocationId)
})
@ -17,8 +19,10 @@ projectsEvaluated {
abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder.Params>, OperationCompletionListener, AutoCloseable {
private boolean buildFailed = false
interface Params extends BuildServiceParameters {
Property<String> getRootProject()
Property<String> getRootProjectName()
Property<String> getRootProjectDir()
Property<String> getRequestedTasks()
Property<String> getGradleHomeDir()
Property<String> getInvocationId()
}
@ -31,9 +35,11 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
@Override
public void close() {
def buildResults = [
rootProject: getParameters().getRootProject().get(),
rootProjectName: getParameters().getRootProjectName().get(),
rootProjectDir: getParameters().getRootProjectDir().get(),
requestedTasks: getParameters().getRequestedTasks().get(),
gradleVersion: GradleVersion.current().version,
gradleHomeDir: getParameters().getGradleHomeDir().get(),
buildFailed: buildFailed,
buildScanUri: null
]

View File

@ -18,7 +18,7 @@ if (isTopLevelBuild) {
settingsEvaluated { settings ->
// The `buildScanPublished` hook is the only way to capture the build scan URI.
if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) {
captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject.name, invocationId)
captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject, invocationId)
}
// We also need to add hooks in case the plugin is applied but no build scan is published
if (useBuildService) {
@ -30,7 +30,7 @@ if (isTopLevelBuild) {
} else if (atLeastGradle3) {
projectsEvaluated { gradle ->
if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) {
captureUsingBuildScanPublished(gradle.rootProject.extensions["buildScan"], gradle.rootProject.name, invocationId)
captureUsingBuildScanPublished(gradle.rootProject.extensions["buildScan"], gradle.rootProject, invocationId)
}
// We need to capture in buildFinished in case the plugin is applied but no build scan is published
captureUsingBuildFinished(gradle, invocationId)
@ -38,10 +38,13 @@ if (isTopLevelBuild) {
}
}
def captureUsingBuildScanPublished(buildScanExtension, rootProjectName, invocationId) {
def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) {
buildScanExtension.with {
def requestedTasks = gradle.startParameter.taskNames.join(" ")
def rootProjectName = rootProject.name
def rootProjectDir = rootProject.projectDir.absolutePath
def gradleVersion = GradleVersion.current().version
def gradleHomeDir = gradle.gradleHomeDir.absolutePath
def buildFailed = false
buildFinished { result ->
@ -52,9 +55,11 @@ def captureUsingBuildScanPublished(buildScanExtension, rootProjectName, invocati
def buildScanUri = buildScan.buildScanUri.toASCIIString()
def buildResults = [
rootProject: rootProjectName,
rootProjectName: rootProjectName,
rootProjectDir: rootProjectDir,
requestedTasks: requestedTasks,
gradleVersion: gradleVersion,
gradleHomeDir: gradleHomeDir,
buildFailed: buildFailed,
buildScanUri: buildScanUri
]
@ -78,9 +83,11 @@ def captureUsingBuildScanPublished(buildScanExtension, rootProjectName, invocati
def captureUsingBuildFinished(gradle, invocationId) {
gradle.buildFinished { result ->
def buildResults = [
rootProject: gradle.rootProject.name,
rootProjectName: gradle.rootProject.name,
rootProjectDir: gradle.rootProject.rootDir.absolutePath,
requestedTasks: gradle.startParameter.taskNames.join(" "),
gradleVersion: GradleVersion.current().version,
gradleHomeDir: gradle.gradleHomeDir.absolutePath,
buildFailed: result.failure != null,
buildScanUri: null
]

View File

@ -120,9 +120,11 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan) {
def results = new JsonSlurper().parse(buildResultFile)
assert results['rootProject'] == ROOT_PROJECT_NAME
assert results['rootProjectName'] == ROOT_PROJECT_NAME
assert results['rootProjectDir'] == testProjectDir.canonicalPath
assert results['requestedTasks'] == task
assert results['gradleVersion'] == testGradleVersion.gradleVersion.version
assert results['gradleHomeDir'] != null
assert results['buildFailed'] == hasFailure
assert results['buildScanUri'] == (hasBuildScan ? "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}" : null)
}