From 500607bc3528900fa24f892c6d50106d93cc8d72 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sat, 4 Jun 2022 22:37:36 -0600 Subject: [PATCH] Capture build-results without build-scan publication The `buildScanPublished` is only called when a build scan is published by the build. Support other invocations by adding a `buildFinished` listener. --- .github/workflows/demo-job-summary.yml | 3 ++ .../build-result-capture.init.gradle | 37 +++++++++++++++---- .../TestBuildResultOutput.groovy | 2 - 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.github/workflows/demo-job-summary.yml b/.github/workflows/demo-job-summary.yml index 8047a9f..facff59 100644 --- a/.github/workflows/demo-job-summary.yml +++ b/.github/workflows/demo-job-summary.yml @@ -23,6 +23,9 @@ jobs: - name: Build kotlin-dsl project working-directory: .github/workflow-samples/kotlin-dsl run: ./gradlew assemble + - name: Build kotlin-dsl project + working-directory: .github/workflow-samples/kotlin-dsl + run: ./gradlew check --no-scan - name: Build groovy-dsl project working-directory: .github/workflow-samples/groovy-dsl run: ./gradlew assemble diff --git a/src/resources/build-result-capture.init.gradle b/src/resources/build-result-capture.init.gradle index ff34b04..a6ecbb1 100644 --- a/src/resources/build-result-capture.init.gradle +++ b/src/resources/build-result-capture.init.gradle @@ -7,25 +7,31 @@ import org.gradle.util.GradleVersion def isTopLevelBuild = gradle.getParent() == null if (isTopLevelBuild) { def version = GradleVersion.current().baseVersion + def atLeastGradle3 = version >= GradleVersion.version("3.0") def atLeastGradle6 = version >= GradleVersion.version("6.0") if (atLeastGradle6) { settingsEvaluated { settings -> if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) { - registerCallbacks(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject.name) + captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject.name) + } else { + captureUsingBuildFinished(gradle) } } } else if (atLeastGradle3) { projectsEvaluated { gradle -> if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) { - registerCallbacks(gradle.rootProject.extensions["buildScan"], gradle.rootProject.name) + captureUsingBuildScanPublished(gradle.rootProject.extensions["buildScan"], gradle.rootProject.name) + } else { + captureUsingBuildFinished(gradle) } } } } -def registerCallbacks(buildScanExtension, rootProjectName) { + +def captureUsingBuildScanPublished(buildScanExtension, rootProjectName) { buildScanExtension.with { def requestedTasks = gradle.startParameter.taskNames.join(" ") def gradleVersion = GradleVersion.current().version @@ -36,10 +42,6 @@ def registerCallbacks(buildScanExtension, rootProjectName) { } buildScanPublished { buildScan -> - def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") - buildResultsDir.mkdirs() - - def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + System.currentTimeMillis() + ".json") def buildScanUri = buildScan.buildScanUri.toASCIIString() def buildResults = [ @@ -49,9 +51,30 @@ def registerCallbacks(buildScanExtension, rootProjectName) { buildFailed: buildFailed, buildScanUri: buildScanUri ] + + def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") + buildResultsDir.mkdirs() + def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + System.currentTimeMillis() + ".json") buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) println("::set-output name=build-scan-url::${buildScan.buildScanUri}") } } +} + +def captureUsingBuildFinished(gradle) { + gradle.buildFinished { result -> + def buildResults = [ + rootProject: gradle.rootProject.name, + requestedTasks: gradle.startParameter.taskNames.join(" "), + gradleVersion: GradleVersion.current().version, + buildFailed: result.failure != null, + buildScanUri: null + ] + + def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") + buildResultsDir.mkdirs() + def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + System.currentTimeMillis() + ".json") + buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) + } } \ No newline at end of file diff --git a/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultOutput.groovy b/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultOutput.groovy index d9d36a1..eab1703 100644 --- a/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultOutput.groovy +++ b/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestBuildResultOutput.groovy @@ -8,7 +8,6 @@ import static org.junit.Assume.assumeTrue class TestBuildResultOutput extends BaseInitScriptTest { def initScript = 'build-result-capture.init.gradle' - @Ignore def "produces build results file for build with #testGradleVersion"() { assumeTrue testGradleVersion.compatibleWithCurrentJvm @@ -22,7 +21,6 @@ class TestBuildResultOutput extends BaseInitScriptTest { testGradleVersion << ALL_VERSIONS } - @Ignore def "produces build results file for failing build with #testGradleVersion"() { assumeTrue testGradleVersion.compatibleWithCurrentJvm