Capture config-cache hit in build results

This commit is contained in:
daz 2024-07-18 14:52:46 -06:00
parent 94355bbb2f
commit 579a013225
No known key found for this signature in database
3 changed files with 49 additions and 24 deletions

View file

@ -1,5 +1,10 @@
import org.gradle.tooling.events.*
import org.gradle.tooling.events.task.*
import org.gradle.internal.operations.*
import org.gradle.initialization.*
import org.gradle.api.internal.tasks.execution.*
import org.gradle.execution.*
import org.gradle.internal.build.event.BuildEventListenerRegistryInternal
import org.gradle.util.GradleVersion
settingsEvaluated { settings ->
@ -11,11 +16,12 @@ settingsEvaluated { settings ->
spec.getParameters().getInvocationId().set(gradle.ext.invocationId)
})
gradle.services.get(BuildEventsListenerRegistry).onTaskCompletion(projectTracker)
gradle.services.get(BuildEventListenerRegistryInternal).onOperationCompletion(projectTracker)
}
abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder.Params>, OperationCompletionListener, AutoCloseable {
abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder.Params>, BuildOperationListener, AutoCloseable {
private boolean buildFailed = false
private boolean configCacheHit = true
interface Params extends BuildServiceParameters {
Property<String> getRootProjectName()
Property<String> getRootProjectDir()
@ -24,9 +30,19 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
Property<String> getInvocationId()
}
public void onFinish(FinishEvent finishEvent) {
if (finishEvent instanceof TaskFinishEvent && finishEvent.result instanceof TaskFailureResult) {
buildFailed = true
void started(BuildOperationDescriptor buildOperation, OperationStartEvent startEvent) {}
void progress(OperationIdentifier operationIdentifier, OperationProgressEvent progressEvent) {}
void finished(BuildOperationDescriptor buildOperation, OperationFinishEvent finishEvent) {
if (buildOperation.details in EvaluateSettingsBuildOperationType.Details) {
// Got EVALUATE SETTINGS event: not a config-cache hit"
configCacheHit = false
}
if (buildOperation.details in RunRootBuildWorkBuildOperationType.Details) {
if (finishEvent.failure != null) {
buildFailed = true
}
}
}
@ -38,7 +54,8 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
requestedTasks: getParameters().getRequestedTasks().get(),
gradleVersion: GradleVersion.current().version,
gradleHomeDir: getParameters().getGradleHomeDir().get(),
buildFailed: buildFailed
buildFailed: buildFailed,
configCacheHit: configCacheHit
]
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")

View file

@ -76,7 +76,8 @@ void captureUsingBuildFinished(gradle, String invocationId, ResultsWriter result
requestedTasks: gradle.startParameter.taskNames.join(" "),
gradleVersion: GradleVersion.current().version,
gradleHomeDir: gradle.gradleHomeDir.absolutePath,
buildFailed: result.failure != null
buildFailed: result.failure != null,
configCacheHit: false
]
resultsWriter.writeToResultsFile("build-results", invocationId, buildResults)
}

View file

@ -1,7 +1,6 @@
package com.gradle.gradlebuildaction
import groovy.json.JsonSlurper
import org.gradle.util.GradleVersion
import static org.junit.Assume.assumeTrue
@ -15,7 +14,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, false)
assertResults('help', testGradleVersion, false)
where:
testGradleVersion << ALL_VERSIONS
@ -29,7 +28,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
runAndFail(testGradleVersion.gradleVersion)
then:
assertResults('expectFailure', testGradleVersion, true, false)
assertResults('expectFailure', testGradleVersion, true)
where:
testGradleVersion << ALL_VERSIONS
@ -49,7 +48,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, false)
assertResults('help', testGradleVersion, false, true)
where:
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
@ -63,7 +62,8 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, true)
assertResults('help', testGradleVersion, false)
assertScanResults()
where:
testGradleVersion << ALL_VERSIONS
@ -77,7 +77,8 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, true)
assertResults('help', testGradleVersion, false)
assertScanResults()
where:
testGradleVersion << ALL_VERSIONS
@ -91,7 +92,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(['help', '--no-scan'], testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, false)
assertResults('help', testGradleVersion, false)
where:
testGradleVersion << ALL_VERSIONS
@ -106,7 +107,8 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
runAndFail(testGradleVersion.gradleVersion)
then:
assertResults('expectFailure', testGradleVersion, true, true)
assertResults('expectFailure', testGradleVersion, true)
assertScanResults()
where:
testGradleVersion << ALL_VERSIONS
@ -120,7 +122,8 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, true)
assertResults('help', testGradleVersion, false, false)
assertScanResults()
assert buildResultFile.delete()
assert scanResultFile.delete()
@ -129,6 +132,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
then:
assertResults('help', testGradleVersion, false, true)
assertScanResults()
where:
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
@ -144,7 +148,8 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
runAndFail(testGradleVersion.gradleVersion)
then:
assertResults('expectFailure', testGradleVersion, true, false, true)
assertResults('expectFailure', testGradleVersion, true)
assertScanResults(true)
where:
testGradleVersion << ALL_VERSIONS
@ -205,7 +210,8 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
run(testGradleVersion.gradleVersion)
then:
assertResults('help', testGradleVersion, false, true)
assertResults('help', testGradleVersion, false)
assertScanResults()
where:
testGradleVersion << SETTINGS_PLUGIN_VERSIONS
@ -233,7 +239,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
]
}
void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan, boolean scanUploadFailed = false) {
void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean configCacheHit = false) {
def results = new JsonSlurper().parse(buildResultFile)
assert results['rootProjectName'] == ROOT_PROJECT_NAME
assert results['rootProjectDir'] == testProjectDir.canonicalPath
@ -241,12 +247,13 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
assert results['gradleVersion'] == testGradleVersion.gradleVersion.version
assert results['gradleHomeDir'] != null
assert results['buildFailed'] == hasFailure
assert results['configCacheHit'] == configCacheHit
}
if (hasBuildScan || scanUploadFailed) {
def scanResults = new JsonSlurper().parse(scanResultFile)
assert scanResults['buildScanUri'] == (hasBuildScan ? "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}" : null)
assert scanResults['buildScanFailed'] == scanUploadFailed
}
void assertScanResults(boolean scanUploadFailed = false) {
def scanResults = new JsonSlurper().parse(scanResultFile)
assert scanResults['buildScanUri'] == (scanUploadFailed ? null : "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}")
assert scanResults['buildScanFailed'] == scanUploadFailed
}
private File getBuildResultFile() {