Skip to content

Commit 5bbc428

Browse files
authored
Merge pull request #139 from Romanx/master
Implements CloudBuild support for Teamcity
2 parents f1cd127 + 2ddfef4 commit 5bbc428

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

doc/cloudbuild.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ you can activate features for some cloud build systems, as follows
2121
Cloud builds tend to associate some calendar date or monotonically increasing
2222
build number to each build. These build numbers are not very informative, if at all.
2323
Instead, Nerdbank.GitVersioning can automatically set your cloud build's
24-
build number to equal the semver version calculated during your build.
24+
build number to equal the semver version calculated during your build.
2525

2626
Enable this feature by setting the `cloudBuild.buildNumber.enabled` field
2727
in your `version.json` file to `true`, as shown below:
@@ -76,4 +76,12 @@ in your `version.json` file to `true`, as shown below:
7676
}
7777
```
7878

79+
## CI Server specific configurations
80+
81+
### TeamCity
82+
TeamCity does not expose the build branch by default as an environment variable. This can be exposed by
83+
adding an environment variable with the value of `%teamcity.build.vcs.branch.<vcsid>%` where `<vcsid>` is
84+
the root id described on the TeamCity VCS roots page. Details on this variable can be found on the
85+
[TeamCity docs](https://confluence.jetbrains.com/display/TCD8/Predefined+Build+Parameters).
86+
7987
[Issue37]: https://github.com/AArnott/Nerdbank.GitVersioning/issues/37

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public static IEnumerable<object[]> CloudBuildOfBranch(string branchName)
436436
new object[] { CloudBuild.AppVeyor.SetItem("APPVEYOR_REPO_BRANCH", branchName) },
437437
new object[] { CloudBuild.VSTS.SetItem("BUILD_SOURCEBRANCH", $"refs/heads/{branchName}") },
438438
new object[] { CloudBuild.VSTS.SetItem("BUILD_SOURCEBRANCH", branchName) }, // VSTS building a github repo
439+
new object[] { CloudBuild.Teamcity.SetItem("BUILD_GIT_BRANCH", $"refs/heads/{branchName}") },
439440
};
440441
}
441442

@@ -1028,11 +1029,16 @@ private static class CloudBuild
10281029
.Add("APPVEYOR_PULL_REQUEST_NUMBER", string.Empty)
10291030
// VSTS
10301031
.Add("SYSTEM_TEAMPROJECTID", string.Empty)
1031-
.Add("BUILD_SOURCEBRANCH", string.Empty);
1032+
.Add("BUILD_SOURCEBRANCH", string.Empty)
1033+
// Teamcity
1034+
.Add("BUILD_VCS_NUMBER", string.Empty)
1035+
.Add("BUILD_GIT_BRANCH", string.Empty);
10321036
public static readonly ImmutableDictionary<string, string> VSTS = SuppressEnvironment
10331037
.SetItem("SYSTEM_TEAMPROJECTID", "1");
10341038
public static readonly ImmutableDictionary<string, string> AppVeyor = SuppressEnvironment
10351039
.SetItem("APPVEYOR", "True");
1040+
public static readonly ImmutableDictionary<string, string> Teamcity = SuppressEnvironment
1041+
.SetItem("BUILD_VCS_NUMBER", "1");
10361042
}
10371043

10381044
private static class Targets

src/NerdBank.GitVersioning/CloudBuildServices/TeamCity.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
using System.Collections.Generic;
55
using System.IO;
66

7+
/// <summary>
8+
/// TeamCity CI build support.
9+
/// </summary>
10+
/// <remarks>
11+
/// The TeamCIty-specific properties referenced here are documented here:
12+
/// https://confluence.jetbrains.com/display/TCD8/Predefined+Build+Parameters
13+
/// </remarks>
714
internal class TeamCity : ICloudBuild
815
{
9-
public string BuildingBranch => null;
16+
public string BuildingBranch => CloudBuild.ShouldStartWith(Environment.GetEnvironmentVariable("BUILD_GIT_BRANCH"), "refs/heads/");
1017

1118
public string BuildingTag => null;
1219

@@ -18,11 +25,15 @@ internal class TeamCity : ICloudBuild
1825

1926
public IReadOnlyDictionary<string, string> SetCloudBuildNumber(string buildNumber, TextWriter stdout, TextWriter stderr)
2027
{
28+
(stdout ?? Console.Out).WriteLine($"##teamcity[buildNumber '{buildNumber}']");
2129
return new Dictionary<string, string>();
2230
}
2331

2432
public IReadOnlyDictionary<string, string> SetCloudBuildVariable(string name, string value, TextWriter stdout, TextWriter stderr)
2533
{
34+
(stdout ?? Console.Out).WriteLine($"##teamcity[setParameter name='{name}' value='{value}']");
35+
(stdout ?? Console.Out).WriteLine($"##teamcity[setParameter name='system.{name}' value='{value}']");
36+
2637
return new Dictionary<string, string>();
2738
}
2839
}

0 commit comments

Comments
 (0)