Extract common functionality for recording build results

This commit is contained in:
Daz DeBoer 2022-06-15 08:29:55 -06:00
parent 132237ba05
commit df4c1902a6
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D

View File

@ -40,91 +40,33 @@ if (isTopLevelBuild) {
def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) { def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) {
buildScanExtension.with { buildScanExtension.with {
def requestedTasks = gradle.startParameter.taskNames.join(" ") def buildResults = new BuildResults(invocationId, gradle, rootProject)
def rootProjectName = rootProject.name
def rootProjectDir = rootProject.projectDir.absolutePath
def gradleVersion = GradleVersion.current().version
def gradleHomeDir = gradle.gradleHomeDir.absolutePath
def buildFailed = false
buildFinished { result -> buildFinished { result ->
buildFailed = (result.failure != null) buildResults.setBuildResult(result)
} }
buildScanPublished { buildScan -> buildScanPublished { buildScan ->
buildResults.setBuildScanUri(buildScan.buildScanUri.toASCIIString())
def buildScanUri = buildScan.buildScanUri.toASCIIString() buildResults.writeToResultsFile(true)
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)
}
println("::set-output name=build-scan-url::${buildScan.buildScanUri}") println("::set-output name=build-scan-url::${buildScan.buildScanUri}")
} }
onError { error -> onError { error ->
def buildResults = [ buildResults.setBuildScanFailed()
rootProjectName: rootProjectName, buildResults.writeToResultsFile(true)
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)
}
} }
} }
} }
def captureUsingBuildFinished(gradle, invocationId) { def captureUsingBuildFinished(gradle, invocationId) {
gradle.buildFinished { result -> gradle.buildFinished { result ->
def buildResults = [ def buildResults = new BuildResults(invocationId, gradle, gradle.rootProject)
rootProjectName: gradle.rootProject.name, buildResults.setBuildResult(result)
rootProjectDir: gradle.rootProject.rootDir.absolutePath,
requestedTasks: gradle.startParameter.taskNames.join(" "), buildResults.writeToResultsFile(false)
gradleVersion: GradleVersion.current().version,
gradleHomeDir: gradle.gradleHomeDir.absolutePath,
buildFailed: result.failure != null,
buildScanUri: null,
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")
// 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 gradle.ext.invocationId = invocationId
apply from: 'build-result-capture-service.plugin.groovy' 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)
}
}
}