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' import {logCachingReport, CacheListener} from './cache-reporting'
interface BuildResult { interface BuildResult {
get rootProject(): string get rootProjectName(): string
get rootProjectDir(): string
get requestedTasks(): string get requestedTasks(): string
get gradleVersion(): string get gradleVersion(): string
get gradleHomeDir(): string
get buildFailed(): boolean get buildFailed(): boolean
get buildScanUri(): string get buildScanUri(): string
} }
@ -50,7 +52,7 @@ function writeSummaryTable(results: BuildResult[]): void {
{data: 'Outcome', header: true} {data: 'Outcome', header: true}
], ],
...results.map(result => [ ...results.map(result => [
result.rootProject, result.rootProjectName,
result.requestedTasks, result.requestedTasks,
result.gradleVersion, result.gradleVersion,
renderOutcome(result) 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 // But projectsEvaluated is good enough, since the build service won't catch configuration failures anyway
projectsEvaluated { projectsEvaluated {
def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-build-action-buildResultsRecorder", BuildResultsRecorder, { spec -> 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().getRequestedTasks().set(gradle.startParameter.taskNames.join(" "))
spec.getParameters().getGradleHomeDir().set(gradle.gradleHomeDir.absolutePath)
spec.getParameters().getInvocationId().set(gradle.ext.invocationId) spec.getParameters().getInvocationId().set(gradle.ext.invocationId)
}) })
@ -17,8 +19,10 @@ projectsEvaluated {
abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder.Params>, OperationCompletionListener, AutoCloseable { abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder.Params>, OperationCompletionListener, AutoCloseable {
private boolean buildFailed = false private boolean buildFailed = false
interface Params extends BuildServiceParameters { interface Params extends BuildServiceParameters {
Property<String> getRootProject() Property<String> getRootProjectName()
Property<String> getRootProjectDir()
Property<String> getRequestedTasks() Property<String> getRequestedTasks()
Property<String> getGradleHomeDir()
Property<String> getInvocationId() Property<String> getInvocationId()
} }
@ -31,9 +35,11 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
@Override @Override
public void close() { public void close() {
def buildResults = [ def buildResults = [
rootProject: getParameters().getRootProject().get(), rootProjectName: getParameters().getRootProjectName().get(),
rootProjectDir: getParameters().getRootProjectDir().get(),
requestedTasks: getParameters().getRequestedTasks().get(), requestedTasks: getParameters().getRequestedTasks().get(),
gradleVersion: GradleVersion.current().version, gradleVersion: GradleVersion.current().version,
gradleHomeDir: getParameters().getGradleHomeDir().get(),
buildFailed: buildFailed, buildFailed: buildFailed,
buildScanUri: null buildScanUri: null
] ]

View file

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

View file

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