Skip to content

Commit f202631

Browse files
committed
For GitHub Actions, outgoing env vars need to be written to a temp file identified by $GITHUB_ENV
1 parent 5a57826 commit f202631

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/GitVersionCore.Tests/BuildAgents/GitHubActionsTests.cs

Lines changed: 18 additions & 3 deletions
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]
@@ -97,8 +108,8 @@ public void GetCurrentBranchShouldHandlePullRequests()
97108
}
98109

99110
[TestCase("Something", "1.0.0",
100-
"\"GitVersion_Something=1.0.0\" >> $GITHUB_ENV")]
101-
public void GetSetParameterMessage(string key, string value, string expectedResult)
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)
102113
{
103114
// Assert
104115
environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace();
@@ -109,6 +120,10 @@ public void GetSetParameterMessage(string key, string value, string expectedResu
109120
// Assert
110121
result.ShouldContain(s => true, 1);
111122
result.ShouldBeEquivalentTo(new[] { expectedResult });
123+
var resultLines = File.ReadAllLines(githubSetEnvironmentTempFilePath);
124+
resultLines.ShouldContain(s => true, 1);
125+
resultLines.ShouldBeEquivalentTo(new[] { expectedFileResult });
126+
112127
}
113128

114129
[Test]
@@ -141,7 +156,7 @@ public void ShouldWriteIntegration()
141156
"Executing GenerateSetVersionMessage for 'GitHubActions'.",
142157
"",
143158
"Executing GenerateBuildLogOutput for 'GitHubActions'.",
144-
"\"GitVersion_Major=1.0.0\" >> $GITHUB_ENV"
159+
"Writing \"GitVersion_Major=1.0.0\" to the file at $GITHUB_ENV"
145160
};
146161

147162
string.Join(Environment.NewLine, list)

src/GitVersionCore/BuildAgents/GitHubActions.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using GitVersion.Logging;
22
using GitVersion.OutputVariables;
3+
using System.IO;
4+
using System.Text;
35

46
namespace GitVersion.BuildAgents
57
{
@@ -9,9 +11,13 @@ public class GitHubActions : BuildAgentBase
911

1012
public GitHubActions(IEnvironment environment, ILog log) : base(environment, log)
1113
{
14+
this.environment = environment;
1215
}
1316

1417
public const string EnvironmentVariableName = "GITHUB_ACTIONS";
18+
public const string GitHubSetEnvTempFileEnvironmentVariableName = "GITHUB_ENV";
19+
private readonly IEnvironment environment;
20+
1521
protected override string EnvironmentVariable { get; } = EnvironmentVariableName;
1622

1723
public override string GenerateSetVersionMessage(VersionVariables variables)
@@ -24,16 +30,31 @@ public override string GenerateSetVersionMessage(VersionVariables variables)
2430
public override string[] GenerateSetParameterMessage(string name, string value)
2531
{
2632
// 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"
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.
2936

3037
if (!string.IsNullOrWhiteSpace(value))
3138
{
32-
var key = $"GitVersion_{name}";
39+
var gitHubSetEnvFilePath = environment.GetEnvironmentVariable(GitHubSetEnvTempFileEnvironmentVariableName);
40+
var assignment = $"GitVersion_{name}={value}";
41+
42+
if (gitHubSetEnvFilePath != null)
43+
{
44+
using (var streamWriter = File.AppendText(gitHubSetEnvFilePath)) // Already uses UTF-8 as required by GitHub
45+
{
46+
streamWriter.WriteLine(assignment);
47+
}
48+
49+
return new[]
50+
{
51+
$"Writing \"{assignment}\" to the file at ${GitHubSetEnvTempFileEnvironmentVariableName}"
52+
};
53+
}
3354

3455
return new[]
3556
{
36-
$"\"{key}={value}\" >> $GITHUB_ENV"
57+
$"Unable to write \"{assignment}\" to ${GitHubSetEnvTempFileEnvironmentVariableName} because the environment variable is not set."
3758
};
3859
}
3960

0 commit comments

Comments
 (0)