2022-06-02 11:53:33 -06:00
|
|
|
/*
|
|
|
|
* Capture information for each executed Gradle build to display in the job summary.
|
|
|
|
*/
|
|
|
|
import org.gradle.util.GradleVersion
|
|
|
|
|
|
|
|
// Only run against root build. Do not run against included builds.
|
|
|
|
def isTopLevelBuild = gradle.getParent() == null
|
|
|
|
if (isTopLevelBuild) {
|
|
|
|
def version = GradleVersion.current().baseVersion
|
2022-06-04 22:37:36 -06:00
|
|
|
|
2022-06-04 22:07:28 -06:00
|
|
|
def atLeastGradle3 = version >= GradleVersion.version("3.0")
|
2022-06-02 11:53:33 -06:00
|
|
|
def atLeastGradle6 = version >= GradleVersion.version("6.0")
|
|
|
|
|
2022-06-05 00:18:20 -06:00
|
|
|
def invocationId = "-${System.currentTimeMillis()}"
|
|
|
|
|
2022-06-02 11:53:33 -06:00
|
|
|
if (atLeastGradle6) {
|
2022-06-04 22:53:36 -06:00
|
|
|
def useBuildService = version >= GradleVersion.version("6.6")
|
2022-06-02 11:53:33 -06:00
|
|
|
settingsEvaluated { settings ->
|
2022-06-05 00:18:20 -06:00
|
|
|
// The `buildScanPublished` hook is the only way to capture the build scan URI.
|
2022-06-02 11:53:33 -06:00
|
|
|
if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) {
|
2022-06-05 08:18:25 -06:00
|
|
|
captureUsingBuildScanPublished(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject, invocationId)
|
2022-06-05 00:18:20 -06:00
|
|
|
}
|
2022-06-05 08:18:25 -06:00
|
|
|
// We also need to add hooks in case the plugin is applied but no build scan is published
|
2022-06-05 00:18:20 -06:00
|
|
|
if (useBuildService) {
|
|
|
|
captureUsingBuildService(settings, invocationId)
|
2022-06-04 22:37:36 -06:00
|
|
|
} else {
|
2022-06-05 00:18:20 -06:00
|
|
|
captureUsingBuildFinished(gradle, invocationId)
|
2022-06-02 11:53:33 -06:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 22:07:28 -06:00
|
|
|
} else if (atLeastGradle3) {
|
2022-06-02 11:53:33 -06:00
|
|
|
projectsEvaluated { gradle ->
|
|
|
|
if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) {
|
2022-06-05 08:18:25 -06:00
|
|
|
captureUsingBuildScanPublished(gradle.rootProject.extensions["buildScan"], gradle.rootProject, invocationId)
|
2022-06-02 11:53:33 -06:00
|
|
|
}
|
2022-06-05 08:18:25 -06:00
|
|
|
// We need to capture in buildFinished in case the plugin is applied but no build scan is published
|
2022-06-05 00:18:20 -06:00
|
|
|
captureUsingBuildFinished(gradle, invocationId)
|
2022-06-02 11:53:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 08:18:25 -06:00
|
|
|
def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) {
|
2022-06-02 11:53:33 -06:00
|
|
|
buildScanExtension.with {
|
2022-06-03 07:12:23 -06:00
|
|
|
def requestedTasks = gradle.startParameter.taskNames.join(" ")
|
2022-06-05 08:18:25 -06:00
|
|
|
def rootProjectName = rootProject.name
|
|
|
|
def rootProjectDir = rootProject.projectDir.absolutePath
|
2022-06-03 07:12:23 -06:00
|
|
|
def gradleVersion = GradleVersion.current().version
|
2022-06-05 08:18:25 -06:00
|
|
|
def gradleHomeDir = gradle.gradleHomeDir.absolutePath
|
2022-06-02 11:53:33 -06:00
|
|
|
def buildFailed = false
|
2022-06-03 07:12:23 -06:00
|
|
|
|
2022-06-02 11:53:33 -06:00
|
|
|
buildFinished { result ->
|
|
|
|
buildFailed = (result.failure != null)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildScanPublished { buildScan ->
|
2022-06-02 12:57:35 -06:00
|
|
|
|
|
|
|
def buildScanUri = buildScan.buildScanUri.toASCIIString()
|
|
|
|
def buildResults = [
|
2022-06-05 08:18:25 -06:00
|
|
|
rootProjectName: rootProjectName,
|
|
|
|
rootProjectDir: rootProjectDir,
|
2022-06-02 12:57:35 -06:00
|
|
|
requestedTasks: requestedTasks,
|
2022-06-05 08:18:25 -06:00
|
|
|
gradleVersion: gradleVersion,
|
|
|
|
gradleHomeDir: gradleHomeDir,
|
|
|
|
buildFailed: buildFailed,
|
2022-06-15 08:08:41 -06:00
|
|
|
buildScanUri: buildScanUri,
|
|
|
|
buildScanFailed: false
|
2022-06-02 12:57:35 -06:00
|
|
|
]
|
2022-06-04 22:37:36 -06:00
|
|
|
|
|
|
|
def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results")
|
|
|
|
buildResultsDir.mkdirs()
|
2022-06-05 00:18:20 -06:00
|
|
|
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()) {
|
2022-06-05 08:18:25 -06:00
|
|
|
buildResultsFile.text = groovy.json.JsonOutput.toJson(buildResults)
|
2022-06-05 00:18:20 -06:00
|
|
|
} else {
|
|
|
|
buildResultsFile << groovy.json.JsonOutput.toJson(buildResults)
|
|
|
|
}
|
2022-06-02 11:53:33 -06:00
|
|
|
|
|
|
|
println("::set-output name=build-scan-url::${buildScan.buildScanUri}")
|
|
|
|
}
|
2022-06-15 08:08:41 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 11:53:33 -06:00
|
|
|
}
|
2022-06-04 22:37:36 -06:00
|
|
|
}
|
|
|
|
|
2022-06-05 00:18:20 -06:00
|
|
|
def captureUsingBuildFinished(gradle, invocationId) {
|
2022-06-04 22:37:36 -06:00
|
|
|
gradle.buildFinished { result ->
|
|
|
|
def buildResults = [
|
2022-06-05 08:18:25 -06:00
|
|
|
rootProjectName: gradle.rootProject.name,
|
2022-06-05 21:55:11 -06:00
|
|
|
rootProjectDir: gradle.rootProject.rootDir.absolutePath,
|
2022-06-04 22:37:36 -06:00
|
|
|
requestedTasks: gradle.startParameter.taskNames.join(" "),
|
2022-06-05 08:18:25 -06:00
|
|
|
gradleVersion: GradleVersion.current().version,
|
|
|
|
gradleHomeDir: gradle.gradleHomeDir.absolutePath,
|
2022-06-04 22:37:36 -06:00
|
|
|
buildFailed: result.failure != null,
|
2022-06-15 08:08:41 -06:00
|
|
|
buildScanUri: null,
|
|
|
|
buildScanFailed: false
|
2022-06-04 22:37:36 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results")
|
|
|
|
buildResultsDir.mkdirs()
|
2022-06-05 00:18:20 -06:00
|
|
|
def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json")
|
2022-06-05 21:55:11 -06:00
|
|
|
// 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)
|
|
|
|
}
|
2022-06-04 22:37:36 -06:00
|
|
|
}
|
2022-06-04 22:53:36 -06:00
|
|
|
}
|
|
|
|
|
2022-06-05 00:18:20 -06:00
|
|
|
def captureUsingBuildService(settings, invocationId) {
|
|
|
|
gradle.ext.invocationId = invocationId
|
2022-06-04 22:53:36 -06:00
|
|
|
apply from: 'build-result-capture-service.plugin.groovy'
|
|
|
|
}
|