Skip to content

Commit e15413f

Browse files
committed
Build agent now writes properties to file as environment variable exports
1 parent f28807e commit e15413f

File tree

2 files changed

+109
-16
lines changed

2 files changed

+109
-16
lines changed

src/GitVersion.Core.Tests/BuildAgents/BitBucketPipelinesTests.cs

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using GitVersion.BuildAgents;
22
using GitVersion.Core.Tests.Helpers;
3+
using GitVersion.Helpers;
4+
using GitVersion.VersionCalculation;
35
using Microsoft.Extensions.DependencyInjection;
46
using NUnit.Framework;
57
using Shouldly;
@@ -11,22 +13,37 @@ public class BitBucketPipelinesTests : TestBase
1113
{
1214
private IEnvironment environment;
1315
private BitBucketPipelines buildServer;
16+
private IServiceProvider sp;
1417

1518
[SetUp]
1619
public void SetEnvironmentVariableForTest()
1720
{
18-
var sp = ConfigureServices(services => services.AddSingleton<BitBucketPipelines>());
21+
this.sp = ConfigureServices(services => services.AddSingleton<BitBucketPipelines>());
1922
this.environment = sp.GetRequiredService<IEnvironment>();
2023
this.buildServer = sp.GetRequiredService<BitBucketPipelines>();
2124

22-
this.environment.SetEnvironmentVariable("BITBUCKET_WORKSPACE", "MyWorkspace");
25+
this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, "MyWorkspace");
26+
}
27+
28+
29+
[Test]
30+
public void CanNotApplyToCurrentContextWhenEnvironmentVariableNotSet()
31+
{
32+
// Arrange
33+
this.environment.SetEnvironmentVariable(BitBucketPipelines.EnvironmentVariableName, "");
34+
35+
// Act
36+
var result = this.buildServer.CanApplyToCurrentContext();
37+
38+
// Assert
39+
result.ShouldBeFalse();
2340
}
2441

2542
[Test]
2643
public void CalculateVersionOnMainBranch()
2744
{
2845
// Arrange
29-
this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/main");
46+
this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/main");
3047

3148
var vars = new TestableVersionVariables(fullSemVer: "1.2.3");
3249
var vsVersion = this.buildServer.GenerateSetVersionMessage(vars);
@@ -38,7 +55,7 @@ public void CalculateVersionOnMainBranch()
3855
public void CalculateVersionOnDevelopBranch()
3956
{
4057
// Arrange
41-
this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/develop");
58+
this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/develop");
4259

4360
var vars = new TestableVersionVariables(fullSemVer: "1.2.3-unstable.4");
4461
var vsVersion = this.buildServer.GenerateSetVersionMessage(vars);
@@ -50,7 +67,7 @@ public void CalculateVersionOnDevelopBranch()
5067
public void CalculateVersionOnFeatureBranch()
5168
{
5269
// Arrange
53-
this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work");
70+
this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/feature/my-work");
5471

5572
var vars = new TestableVersionVariables(fullSemVer: "1.2.3-beta.4");
5673
var vsVersion = this.buildServer.GenerateSetVersionMessage(vars);
@@ -62,7 +79,7 @@ public void CalculateVersionOnFeatureBranch()
6279
public void GetCurrentBranchShouldHandleBranches()
6380
{
6481
// Arrange
65-
this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", "refs/heads/feature/my-work");
82+
this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, "refs/heads/feature/my-work");
6683

6784
// Act
6885
var result = this.buildServer.GetCurrentBranch(false);
@@ -75,8 +92,8 @@ public void GetCurrentBranchShouldHandleBranches()
7592
public void GetCurrentBranchShouldHandleTags()
7693
{
7794
// Arrange
78-
this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null);
79-
this.environment.SetEnvironmentVariable("BITBUCKET_TAG", "refs/heads/tags/1.2.3");
95+
this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, null);
96+
this.environment.SetEnvironmentVariable(BitBucketPipelines.TagEnvironmentVariableName, "refs/heads/tags/1.2.3");
8097

8198
// Act
8299
var result = this.buildServer.GetCurrentBranch(false);
@@ -89,14 +106,68 @@ public void GetCurrentBranchShouldHandleTags()
89106
public void GetCurrentBranchShouldHandlePullRequests()
90107
{
91108
// Arrange
92-
this.environment.SetEnvironmentVariable("BITBUCKET_BRANCH", null);
93-
this.environment.SetEnvironmentVariable("BITBUCKET_TAG", null);
94-
this.environment.SetEnvironmentVariable("BITBUCKET_PR_ID", "refs/pull/1/merge");
109+
this.environment.SetEnvironmentVariable(BitBucketPipelines.BranchEnvironmentVariableName, null);
110+
this.environment.SetEnvironmentVariable(BitBucketPipelines.TagEnvironmentVariableName, null);
111+
this.environment.SetEnvironmentVariable(BitBucketPipelines.PullRequestEnvironmentVariableName, "refs/pull/1/merge");
95112

96113
// Act
97114
var result = this.buildServer.GetCurrentBranch(false);
98115

99116
// Assert
100117
result.ShouldBeNull();
101118
}
119+
120+
121+
[Test]
122+
public void WriteAllVariablesToTheTextWriter()
123+
{
124+
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
125+
assemblyLocation.ShouldNotBeNull();
126+
var f = PathHelper.Combine(assemblyLocation, "gitversion.env");
127+
128+
try
129+
{
130+
AssertVariablesAreWrittenToFile(f);
131+
}
132+
finally
133+
{
134+
File.Delete(f);
135+
}
136+
}
137+
138+
private void AssertVariablesAreWrittenToFile(string file)
139+
{
140+
var writes = new List<string?>();
141+
var semanticVersion = new SemanticVersion
142+
{
143+
Major = 1,
144+
Minor = 2,
145+
Patch = 3,
146+
PreReleaseTag = "beta1",
147+
BuildMetaData = "5"
148+
};
149+
150+
semanticVersion.BuildMetaData.CommitDate = new DateTimeOffset(2022, 4, 6, 16, 10, 59, TimeSpan.FromHours(10));
151+
semanticVersion.BuildMetaData.Sha = "f28807e615e9f06aec8a33c87780374e0c1f6fb8";
152+
153+
var config = new TestEffectiveConfiguration();
154+
var variableProvider = this.sp.GetRequiredService<IVariableProvider>();
155+
156+
var variables = variableProvider.GetVariablesFor(semanticVersion, config, false);
157+
158+
this.buildServer.WithPropertyFile(file);
159+
160+
this.buildServer.WriteIntegration(writes.Add, variables);
161+
162+
writes[1].ShouldBe("1.2.3-beta.1+5");
163+
164+
File.Exists(file).ShouldBe(true);
165+
166+
var props = File.ReadAllText(file);
167+
168+
props.ShouldContain("export GITVERSION_MAJOR=1");
169+
props.ShouldContain("export GITVERSION_MINOR=2");
170+
props.ShouldContain("export GITVERSION_SHA=f28807e615e9f06aec8a33c87780374e0c1f6fb8");
171+
props.ShouldContain("export GITVERSION_COMMITDATE=2022-04-06");
172+
}
102173
}

src/GitVersion.Core/BuildAgents/BitBucketPipelines.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@ namespace GitVersion.BuildAgents;
55

66
public class BitBucketPipelines : BuildAgentBase
77
{
8-
public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log)
9-
{
10-
}
8+
public const string EnvironmentVariableName = "BITBUCKET_WORKSPACE";
9+
public const string BranchEnvironmentVariableName = "BITBUCKET_BRANCH";
10+
public const string TagEnvironmentVariableName = "BITBUCKET_TAG";
11+
public const string PullRequestEnvironmentVariableName = "BITBUCKET_PR_ID";
12+
private string? file;
13+
14+
public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) => WithPropertyFile("gitversion.env");
1115

12-
protected override string EnvironmentVariable => "BITBUCKET_WORKSPACE";
16+
protected override string EnvironmentVariable => EnvironmentVariableName;
1317

1418
public override string? GenerateSetVersionMessage(VersionVariables variables) => variables.FullSemVer;
1519

20+
public void WithPropertyFile(string propertiesFileName) => this.file = propertiesFileName;
21+
1622
public override string[] GenerateSetParameterMessage(string name, string value) => new[]
1723
{
1824
$"GITVERSION_{name.ToUpperInvariant()}={value}"
1925
};
2026

2127
public override string? GetCurrentBranch(bool usingDynamicRepos)
2228
{
23-
var branchName = EvaluateEnvironmentVariable("BITBUCKET_BRANCH");
29+
var branchName = EvaluateEnvironmentVariable(BranchEnvironmentVariableName);
2430
if (branchName != null && branchName.StartsWith("refs/heads/"))
2531
{
2632
return branchName;
@@ -29,6 +35,22 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2935
return null;
3036
}
3137

38+
public override void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)
39+
{
40+
if (this.file is null)
41+
return;
42+
43+
base.WriteIntegration(writer, variables, updateBuildNumber);
44+
writer($"Outputting variables to '{this.file}' ... ");
45+
46+
var exports = variables
47+
.Select(variable => $"export GITVERSION_{variable.Key.ToUpperInvariant()}={variable.Value}")
48+
.ToList();
49+
50+
File.WriteAllLines(this.file, exports);
51+
}
52+
53+
3254
private string? EvaluateEnvironmentVariable(string variableName)
3355
{
3456
var branchName = Environment.GetEnvironmentVariable(variableName);

0 commit comments

Comments
 (0)