-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathGitHubActions.cs
60 lines (48 loc) · 2.85 KB
/
GitHubActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;
namespace GitVersion.Agents;
internal class GitHubActions(IEnvironment environment, ILog log) : BuildAgentBase(environment, log)
{
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
public const string EnvironmentVariableName = "GITHUB_ACTIONS";
public const string GitHubSetEnvTempFileEnvironmentVariableName = "GITHUB_ENV";
protected override string EnvironmentVariable => EnvironmentVariableName;
public override string GenerateSetVersionMessage(GitVersionVariables variables) =>
string.Empty; // There is no equivalent function in GitHub Actions.
public override string[] GenerateSetParameterMessage(string name, string? value) =>
[]; // There is no equivalent function in GitHub Actions.
public override void WriteIntegration(Action<string?> writer, GitVersionVariables variables, bool updateBuildNumber = true)
{
base.WriteIntegration(writer, variables, updateBuildNumber);
// https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files
// The outgoing environment variables must be written to a temporary file (identified by the $GITHUB_ENV environment
// variable, which changes for every step in a workflow) which is then parsed. That file must also be UTF-8, or it will fail.
var gitHubSetEnvFilePath = this.Environment.GetEnvironmentVariable(GitHubSetEnvTempFileEnvironmentVariableName);
if (gitHubSetEnvFilePath != null)
{
writer($"Writing version variables to $GITHUB_ENV file for '{GetType().Name}'.");
using var streamWriter = File.AppendText(gitHubSetEnvFilePath);
foreach (var (key, value) in variables)
{
if (!value.IsNullOrEmpty())
{
streamWriter.WriteLine($"GitVersion_{key}={value}");
}
}
}
else
{
writer($"Unable to write GitVersion variables to ${GitHubSetEnvTempFileEnvironmentVariableName} because the environment variable is not set.");
}
}
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
// GITHUB_REF must be used only for "real" branches, not for tags.
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838
var refType = Environment.GetEnvironmentVariable("GITHUB_REF_TYPE") ?? "";
return refType.Equals("tag", StringComparison.OrdinalIgnoreCase) ? null : Environment.GetEnvironmentVariable("GITHUB_REF");
}
public override bool PreventFetch() => true;
}