From e4f356955fdb91f295aeaa8cb851ad0a07d0b739 Mon Sep 17 00:00:00 2001 From: daz Date: Mon, 20 Jan 2025 15:13:52 -0700 Subject: [PATCH] Avoid capturing build-results for cache cleanup The Gradle build used to perform cache-cleanup will run in the context of init-scripts provided by the action, including those that collect build-results. In some circumstances this can lead to unexpected results, such as saving configuration-cache entries for cache cleanup executions. With this change, build results will not be captured for cache-cleanup builds. --- sources/src/caching/cache-cleaner.ts | 1 + ...actions.build-result-capture-service.plugin.groovy | 3 +++ .../gradle-actions.build-result-capture.init.gradle | 11 ++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/sources/src/caching/cache-cleaner.ts b/sources/src/caching/cache-cleaner.ts index d8e0b237..e050c3b7 100644 --- a/sources/src/caching/cache-cleaner.ts +++ b/sources/src/caching/cache-cleaner.ts @@ -75,6 +75,7 @@ export class CacheCleaner { '--no-scan', '--build-cache', '-DGITHUB_DEPENDENCY_GRAPH_ENABLED=false', + '-DGRADLE_ACTIONS_SKIP_BUILD_RESULT_CAPTURE=true', 'noop' ] diff --git a/sources/src/resources/init-scripts/gradle-actions.build-result-capture-service.plugin.groovy b/sources/src/resources/init-scripts/gradle-actions.build-result-capture-service.plugin.groovy index c2d938b2..b30c117e 100644 --- a/sources/src/resources/init-scripts/gradle-actions.build-result-capture-service.plugin.groovy +++ b/sources/src/resources/init-scripts/gradle-actions.build-result-capture-service.plugin.groovy @@ -6,6 +6,7 @@ import org.gradle.api.internal.tasks.execution.* import org.gradle.execution.* import org.gradle.internal.build.event.BuildEventListenerRegistryInternal import org.gradle.util.GradleVersion +import org.slf4j.LoggerFactory settingsEvaluated { settings -> def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-action-buildResultsRecorder", BuildResultsRecorder, { spec -> @@ -20,6 +21,7 @@ settingsEvaluated { settings -> } abstract class BuildResultsRecorder implements BuildService, BuildOperationListener, AutoCloseable { + private final logger = LoggerFactory.getLogger("gradle/actions") private boolean buildFailed = false private boolean configCacheHit = true interface Params extends BuildServiceParameters { @@ -69,6 +71,7 @@ abstract class BuildResultsRecorder implements BuildService - println "Got buildFinished: ${result}" def buildResults = [ rootProjectName: rootProject.name, rootProjectDir: rootProject.projectDir.absolutePath, @@ -124,6 +130,8 @@ void captureUsingBuildScanPublished(buildScanExtension, String invocationId, Res } class ResultsWriter { + private final logger = LoggerFactory.getLogger("gradle/actions") + void writeToResultsFile(String subDir, String invocationId, def content) { def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP") def githubActionStep = System.getProperty("GITHUB_ACTION") ?: System.getenv("GITHUB_ACTION") @@ -136,6 +144,7 @@ class ResultsWriter { buildResultsDir.mkdirs() def buildResultsFile = new File(buildResultsDir, githubActionStep + invocationId + ".json") if (!buildResultsFile.exists()) { + logger.lifecycle("gradle/actions: Writing build results to ${buildResultsFile}") buildResultsFile << groovy.json.JsonOutput.toJson(content) } } catch (Exception e) {