-
Notifications
You must be signed in to change notification settings - Fork 652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for GitHub Actions #1966
Merged
Merged
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
fac5c10
Adding a very minimal implementation
StanleyGoldman 22849a4
Implementing a GitHubActions build server
StanleyGoldman a97d489
Registring GitHubAction build server with DI
StanleyGoldman 34dbec2
Allowing GitVersionTask to set the environment variables of GitHub Ac…
StanleyGoldman 3dc9566
Avoiding tags
StanleyGoldman f0a32e5
Some fixes and some tests
StanleyGoldman af608a4
Merge branch 'master' into github-actions
StanleyGoldman a489aa8
Adding debug information
StanleyGoldman 08f86be
Fixing test
StanleyGoldman 53deb69
Adding keys
StanleyGoldman fb4ed56
Debugging more
StanleyGoldman a154ed7
Changing variable writing method
StanleyGoldman a316087
Assuming I have the right idea
StanleyGoldman b578b4c
Also write to the current environment
StanleyGoldman e345390
Tweaking message
StanleyGoldman 70936d5
Restoring code
StanleyGoldman c43ae5d
Fixing test
StanleyGoldman 50eed4d
Trying to set the environment variable to the user instead of the pro…
StanleyGoldman ce6a07d
Changing Resharper setting which relocates using import to inside nam…
StanleyGoldman 7c0f60b
Fixing build errors
StanleyGoldman 8aa86c9
Removing functionality to set environment variables for current step
StanleyGoldman e6d95eb
Cleanup
StanleyGoldman ae75b3c
Merge branch 'master' into github-actions
StanleyGoldman 3d69e6e
Merge branch 'master' into github-actions
arturcic 80df6af
Cleanup
StanleyGoldman 3f31425
Remove console
StanleyGoldman c896a85
Fixing tests
StanleyGoldman 9b42b52
Removing redundant override
StanleyGoldman 926e3a6
Cleanup
StanleyGoldman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
using System; | ||
using GitVersion; | ||
using GitVersion.BuildServers; | ||
using GitVersion.Logging; | ||
using NUnit.Framework; | ||
using Shouldly; | ||
using System.Collections.Generic; | ||
using GitVersion.Configuration; | ||
using NSubstitute; | ||
using Environment = System.Environment; | ||
|
||
namespace GitVersionCore.Tests.BuildServers | ||
{ | ||
|
||
[TestFixture] | ||
public class GitHubActionsTests : TestBase | ||
{ | ||
private IConsole console; | ||
private IEnvironment environment; | ||
private ILog log; | ||
private List<string> consoleLinesWritten; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
log = new NullLog(); | ||
console = Substitute.For<IConsole>(); | ||
|
||
consoleLinesWritten = new List<string>(); | ||
console.WhenForAnyArgs(c => c.WriteLine(default)) | ||
.Do(info => consoleLinesWritten.Add(info.Arg<string>())); | ||
console.WhenForAnyArgs(c => c.WriteLine()) | ||
.Do(info => consoleLinesWritten.Add(string.Empty)); | ||
|
||
environment = new TestEnvironment(); | ||
environment.SetEnvironmentVariable("GITHUB_ACTION", Guid.NewGuid().ToString()); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
environment.SetEnvironmentVariable("GITHUB_ACTION", null); | ||
} | ||
|
||
[Test] | ||
public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet() | ||
{ | ||
// Arrange | ||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
// Act | ||
var result = buildServer.CanApplyToCurrentContext(); | ||
|
||
// Assert | ||
result.ShouldBeTrue(); | ||
} | ||
|
||
[Test] | ||
public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet() | ||
{ | ||
// Arrange | ||
environment.SetEnvironmentVariable("GITHUB_ACTION", ""); | ||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
// Act | ||
var result = buildServer.CanApplyToCurrentContext(); | ||
|
||
// Assert | ||
result.ShouldBeFalse(); | ||
} | ||
|
||
[Test] | ||
public void GetCurrentBranchShouldGetBranchIfSet() | ||
{ | ||
// Arrange | ||
const string expected = "actionsBranch"; | ||
|
||
environment.SetEnvironmentVariable("GITHUB_REF", $"refs/heads/{expected}"); | ||
|
||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
// Act | ||
var result = buildServer.GetCurrentBranch(false); | ||
|
||
// Assert | ||
result.ShouldBe(expected); | ||
} | ||
|
||
[Test] | ||
public void GetCurrentBranchShouldNotMatchTag() | ||
{ | ||
// Arrange | ||
environment.SetEnvironmentVariable("GITHUB_REF", $"refs/tags/v1.0.0"); | ||
|
||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
// Act | ||
var result = buildServer.GetCurrentBranch(false); | ||
|
||
// Assert | ||
result.ShouldBeNull(); | ||
} | ||
|
||
[TestCase("Something", "1.0.0", | ||
"Adding Environment Variable to future steps. name='GitVersion_Something' value='1.0.0'", | ||
"::set-env name=GitVersion_Something::1.0.0")] | ||
public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedConsole) | ||
{ | ||
// Arrange | ||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
// Assert | ||
environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace(); | ||
|
||
// Act | ||
var result = buildServer.GenerateSetParameterMessage(key, value); | ||
|
||
// Assert | ||
result.ShouldContain(s => true, 1); | ||
result.ShouldBeEquivalentTo(new[] { expectedResult }); | ||
|
||
consoleLinesWritten.ShouldBeEquivalentTo(new List<string> { expectedConsole }); | ||
} | ||
|
||
[Test] | ||
public void SkipEmptySetParameterMessage() | ||
{ | ||
// Arrange | ||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
// Act | ||
var result = buildServer.GenerateSetParameterMessage("Hello", string.Empty); | ||
|
||
// Assert | ||
result.ShouldBeEquivalentTo(new string[0]); | ||
} | ||
|
||
[Test] | ||
public void ShouldWriteIntegration() | ||
{ | ||
// Arrange | ||
var buildServer = new GitHubActions(environment, log, console); | ||
|
||
var vars = new TestableVersionVariables("1.0.0"); | ||
|
||
var list = new List<string>(); | ||
|
||
// Assert | ||
environment.GetEnvironmentVariable("GitVersion_Major").ShouldBeNullOrWhiteSpace(); | ||
|
||
// Act | ||
buildServer.WriteIntegration(s => { list.Add(s); }, vars); | ||
|
||
// Assert | ||
var expected = new List<string> | ||
{ | ||
"Executing GenerateSetVersionMessage for 'GitHubActions'.", | ||
"", | ||
"Executing GenerateBuildLogOutput for 'GitHubActions'.", | ||
"Adding Environment Variable to future steps. name='GitVersion_Major' value='1.0.0'" | ||
}; | ||
|
||
string.Join(Environment.NewLine, list) | ||
.ShouldBe(string.Join(Environment.NewLine, expected)); | ||
|
||
consoleLinesWritten.ShouldBeEquivalentTo(new List<string> { "::set-env name=GitVersion_Major::1.0.0" }); | ||
} | ||
|
||
[Test] | ||
public void GetEmptyGenerateSetVersionMessage() | ||
{ | ||
// Arrange | ||
var buildServer = new GitHubActions(environment, log, console); | ||
var vars = new TestableVersionVariables("1.0.0"); | ||
|
||
// Act | ||
var message = buildServer.GenerateSetVersionMessage(vars); | ||
|
||
// Assert | ||
message.ShouldBeEmpty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using GitVersion.Logging; | ||
using GitVersion.OutputVariables; | ||
using GitVersion.Configuration; | ||
|
||
namespace GitVersion.BuildServers | ||
{ | ||
public class GitHubActions: BuildServerBase | ||
{ | ||
private readonly IConsole console; | ||
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables | ||
|
||
public GitHubActions(IEnvironment environment, ILog log, IConsole console) : base(environment, log) | ||
{ | ||
this.console = console; | ||
} | ||
|
||
public const string EnvironmentVariableName = "GITHUB_ACTION"; | ||
protected override string EnvironmentVariable { get; } = EnvironmentVariableName; | ||
public override bool CanApplyToCurrentContext() | ||
{ | ||
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(EnvironmentVariable)); | ||
} | ||
StanleyGoldman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public override string GenerateSetVersionMessage(VersionVariables variables) | ||
{ | ||
// There is no equivalent function in GitHub Actions. | ||
|
||
return string.Empty; | ||
} | ||
|
||
public override string[] GenerateSetParameterMessage(string name, string value) | ||
{ | ||
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env | ||
// Example | ||
// echo "::set-env name=action_state::yellow" | ||
|
||
if (!string.IsNullOrWhiteSpace(value)) | ||
{ | ||
var key = $"GitVersion_{name}"; | ||
|
||
console.WriteLine($"::set-env name={key}::{value}"); | ||
|
||
return new[] | ||
{ | ||
$"Adding Environment Variable to future steps. name='{key}' value='{value}'" | ||
}; | ||
} | ||
|
||
return new string[0]; | ||
StanleyGoldman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public override string GetCurrentBranch(bool usingDynamicRepos) | ||
{ | ||
// GITHUB_REF | ||
// The branch or tag ref that triggered the workflow. | ||
// For example, refs/heads/feature-branch-1. If neither a branch or | ||
// tag is available for the event type, the variable will not exist. | ||
|
||
var value = Environment.GetEnvironmentVariable("GITHUB_REF"); | ||
if (!string.IsNullOrWhiteSpace(value)) | ||
{ | ||
const string refsHeads = "refs/heads/"; | ||
|
||
if (value.StartsWith(refsHeads)) | ||
{ | ||
value = value.Substring(refsHeads.Length); | ||
return value; | ||
} | ||
} | ||
|
||
return base.GetCurrentBranch(usingDynamicRepos); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
GITHUB_ACTIONS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, are you in a position to send a PR with the fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2006