From df4c1902a6ed340598fc93eac89d731a192e0248 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Wed, 15 Jun 2022 08:29:55 -0600 Subject: [PATCH] Extract common functionality for recording build results --- .../build-result-capture.init.gradle | 124 ++++++++---------- 1 file changed, 56 insertions(+), 68 deletions(-) diff --git a/src/resources/init-scripts/build-result-capture.init.gradle b/src/resources/init-scripts/build-result-capture.init.gradle index c620607..c7c9013 100644 --- a/src/resources/init-scripts/build-result-capture.init.gradle +++ b/src/resources/init-scripts/build-result-capture.init.gradle @@ -40,91 +40,33 @@ if (isTopLevelBuild) { 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 + def buildResults = new BuildResults(invocationId, gradle, rootProject) buildFinished { result -> - buildFailed = (result.failure != null) + buildResults.setBuildResult(result) } buildScanPublished { buildScan -> - - def buildScanUri = buildScan.buildScanUri.toASCIIString() - def buildResults = [ - rootProjectName: rootProjectName, - rootProjectDir: rootProjectDir, - requestedTasks: requestedTasks, - gradleVersion: gradleVersion, - gradleHomeDir: gradleHomeDir, - buildFailed: buildFailed, - buildScanUri: buildScanUri, - buildScanFailed: false - ] - - def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") - buildResultsDir.mkdirs() - def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json") - - // Overwrite any contents written by buildFinished or build service, since this result is a superset. - if (buildResultsFile.exists()) { - buildResultsFile.text = groovy.json.JsonOutput.toJson(buildResults) - } else { - buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) - } + buildResults.setBuildScanUri(buildScan.buildScanUri.toASCIIString()) + buildResults.writeToResultsFile(true) println("::set-output name=build-scan-url::${buildScan.buildScanUri}") } onError { error -> - def buildResults = [ - rootProjectName: rootProjectName, - rootProjectDir: rootProjectDir, - requestedTasks: requestedTasks, - gradleVersion: gradleVersion, - gradleHomeDir: gradleHomeDir, - buildFailed: buildFailed, - buildScanUri: null, - buildScanFailed: true - ] - - def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") - buildResultsDir.mkdirs() - def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json") - - // Overwrite any contents written by buildFinished or build service, since this result is a superset. - if (buildResultsFile.exists()) { - buildResultsFile.text = groovy.json.JsonOutput.toJson(buildResults) - } else { - buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) - } + buildResults.setBuildScanFailed() + buildResults.writeToResultsFile(true) } } } def captureUsingBuildFinished(gradle, invocationId) { gradle.buildFinished { result -> - def buildResults = [ - 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, - buildScanFailed: false - ] + def buildResults = new BuildResults(invocationId, gradle, gradle.rootProject) + buildResults.setBuildResult(result) + + buildResults.writeToResultsFile(false) - def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") - buildResultsDir.mkdirs() - def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json") - // Don't overwrite file generated by build-scan plugin if present (which has build-scan-uri) - if (!buildResultsFile.exists()) { - buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) - } } } @@ -132,3 +74,49 @@ def captureUsingBuildService(settings, invocationId) { gradle.ext.invocationId = invocationId apply from: 'build-result-capture-service.plugin.groovy' } + +class BuildResults { + def invocationId + def buildResults + + BuildResults(String invocationId, def gradle, def rootProject) { + this.invocationId = invocationId + buildResults = [ + rootProjectName: rootProject.name, + rootProjectDir: rootProject.projectDir.absolutePath, + requestedTasks: gradle.startParameter.taskNames.join(" "), + gradleVersion: GradleVersion.current().version, + gradleHomeDir: gradle.gradleHomeDir.absolutePath, + buildFailed: false, + buildScanUri: null, + buildScanFailed: false + ] + } + + def setBuildResult(def result) { + buildResults['buildFailed'] = result.failure != null + } + + def setBuildScanUri(def buildScanUrl) { + buildResults['buildScanUri'] = buildScanUrl + } + + def setBuildScanFailed() { + buildResults['buildScanFailed'] = true + } + + def writeToResultsFile(boolean overwrite) { + def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") + buildResultsDir.mkdirs() + def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json") + + // Overwrite any contents written by buildFinished or build service, since this result is a superset. + if (buildResultsFile.exists()) { + if (overwrite) { + buildResultsFile.text = groovy.json.JsonOutput.toJson(buildResults) + } + } else { + buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) + } + } +}