Skip to content

Commit 19e7556

Browse files
authored
Merge pull request #2450 from DavidBoike/fix-github-actions
For GitHub Actions, outgoing env vars need to be written to $GITHUV_ENV file
2 parents 5a57826 + 9961722 commit 19e7556

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

src/GitVersionCore.Tests/BuildAgents/GitHubActionsTests.cs

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using GitVersion;
34
using GitVersion.BuildAgents;
45
using GitVersionCore.Tests.Helpers;
@@ -15,6 +16,7 @@ public class GitHubActionsTests : TestBase
1516
{
1617
private IEnvironment environment;
1718
private GitHubActions buildServer;
19+
private string githubSetEnvironmentTempFilePath;
1820

1921
[SetUp]
2022
public void SetUp()
@@ -26,12 +28,21 @@ public void SetUp()
2628
environment = sp.GetService<IEnvironment>();
2729
buildServer = sp.GetService<GitHubActions>();
2830
environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true");
31+
32+
githubSetEnvironmentTempFilePath = Path.GetTempFileName();
33+
environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, githubSetEnvironmentTempFilePath);
2934
}
3035

3136
[TearDown]
3237
public void TearDown()
3338
{
3439
environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, null);
40+
environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, null);
41+
if (githubSetEnvironmentTempFilePath != null && File.Exists(githubSetEnvironmentTempFilePath))
42+
{
43+
File.Delete(githubSetEnvironmentTempFilePath);
44+
githubSetEnvironmentTempFilePath = null;
45+
}
3546
}
3647

3748
[Test]
@@ -96,19 +107,17 @@ public void GetCurrentBranchShouldHandlePullRequests()
96107
result.ShouldBe("refs/pull/1/merge");
97108
}
98109

99-
[TestCase("Something", "1.0.0",
100-
"\"GitVersion_Something=1.0.0\" >> $GITHUB_ENV")]
101-
public void GetSetParameterMessage(string key, string value, string expectedResult)
110+
[Test]
111+
public void GetSetParameterMessage()
102112
{
103113
// Assert
104114
environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace();
105115

106116
// Act
107-
var result = buildServer.GenerateSetParameterMessage(key, value);
117+
var result = buildServer.GenerateSetParameterMessage("GitVersion_Something", "1.0.0");
108118

109119
// Assert
110-
result.ShouldContain(s => true, 1);
111-
result.ShouldBeEquivalentTo(new[] { expectedResult });
120+
result.ShouldContain(s => true, 0);
112121
}
113122

114123
[Test]
@@ -141,11 +150,20 @@ public void ShouldWriteIntegration()
141150
"Executing GenerateSetVersionMessage for 'GitHubActions'.",
142151
"",
143152
"Executing GenerateBuildLogOutput for 'GitHubActions'.",
144-
"\"GitVersion_Major=1.0.0\" >> $GITHUB_ENV"
153+
"Writing version variables to $GITHUB_ENV file for 'GitHubActions'."
145154
};
146155

147156
string.Join(Environment.NewLine, list)
148157
.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);
149167
}
150168

151169
[Test]

src/GitVersionCore/BuildAgents/GitHubActions.cs

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GitVersion.Logging;
22
using GitVersion.OutputVariables;
3+
using System.IO;
34

45
namespace GitVersion.BuildAgents
56
{
@@ -12,6 +13,8 @@ public GitHubActions(IEnvironment environment, ILog log) : base(environment, log
1213
}
1314

1415
public const string EnvironmentVariableName = "GITHUB_ACTIONS";
16+
public const string GitHubSetEnvTempFileEnvironmentVariableName = "GITHUB_ENV";
17+
1518
protected override string EnvironmentVariable { get; } = EnvironmentVariableName;
1619

1720
public override string GenerateSetVersionMessage(VersionVariables variables)
@@ -23,21 +26,44 @@ public override string GenerateSetVersionMessage(VersionVariables variables)
2326

2427
public override string[] GenerateSetParameterMessage(string name, string value)
2528
{
29+
// There is no equivalent function in GitHub Actions.
30+
31+
return new string[0];
32+
}
33+
34+
public override void WriteIntegration(System.Action<string> writer, VersionVariables variables, bool updateBuildNumber = true)
35+
{
36+
base.WriteIntegration(writer, variables, updateBuildNumber);
37+
2638
// https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files
27-
// Example
28-
// echo "name=action_state::yellow >> $GITHUB_ENV"
39+
// The outgoing environment variables must be written to a temporary file (identified by the $GITHUB_ENV environment
40+
// variable, which changes for every step in a workflow) which is then parsed. That file must also be UTF-8 or it will fail.
2941

30-
if (!string.IsNullOrWhiteSpace(value))
42+
if (writer == null || !updateBuildNumber)
3143
{
32-
var key = $"GitVersion_{name}";
44+
return;
45+
}
46+
47+
var gitHubSetEnvFilePath = this.Environment.GetEnvironmentVariable(GitHubSetEnvTempFileEnvironmentVariableName);
3348

34-
return new[]
49+
if (gitHubSetEnvFilePath != null)
50+
{
51+
writer($"Writing version variables to $GITHUB_ENV file for '{GetType().Name}'.");
52+
using (var streamWriter = File.AppendText(gitHubSetEnvFilePath)) // Already uses UTF-8 as required by GitHub
3553
{
36-
$"\"{key}={value}\" >> $GITHUB_ENV"
37-
};
54+
foreach (var variable in variables)
55+
{
56+
if (!string.IsNullOrEmpty(variable.Value))
57+
{
58+
streamWriter.WriteLine($"GitVersion_{variable.Key}={variable.Value}");
59+
}
60+
}
61+
}
62+
}
63+
else
64+
{
65+
writer($"Unable to write GitVersion variables to ${GitHubSetEnvTempFileEnvironmentVariableName} because the environment variable is not set.");
3866
}
39-
40-
return new string[0];
4167
}
4268

4369
public override string GetCurrentBranch(bool usingDynamicRepos)

0 commit comments

Comments
 (0)