Skip to content

Commit 74d4fb6

Browse files
committed
Implement in WriteIntegration to only open file handle once
1 parent f202631 commit 74d4fb6

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

src/GitVersionCore.Tests/BuildAgents/GitHubActionsTests.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,17 @@ public void GetCurrentBranchShouldHandlePullRequests()
107107
result.ShouldBe("refs/pull/1/merge");
108108
}
109109

110-
[TestCase("Something", "1.0.0",
111-
"Writing \"GitVersion_Something=1.0.0\" to the file at $GITHUB_ENV", "GitVersion_Something=1.0.0")]
112-
public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedFileResult)
110+
[Test]
111+
public void GetSetParameterMessage()
113112
{
114113
// Assert
115114
environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace();
116115

117116
// Act
118-
var result = buildServer.GenerateSetParameterMessage(key, value);
117+
var result = buildServer.GenerateSetParameterMessage("GitVersion_Something", "1.0.0");
119118

120119
// Assert
121-
result.ShouldContain(s => true, 1);
122-
result.ShouldBeEquivalentTo(new[] { expectedResult });
123-
var resultLines = File.ReadAllLines(githubSetEnvironmentTempFilePath);
124-
resultLines.ShouldContain(s => true, 1);
125-
resultLines.ShouldBeEquivalentTo(new[] { expectedFileResult });
126-
120+
result.ShouldContain(s => true, 0);
127121
}
128122

129123
[Test]
@@ -156,11 +150,20 @@ public void ShouldWriteIntegration()
156150
"Executing GenerateSetVersionMessage for 'GitHubActions'.",
157151
"",
158152
"Executing GenerateBuildLogOutput for 'GitHubActions'.",
159-
"Writing \"GitVersion_Major=1.0.0\" to the file at $GITHUB_ENV"
153+
"Writing version variables to $GITHUB_ENV file for 'GitHubActions'."
160154
};
161155

162156
string.Join(Environment.NewLine, list)
163157
.ShouldBe(string.Join(Environment.NewLine, expected));
158+
159+
var expectedFileContents = new List<string>
160+
{
161+
"GitVersion_Major=1.0.0"
162+
};
163+
164+
var actualFileContents = File.ReadAllLines(githubSetEnvironmentTempFilePath);
165+
166+
actualFileContents.ShouldBe(expectedFileContents);
164167
}
165168

166169
[Test]

src/GitVersionCore/BuildAgents/GitHubActions.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,44 @@ public override string GenerateSetVersionMessage(VersionVariables variables)
2929

3030
public override string[] GenerateSetParameterMessage(string name, string value)
3131
{
32+
// There is no equivalent function in GitHub Actions.
33+
34+
return new string[0];
35+
}
36+
37+
public override void WriteIntegration(System.Action<string> writer, VersionVariables variables, bool updateBuildNumber = true)
38+
{
39+
base.WriteIntegration(writer, variables, updateBuildNumber);
40+
3241
// https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files
33-
// However it's important that GitHub Actions does not parse the log output. The outgoing environment variables must be
34-
// written to a temporary file (identified by the $GITHUB_ENV environment variable, which changes for every step in a workflow)
35-
// which is then parsed. That file must also be UTF-8 or it will fail.
42+
// The outgoing environment variables must be written to a temporary file (identified by the $GITHUB_ENV environment
43+
// variable, which changes for every step in a workflow) which is then parsed. That file must also be UTF-8 or it will fail.
3644

37-
if (!string.IsNullOrWhiteSpace(value))
45+
if (writer == null || !updateBuildNumber)
3846
{
39-
var gitHubSetEnvFilePath = environment.GetEnvironmentVariable(GitHubSetEnvTempFileEnvironmentVariableName);
40-
var assignment = $"GitVersion_{name}={value}";
47+
return;
48+
}
49+
50+
var gitHubSetEnvFilePath = environment.GetEnvironmentVariable(GitHubSetEnvTempFileEnvironmentVariableName);
4151

42-
if (gitHubSetEnvFilePath != null)
52+
if (gitHubSetEnvFilePath != null)
53+
{
54+
writer($"Writing version variables to $GITHUB_ENV file for '{GetType().Name}'.");
55+
using (var streamWriter = File.AppendText(gitHubSetEnvFilePath)) // Already uses UTF-8 as required by GitHub
4356
{
44-
using (var streamWriter = File.AppendText(gitHubSetEnvFilePath)) // Already uses UTF-8 as required by GitHub
57+
foreach (var variable in variables)
4558
{
46-
streamWriter.WriteLine(assignment);
59+
if (!string.IsNullOrEmpty(variable.Value))
60+
{
61+
streamWriter.WriteLine($"GitVersion_{variable.Key}={variable.Value}");
62+
}
4763
}
48-
49-
return new[]
50-
{
51-
$"Writing \"{assignment}\" to the file at ${GitHubSetEnvTempFileEnvironmentVariableName}"
52-
};
5364
}
54-
55-
return new[]
56-
{
57-
$"Unable to write \"{assignment}\" to ${GitHubSetEnvTempFileEnvironmentVariableName} because the environment variable is not set."
58-
};
5965
}
60-
61-
return new string[0];
66+
else
67+
{
68+
writer($"Unable to write GitVersion variables to ${GitHubSetEnvTempFileEnvironmentVariableName} because the environment variable is not set.");
69+
}
6270
}
6371

6472
public override string GetCurrentBranch(bool usingDynamicRepos)

0 commit comments

Comments
 (0)