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) {
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)
}
}
}