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.
This commit is contained in:
Daz DeBoer 2022-06-04 22:37:36 -06:00
parent 754892d4ae
commit 500607bc35
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
3 changed files with 33 additions and 9 deletions

View file

@ -23,6 +23,9 @@ jobs:
- name: Build kotlin-dsl project - name: Build kotlin-dsl project
working-directory: .github/workflow-samples/kotlin-dsl working-directory: .github/workflow-samples/kotlin-dsl
run: ./gradlew assemble 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 - name: Build groovy-dsl project
working-directory: .github/workflow-samples/groovy-dsl working-directory: .github/workflow-samples/groovy-dsl
run: ./gradlew assemble run: ./gradlew assemble

View file

@ -7,25 +7,31 @@ import org.gradle.util.GradleVersion
def isTopLevelBuild = gradle.getParent() == null def isTopLevelBuild = gradle.getParent() == null
if (isTopLevelBuild) { if (isTopLevelBuild) {
def version = GradleVersion.current().baseVersion def version = GradleVersion.current().baseVersion
def atLeastGradle3 = version >= GradleVersion.version("3.0") def atLeastGradle3 = version >= GradleVersion.version("3.0")
def atLeastGradle6 = version >= GradleVersion.version("6.0") def atLeastGradle6 = version >= GradleVersion.version("6.0")
if (atLeastGradle6) { if (atLeastGradle6) {
settingsEvaluated { settings -> settingsEvaluated { settings ->
if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) { 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) { } 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")) {
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 { buildScanExtension.with {
def requestedTasks = gradle.startParameter.taskNames.join(" ") def requestedTasks = gradle.startParameter.taskNames.join(" ")
def gradleVersion = GradleVersion.current().version def gradleVersion = GradleVersion.current().version
@ -36,10 +42,6 @@ def registerCallbacks(buildScanExtension, rootProjectName) {
} }
buildScanPublished { buildScan -> 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 buildScanUri = buildScan.buildScanUri.toASCIIString()
def buildResults = [ def buildResults = [
@ -49,9 +51,30 @@ def registerCallbacks(buildScanExtension, rootProjectName) {
buildFailed: buildFailed, buildFailed: buildFailed,
buildScanUri: buildScanUri 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) buildResultsFile << groovy.json.JsonOutput.toJson(buildResults)
println("::set-output name=build-scan-url::${buildScan.buildScanUri}") 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)
}
} }

View file

@ -8,7 +8,6 @@ import static org.junit.Assume.assumeTrue
class TestBuildResultOutput extends BaseInitScriptTest { class TestBuildResultOutput extends BaseInitScriptTest {
def initScript = 'build-result-capture.init.gradle' def initScript = 'build-result-capture.init.gradle'
@Ignore
def "produces build results file for build with #testGradleVersion"() { def "produces build results file for build with #testGradleVersion"() {
assumeTrue testGradleVersion.compatibleWithCurrentJvm assumeTrue testGradleVersion.compatibleWithCurrentJvm
@ -22,7 +21,6 @@ class TestBuildResultOutput extends BaseInitScriptTest {
testGradleVersion << ALL_VERSIONS testGradleVersion << ALL_VERSIONS
} }
@Ignore
def "produces build results file for failing build with #testGradleVersion"() { def "produces build results file for failing build with #testGradleVersion"() {
assumeTrue testGradleVersion.compatibleWithCurrentJvm assumeTrue testGradleVersion.compatibleWithCurrentJvm