Do not write build-results file outside GitHub Actions context

The init-scripts added to Gradle User Home were assuming the presence of certain
GitHub Actions environment variables. With this fix, these init scripts behave
better without these env vars.

Fixes #350
This commit is contained in:
Daz DeBoer 2022-07-11 15:09:56 -06:00
parent 86da5e6c4e
commit 792a93a5e3
No known key found for this signature in database
GPG Key ID: DD6B9F0B06683D5D
4 changed files with 33 additions and 10 deletions

View File

@ -45,9 +45,15 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
buildScanFailed: false buildScanFailed: false
] ]
def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") def runnerTempDir = System.getenv("RUNNER_TEMP")
def githubActionStep = System.getenv("GITHUB_ACTION")
if (!runnerTempDir || !githubActionStep) {
return
}
def buildResultsDir = new File(runnerTempDir, ".build-results")
buildResultsDir.mkdirs() buildResultsDir.mkdirs()
def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + getParameters().getInvocationId().get() + ".json") def buildResultsFile = new File(buildResultsDir, githubActionStep + getParameters().getInvocationId().get() + ".json")
buildResultsFile << groovy.json.JsonOutput.toJson(buildResults) buildResultsFile << groovy.json.JsonOutput.toJson(buildResults)
} }
} }

View File

@ -106,9 +106,15 @@ class BuildResults {
} }
def writeToResultsFile(boolean overwrite) { def writeToResultsFile(boolean overwrite) {
def buildResultsDir = new File(System.getenv("RUNNER_TEMP"), ".build-results") def runnerTempDir = System.getenv("RUNNER_TEMP")
def githubActionStep = System.getenv("GITHUB_ACTION")
if (!runnerTempDir || !githubActionStep) {
return
}
def buildResultsDir = new File(runnerTempDir, ".build-results")
buildResultsDir.mkdirs() buildResultsDir.mkdirs()
def buildResultsFile = new File(buildResultsDir, System.getenv("GITHUB_ACTION") + invocationId + ".json") def buildResultsFile = new File(buildResultsDir, githubActionStep + invocationId + ".json")
// Overwrite any contents written by buildFinished or build service, since this result is a superset. // Overwrite any contents written by buildFinished or build service, since this result is a superset.
if (buildResultsFile.exists()) { if (buildResultsFile.exists()) {

View File

@ -200,20 +200,17 @@ task expectFailure {
File initScriptsDir = new File(testProjectDir, "initScripts") File initScriptsDir = new File(testProjectDir, "initScripts")
args << '-I' << new File(initScriptsDir, initScript).absolutePath args << '-I' << new File(initScriptsDir, initScript).absolutePath
envVars['RUNNER_TEMP'] = testProjectDir.absolutePath envVars.putIfAbsent('RUNNER_TEMP', testProjectDir.absolutePath)
envVars['GITHUB_ACTION'] = 'github-step-id' envVars.putIfAbsent('GITHUB_ACTION', 'github-step-id')
def runner = ((DefaultGradleRunner) GradleRunner.create()) def runner = ((DefaultGradleRunner) GradleRunner.create())
.withJvmArguments(jvmArgs) .withJvmArguments(jvmArgs)
.withGradleVersion(gradleVersion.version) .withGradleVersion(gradleVersion.version)
.withProjectDir(testProjectDir) .withProjectDir(testProjectDir)
.withArguments(args) .withArguments(args)
.withEnvironment(envVars)
.forwardOutput() .forwardOutput()
if (envVars) {
runner.withEnvironment(envVars)
}
runner runner
} }

View File

@ -134,6 +134,20 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
testGradleVersion << ALL_VERSIONS testGradleVersion << ALL_VERSIONS
} }
def "produces no build results file when GitHub env vars not set with #testGradleVersion"() {
assumeTrue testGradleVersion.compatibleWithCurrentJvm
when:
run(['help'], initScript, testGradleVersion.gradleVersion, [], [RUNNER_TEMP: '', GITHUB_ACTION: ''])
then:
def buildResultsDir = new File(testProjectDir, '.build-results')
assert !buildResultsDir.exists()
where:
testGradleVersion << ALL_VERSIONS
}
void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan, boolean scanUploadFailed = false) { void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan, boolean scanUploadFailed = false) {
def results = new JsonSlurper().parse(buildResultFile) def results = new JsonSlurper().parse(buildResultFile)
assert results['rootProjectName'] == ROOT_PROJECT_NAME assert results['rootProjectName'] == ROOT_PROJECT_NAME