mirror of
https://github.com/gradle/actions.git
synced 2025-08-26 20:01:28 +08:00
Split build-results and build-scan capture into separate files
This commit is contained in:
parent
6d20c16462
commit
94355bbb2f
@ -38,9 +38,7 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
|
|||||||
requestedTasks: getParameters().getRequestedTasks().get(),
|
requestedTasks: getParameters().getRequestedTasks().get(),
|
||||||
gradleVersion: GradleVersion.current().version,
|
gradleVersion: GradleVersion.current().version,
|
||||||
gradleHomeDir: getParameters().getGradleHomeDir().get(),
|
gradleHomeDir: getParameters().getGradleHomeDir().get(),
|
||||||
buildFailed: buildFailed,
|
buildFailed: buildFailed
|
||||||
buildScanUri: null,
|
|
||||||
buildScanFailed: false
|
|
||||||
]
|
]
|
||||||
|
|
||||||
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")
|
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")
|
||||||
@ -50,7 +48,7 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
def buildResultsDir = new File(runnerTempDir, ".build-results")
|
def buildResultsDir = new File(runnerTempDir, ".gradle-actions/build-results")
|
||||||
buildResultsDir.mkdirs()
|
buildResultsDir.mkdirs()
|
||||||
def buildResultsFile = new File(buildResultsDir, githubActionStep + getParameters().getInvocationId().get() + ".json")
|
def buildResultsFile = new File(buildResultsDir, githubActionStep + getParameters().getInvocationId().get() + ".json")
|
||||||
if (!buildResultsFile.exists()) {
|
if (!buildResultsFile.exists()) {
|
||||||
|
@ -13,6 +13,7 @@ def GE_EXTENSION = "gradleEnterprise"
|
|||||||
// Only run against root build. Do not run against included builds.
|
// Only run against root build. Do not run against included builds.
|
||||||
def isTopLevelBuild = gradle.getParent() == null
|
def isTopLevelBuild = gradle.getParent() == null
|
||||||
if (isTopLevelBuild) {
|
if (isTopLevelBuild) {
|
||||||
|
def resultsWriter = new ResultsWriter()
|
||||||
def version = GradleVersion.current().baseVersion
|
def version = GradleVersion.current().baseVersion
|
||||||
|
|
||||||
def atLeastGradle3 = version >= GradleVersion.version("3.0")
|
def atLeastGradle3 = version >= GradleVersion.version("3.0")
|
||||||
@ -26,7 +27,7 @@ if (isTopLevelBuild) {
|
|||||||
if (useBuildService) {
|
if (useBuildService) {
|
||||||
captureUsingBuildService(invocationId)
|
captureUsingBuildService(invocationId)
|
||||||
} else {
|
} else {
|
||||||
captureUsingBuildFinished(gradle, invocationId)
|
captureUsingBuildFinished(gradle, invocationId, resultsWriter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the Develocity plugin to also capture build scan links, when available
|
// Use the Develocity plugin to also capture build scan links, when available
|
||||||
@ -34,46 +35,63 @@ if (isTopLevelBuild) {
|
|||||||
settings.pluginManager.withPlugin(GE_PLUGIN_ID) {
|
settings.pluginManager.withPlugin(GE_PLUGIN_ID) {
|
||||||
// Only execute if develocity plugin isn't applied.
|
// Only execute if develocity plugin isn't applied.
|
||||||
if (!settings.extensions.findByName(DEVELOCITY_EXTENSION)) {
|
if (!settings.extensions.findByName(DEVELOCITY_EXTENSION)) {
|
||||||
captureUsingBuildScanPublished(settings.extensions[GE_EXTENSION].buildScan, settings.rootProject, invocationId)
|
captureUsingBuildScanPublished(settings.extensions[GE_EXTENSION].buildScan, invocationId, resultsWriter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
|
settings.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
|
||||||
captureUsingBuildScanPublished(settings.extensions[DEVELOCITY_EXTENSION].buildScan, settings.rootProject, invocationId)
|
captureUsingBuildScanPublished(settings.extensions[DEVELOCITY_EXTENSION].buildScan, invocationId, resultsWriter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (atLeastGradle3) {
|
} else if (atLeastGradle3) {
|
||||||
projectsEvaluated { gradle ->
|
projectsEvaluated { gradle ->
|
||||||
// By default, use 'buildFinished' to capture build results
|
// By default, use 'buildFinished' to capture build results
|
||||||
captureUsingBuildFinished(gradle, invocationId)
|
captureUsingBuildFinished(gradle, invocationId, resultsWriter)
|
||||||
|
|
||||||
gradle.rootProject.pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
|
gradle.rootProject.pluginManager.withPlugin(BUILD_SCAN_PLUGIN_ID) {
|
||||||
// Only execute if develocity plugin isn't applied.
|
// Only execute if develocity plugin isn't applied.
|
||||||
if (!gradle.rootProject.extensions.findByName(DEVELOCITY_EXTENSION)) {
|
if (!gradle.rootProject.extensions.findByName(DEVELOCITY_EXTENSION)) {
|
||||||
captureUsingBuildScanPublished(gradle.rootProject.extensions[BUILD_SCAN_EXTENSION], gradle.rootProject, invocationId)
|
captureUsingBuildScanPublished(gradle.rootProject.extensions[BUILD_SCAN_EXTENSION], invocationId, resultsWriter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gradle.rootProject.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
|
gradle.rootProject.pluginManager.withPlugin(DEVELOCITY_PLUGIN_ID) {
|
||||||
captureUsingBuildScanPublished(gradle.rootProject.extensions[DEVELOCITY_EXTENSION].buildScan, gradle.rootProject, invocationId)
|
captureUsingBuildScanPublished(gradle.rootProject.extensions[DEVELOCITY_EXTENSION].buildScan, invocationId, resultsWriter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def captureUsingBuildService(invocationId) {
|
||||||
|
gradle.ext.invocationId = invocationId
|
||||||
|
apply from: 'gradle-actions.build-result-capture-service.plugin.groovy'
|
||||||
|
}
|
||||||
|
|
||||||
|
void captureUsingBuildFinished(gradle, String invocationId, ResultsWriter resultsWriter) {
|
||||||
|
gradle.buildFinished { result ->
|
||||||
|
println "Got buildFinished: ${result}"
|
||||||
|
def buildResults = [
|
||||||
|
rootProjectName: rootProject.name,
|
||||||
|
rootProjectDir: rootProject.projectDir.absolutePath,
|
||||||
|
requestedTasks: gradle.startParameter.taskNames.join(" "),
|
||||||
|
gradleVersion: GradleVersion.current().version,
|
||||||
|
gradleHomeDir: gradle.gradleHomeDir.absolutePath,
|
||||||
|
buildFailed: result.failure != null
|
||||||
|
]
|
||||||
|
resultsWriter.writeToResultsFile("build-results", invocationId, buildResults)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The `buildScanPublished` hook allows the capture of the Build Scan URI.
|
// The `buildScanPublished` hook allows the capture of the Build Scan URI.
|
||||||
// Results captured this way will overwrite any results from 'buildFinished'.
|
// Results captured this way will overwrite any results from 'buildFinished'.
|
||||||
def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId) {
|
void captureUsingBuildScanPublished(buildScanExtension, String invocationId, ResultsWriter resultsWriter) {
|
||||||
buildScanExtension.with {
|
buildScanExtension.with {
|
||||||
def buildResults = new BuildResults(invocationId, gradle, rootProject)
|
|
||||||
|
|
||||||
buildFinished { result ->
|
|
||||||
buildResults.setBuildResult(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
buildScanPublished { buildScan ->
|
buildScanPublished { buildScan ->
|
||||||
buildResults.setBuildScanUri(buildScan.buildScanUri.toASCIIString())
|
def scanResults = [
|
||||||
buildResults.writeToResultsFile(true)
|
buildScanUri: buildScan.buildScanUri.toASCIIString(),
|
||||||
|
buildScanFailed: false
|
||||||
|
]
|
||||||
|
resultsWriter.writeToResultsFile("build-scans", invocationId, scanResults)
|
||||||
|
|
||||||
def githubOutput = System.getenv("GITHUB_OUTPUT")
|
def githubOutput = System.getenv("GITHUB_OUTPUT")
|
||||||
if (githubOutput) {
|
if (githubOutput) {
|
||||||
@ -85,63 +103,17 @@ def captureUsingBuildScanPublished(buildScanExtension, rootProject, invocationId
|
|||||||
}
|
}
|
||||||
|
|
||||||
onError { error ->
|
onError { error ->
|
||||||
buildResults.setBuildScanFailed()
|
def scanResults = [
|
||||||
buildResults.writeToResultsFile(true)
|
buildScanUri: null,
|
||||||
|
buildScanFailed: true
|
||||||
|
]
|
||||||
|
resultsWriter.writeToResultsFile("build-scans", invocationId, scanResults)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def captureUsingBuildFinished(gradle, invocationId) {
|
class ResultsWriter {
|
||||||
gradle.buildFinished { result ->
|
void writeToResultsFile(String subDir, String invocationId, def content) {
|
||||||
println "Got buildFinished: ${result}"
|
|
||||||
def buildResults = new BuildResults(invocationId, gradle, gradle.rootProject)
|
|
||||||
buildResults.setBuildResult(result)
|
|
||||||
buildResults.writeToResultsFile(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def captureUsingBuildService(invocationId) {
|
|
||||||
gradle.ext.invocationId = invocationId
|
|
||||||
apply from: 'gradle-actions.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) {
|
|
||||||
try {
|
|
||||||
// Gradle and old Build Scan/Gradle Enterprise plugins report a single optional failure in the build result
|
|
||||||
buildResults['buildFailed'] = result.failure != null
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Develocity plugin unwraps all build failures and reports them as a mandatory array
|
|
||||||
buildResults['buildFailed'] = !result.failures.empty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def setBuildScanUri(def buildScanUrl) {
|
|
||||||
buildResults['buildScanUri'] = buildScanUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
def setBuildScanFailed() {
|
|
||||||
buildResults['buildScanFailed'] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
def writeToResultsFile(boolean overwrite) {
|
|
||||||
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")
|
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")
|
||||||
def githubActionStep = System.getProperty("GITHUB_ACTION") ?: System.getenv("GITHUB_ACTION")
|
def githubActionStep = System.getProperty("GITHUB_ACTION") ?: System.getenv("GITHUB_ACTION")
|
||||||
if (!runnerTempDir || !githubActionStep) {
|
if (!runnerTempDir || !githubActionStep) {
|
||||||
@ -149,19 +121,12 @@ class BuildResults {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
def buildResultsDir = new File(runnerTempDir, ".build-results")
|
def buildResultsDir = new File(runnerTempDir, ".gradle-actions/${subDir}")
|
||||||
buildResultsDir.mkdirs()
|
buildResultsDir.mkdirs()
|
||||||
def buildResultsFile = new File(buildResultsDir, githubActionStep + invocationId + ".json")
|
def buildResultsFile = new File(buildResultsDir, githubActionStep + invocationId + ".json")
|
||||||
|
if (!buildResultsFile.exists()) {
|
||||||
// Overwrite any contents written by buildFinished or build service, since this result is a superset.
|
buildResultsFile << groovy.json.JsonOutput.toJson(content)
|
||||||
if (buildResultsFile.exists()) {
|
|
||||||
if (overwrite) {
|
|
||||||
buildResultsFile.text = groovy.json.JsonOutput.toJson(buildResults)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
buildResultsFile << groovy.json.JsonOutput.toJson(buildResults)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
println "\ngradle action failed to write build-results file. Will continue.\n> ${e.getLocalizedMessage()}"
|
println "\ngradle action failed to write build-results file. Will continue.\n> ${e.getLocalizedMessage()}"
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
|
|||||||
then:
|
then:
|
||||||
assertResults('help', testGradleVersion, false, true)
|
assertResults('help', testGradleVersion, false, true)
|
||||||
assert buildResultFile.delete()
|
assert buildResultFile.delete()
|
||||||
|
assert scanResultFile.delete()
|
||||||
|
|
||||||
when:
|
when:
|
||||||
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)
|
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)
|
||||||
@ -240,12 +241,16 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
|
|||||||
assert results['gradleVersion'] == testGradleVersion.gradleVersion.version
|
assert results['gradleVersion'] == testGradleVersion.gradleVersion.version
|
||||||
assert results['gradleHomeDir'] != null
|
assert results['gradleHomeDir'] != null
|
||||||
assert results['buildFailed'] == hasFailure
|
assert results['buildFailed'] == hasFailure
|
||||||
assert results['buildScanUri'] == (hasBuildScan ? "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}" : null)
|
|
||||||
assert results['buildScanFailed'] == scanUploadFailed
|
if (hasBuildScan || scanUploadFailed) {
|
||||||
|
def scanResults = new JsonSlurper().parse(scanResultFile)
|
||||||
|
assert scanResults['buildScanUri'] == (hasBuildScan ? "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}" : null)
|
||||||
|
assert scanResults['buildScanFailed'] == scanUploadFailed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getBuildResultFile() {
|
private File getBuildResultFile() {
|
||||||
def buildResultsDir = new File(testProjectDir, '.build-results')
|
def buildResultsDir = new File(testProjectDir, '.gradle-actions/build-results')
|
||||||
assert buildResultsDir.directory
|
assert buildResultsDir.directory
|
||||||
assert buildResultsDir.listFiles().size() == 1
|
assert buildResultsDir.listFiles().size() == 1
|
||||||
def resultsFile = buildResultsDir.listFiles()[0]
|
def resultsFile = buildResultsDir.listFiles()[0]
|
||||||
@ -253,4 +258,14 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
|
|||||||
assert resultsFile.text.count('rootProjectName') == 1
|
assert resultsFile.text.count('rootProjectName') == 1
|
||||||
return resultsFile
|
return resultsFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private File getScanResultFile() {
|
||||||
|
def buildResultsDir = new File(testProjectDir, '.gradle-actions/build-scans')
|
||||||
|
assert buildResultsDir.directory
|
||||||
|
assert buildResultsDir.listFiles().size() == 1
|
||||||
|
def resultsFile = buildResultsDir.listFiles()[0]
|
||||||
|
assert resultsFile.name.startsWith('github-step-id')
|
||||||
|
assert resultsFile.text.count('buildScanUri') == 1
|
||||||
|
return resultsFile
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user