Skip to content
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 29 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fac5c10
Adding a very minimal implementation
StanleyGoldman Dec 4, 2019
22849a4
Implementing a GitHubActions build server
StanleyGoldman Dec 4, 2019
a97d489
Registring GitHubAction build server with DI
StanleyGoldman Dec 4, 2019
34dbec2
Allowing GitVersionTask to set the environment variables of GitHub Ac…
StanleyGoldman Dec 5, 2019
3dc9566
Avoiding tags
StanleyGoldman Dec 5, 2019
f0a32e5
Some fixes and some tests
StanleyGoldman Dec 5, 2019
af608a4
Merge branch 'master' into github-actions
StanleyGoldman Dec 5, 2019
a489aa8
Adding debug information
StanleyGoldman Dec 5, 2019
08f86be
Fixing test
StanleyGoldman Dec 5, 2019
53deb69
Adding keys
StanleyGoldman Dec 5, 2019
fb4ed56
Debugging more
StanleyGoldman Dec 5, 2019
a154ed7
Changing variable writing method
StanleyGoldman Dec 5, 2019
a316087
Assuming I have the right idea
StanleyGoldman Dec 5, 2019
b578b4c
Also write to the current environment
StanleyGoldman Dec 5, 2019
e345390
Tweaking message
StanleyGoldman Dec 5, 2019
70936d5
Restoring code
StanleyGoldman Dec 5, 2019
c43ae5d
Fixing test
StanleyGoldman Dec 5, 2019
50eed4d
Trying to set the environment variable to the user instead of the pro…
StanleyGoldman Dec 5, 2019
ce6a07d
Changing Resharper setting which relocates using import to inside nam…
StanleyGoldman Dec 5, 2019
7c0f60b
Fixing build errors
StanleyGoldman Dec 5, 2019
8aa86c9
Removing functionality to set environment variables for current step
StanleyGoldman Dec 5, 2019
e6d95eb
Cleanup
StanleyGoldman Dec 5, 2019
ae75b3c
Merge branch 'master' into github-actions
StanleyGoldman Dec 6, 2019
3d69e6e
Merge branch 'master' into github-actions
arturcic Dec 10, 2019
80df6af
Cleanup
StanleyGoldman Dec 16, 2019
3f31425
Remove console
StanleyGoldman Dec 16, 2019
c896a85
Fixing tests
StanleyGoldman Dec 16, 2019
9b42b52
Removing redundant override
StanleyGoldman Dec 17, 2019
926e3a6
Cleanup
StanleyGoldman Dec 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GitVersion.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ II.2.12 <HandlesEvent />
</Patterns>
</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpMemberOrderPattern/LayoutType/@EntryValue">CustomLayout</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/QualifiedUsingAtNestedScope/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Constructor/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Constructor/Options/=XmlDocumentation/@EntryIndexedValue">False</s:String>
Expand Down
167 changes: 167 additions & 0 deletions src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
using System;
using GitVersion;
using GitVersion.BuildServers;
using GitVersion.Logging;
using NUnit.Framework;
using Shouldly;
using System.Collections.Generic;
using Environment = System.Environment;

namespace GitVersionCore.Tests.BuildServers
{

[TestFixture]
public class GitHubActionsTests : TestBase
{
private IEnvironment environment;
private ILog log;

[SetUp]
public void SetUp()
{
log = new NullLog();

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);

// Act
var result = buildServer.CanApplyToCurrentContext();

// Assert
result.ShouldBeTrue();
}

[Test]
public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet()
{
// Arrange
environment.SetEnvironmentVariable("GITHUB_ACTION", "");
var buildServer = new GitHubActions(environment, log);

// 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);

// 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);

// Act
var result = buildServer.GetCurrentBranch(false);

// Assert
result.ShouldBeNull();
}

[TestCase("Something", "1.0.0",
"::set-env name=GitVersion_Something::1.0.0")]
public void GetSetParameterMessage(string key, string value, string expectedResult)
{
// Arrange
var buildServer = new GitHubActions(environment, log);

// Assert
environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace();

// Act
var result = buildServer.GenerateSetParameterMessage(key, value);

// Assert
result.ShouldContain(s => true, 1);
result.ShouldBeEquivalentTo(new[] { expectedResult });
}

[Test]
public void SkipEmptySetParameterMessage()
{
// Arrange
var buildServer = new GitHubActions(environment, log);

// 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);

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'.",
"::set-env name=GitVersion_Major::1.0.0"
};

string.Join(Environment.NewLine, list)
.ShouldBe(string.Join(Environment.NewLine, expected));
}

[Test]
public void GetEmptyGenerateSetVersionMessage()
{
// Arrange
var buildServer = new GitHubActions(environment, log);
var vars = new TestableVersionVariables("1.0.0");

// Act
var message = buildServer.GenerateSetVersionMessage(vars);

// Assert
message.ShouldBeEmpty();
}
}
}
65 changes: 65 additions & 0 deletions src/GitVersionCore/BuildServers/GitHubActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using GitVersion.Logging;
using GitVersion.OutputVariables;

namespace GitVersion.BuildServers
{
public class GitHubActions: BuildServerBase
{
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables

public GitHubActions(IEnvironment environment, ILog log) : base(environment, log)
{
}

public const string EnvironmentVariableName = "GITHUB_ACTION";
Copy link
Contributor

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?

GITHUB_ACTIONS Always set to true when GitHub Actions is running the workflow. You can use this variable to differentiate when tests are being run locally or by GitHub Actions.

Copy link
Member

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected override string EnvironmentVariable { get; } = EnvironmentVariableName;

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}";

return new[]
{
$"::set-env name={key}::{value}"
};
}

return new string[0];
}

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);
}
}
}
1 change: 1 addition & 0 deletions src/GitVersionCore/GitVersionCoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private static void RegisterBuildServers(IServiceCollection services)
services.AddSingleton<IBuildServer, EnvRun>();
services.AddSingleton<IBuildServer, Drone>();
services.AddSingleton<IBuildServer, CodeBuild>();
services.AddSingleton<IBuildServer, GitHubActions>();
}

private static void RegisterVersionStrategies(IServiceCollection services)
Expand Down