Skip to content

Commit 64a64ca

Browse files
committed
Trying to make the TeamCity getting started experience a bit better
Also improving the TeamCity docs
1 parent e98f68b commit 64a64ca

File tree

7 files changed

+56
-36
lines changed

7 files changed

+56
-36
lines changed

docs/build-server-support/build-server/teamcity.md

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,28 @@ TeamCity has support for meta-runners which allow custom tasks. There is a GitVe
1717
- [Project Link](https://github.com/JetBrains/meta-runner-power-pack/tree/master/gitversion)
1818

1919
## Running inside TeamCity
20-
* Make sure to use **agent checkouts** (required, server checkouts do not copy the needed `.git` directory)
21-
- If you want to use *checkout on server*, see [dynamic repositories](../../more-info/dynamic-repositories.md)
22-
* For the moment you need to promote the `%teamcity.build.vcs.branch.{configurationid}%` build parameter to an environment variable with the same name for pull requests to be handled correctly
20+
When running in TeamCIty you have two options, run using **agent checkout** or use dynamic repositories.
21+
22+
### Agent checkout
23+
For GitVersion to pick up pull requests properly you need to promote the `%teamcity.build.vcs.branch.{configurationid}%` variable to a system variable called `Git_Branch`
24+
25+
Just go to your build configuration, Parameters, click Add, Name should be `system.Git_Branch`, value should be `%teamcity.build.vcs.branch.{vcsid}%` where vcsid is your VCS root id. You should get auto completion for this.
26+
27+
### Dynamic repositories
28+
To use server side checkout, you must use the dynamic repositories feature of GitVersion. Server side checkout sends just the files to the agent and not the actual .git folder. Dynamic repositories will clone your repo into a temp folder and use it to calculate version information.
29+
30+
See [dynamic repositories](../../more-info/dynamic-repositories.md) for more info.
31+
32+
### Output
2333
* We update the TC build number to the GitVersion number automatically
24-
* We output the individual values of the GitVersion version as the build parameter: `GitVersion.*` (Eg: `GitVersion.Major`) if you need access to them in your build script
34+
* We output the individual values of the GitVersion version variables as build parameters with format `GitVersion.*` (Eg: `GitVersion.Major`) if you need access to them in your build script. Being system variables they will be passed as msbuild/environmental variables to other build steps
2535

2636
### NuGet in TeamCity
2737
* Add dummy [parameter](http://confluence.jetbrains.com/display/TCD8/Configuring+Build+Parameters) to
2838
the project called `GitVersion.NuGetVersion`. If many of your projects uses git-flow and SemVer you
29-
can add the parameter to the "root-project" (TeamCity 8.x+)
39+
can add the parameter to the "root-project" (TeamCity 8.x+). You need a dummy param because GitVersion creates the variables at runtime, and you cannot reference a paramter which is not available statically. GitVersion will overwrite the dummy value
3040
* Then setup you nuget pack build set the "version" to `%GitVersion.NuGetVersion%`
41+
* If you do your pack in a build script then you can just use environmental variables because teamcity will pass them through automatically.
3142

3243
### When TeamCity -> GitHub can't use https
3344
GitVersion requires the presence of master branch in order to determine the version number. If TeamCity uses https to clone git repos then GitVersion will pull down master branch for you during the build.
@@ -37,29 +48,5 @@ If however your TeamCity uses SSH to clone git repos and https is unavailable th
3748
> [GitVersionTask.UpdateAssemblyInfo] Error occurred: GitVersion.MissingBranchException: Could not fetch from '[email protected]:Xero/Bus.git' since LibGit2 does not support the transport. You have most likely cloned using SSH. If there is a remote branch named 'master' then fetch it manually, otherwise please create a local branch named 'master'. ---> LibGit2Sharp.LibGit2SharpException: An error was raised by libgit2. Category = Net (Error).
3849
This transport isn't implemented. Sorry
3950

40-
You need to create a TeamCity build step before your compile step which manually creates a local master branch which tracks remote master. Like so (in powershell):
41-
42-
```Powershell
43-
$branchBeingBuilt = . git symbolic-ref --short -q HEAD
44-
. git pull 2>&1 | write-host
45-
foreach ($remoteBranch in . git branch -r) {
46-
. git checkout $remoteBranch.Trim().Replace("origin/", "") 2>&1 | write-host
47-
. git pull 2>&1 | write-host
48-
}
49-
. git checkout $branchBeingBuilt 2>&1 | write-host
50-
exit 0
51-
```
52-
53-
you should get build output like
54-
55-
```
56-
[Step 1/1]: Ensure all branches are available for GitVersion (Powershell) (5s)
57-
[Step 1/1] From file:///C:/BuildAgent2/system/git/git-12345678
58-
[Step 1/1] * [new branch] master -> origin/master
59-
[Step 1/1] Switched to a new branch 'master'
60-
[Step 1/1] Branch master set up to track remote branch master from origin.
61-
[Step 1/1] Switched to branch 'develop'
62-
```
63-
6451
## Guides
6552
- [Continuous Delivery Setup in TeamCity](http://jake.ginnivan.net/blog/2014/07/09/my-typical-teamcity-build-setup)

src/GitVersionCore/BuildServers/BuildServerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public abstract class BuildServerBase : IBuildServer
88
public abstract string GenerateSetVersionMessage(VersionVariables variables);
99
public abstract string[] GenerateSetParameterMessage(string name, string value);
1010

11-
public virtual string GetCurrentBranch()
11+
public virtual string GetCurrentBranch(bool usingDynamicRepos)
1212
{
1313
return null;
1414
}

src/GitVersionCore/BuildServers/IBuildServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IBuildServer
88
string GenerateSetVersionMessage(VersionVariables variables);
99
string[] GenerateSetParameterMessage(string name, string value);
1010
void WriteIntegration(Action<string> writer, VersionVariables variables);
11-
string GetCurrentBranch();
11+
string GetCurrentBranch(bool usingDynamicRepos);
1212
/// <summary>
1313
/// If the build server should not try and fetch
1414
/// </summary>

src/GitVersionCore/BuildServers/Jenkins.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3434
};
3535
}
3636

37-
public override string GetCurrentBranch()
37+
public override string GetCurrentBranch(bool usingDynamicRepos)
3838
{
3939
return Environment.GetEnvironmentVariable("GIT_BRANCH");
4040
}

src/GitVersionCore/BuildServers/TeamCity.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@ public override bool CanApplyToCurrentContext()
99
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"));
1010
}
1111

12+
public override string GetCurrentBranch(bool usingDynamicRepos)
13+
{
14+
var branchName = Environment.GetEnvironmentVariable("Git_Branch");
15+
16+
if (string.IsNullOrEmpty(branchName))
17+
{
18+
if (!usingDynamicRepos)
19+
{
20+
WriteBranchEnvVariableWarning();
21+
}
22+
23+
return base.GetCurrentBranch(usingDynamicRepos);
24+
}
25+
26+
return branchName;
27+
}
28+
29+
static void WriteBranchEnvVariableWarning()
30+
{
31+
Logger.WriteWarning(@"TeamCity doesn't make the current branch available through environmental variables.
32+
33+
Depending on your authentication and transport setup of your git VCS root things may work. In that case, ignore this warning.
34+
35+
In your TeamCity build configuration, add a parameter called `system.Git_Branch` with value %teamcity.build.vcs.branch.<vcsid>%
36+
37+
See http://gitversion.readthedocs.org/en/latest/build-server-support/build-server/teamcity for more info");
38+
}
39+
40+
public override bool PreventFetch()
41+
{
42+
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Git_Branch"));
43+
}
44+
1245
public override string[] GenerateSetParameterMessage(string name, string value)
1346
{
1447
return new[]

src/GitVersionCore/BuildServers/VsoAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
1919
};
2020
}
2121

22-
public override string GetCurrentBranch()
22+
public override string GetCurrentBranch(bool usingDynamicRepos)
2323
{
2424
return Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
2525
}

src/GitVersionCore/ExecuteCore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ public bool TryGetVersion(string directory, out VersionVariables versionVariable
6363
}
6464
}
6565

66-
static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
66+
static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository)
6767
{
6868
if (buildServer == null)
6969
{
7070
return targetBranch;
7171
}
7272

73-
var currentBranch = buildServer.GetCurrentBranch() ?? targetBranch;
73+
var currentBranch = buildServer.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
7474
Logger.WriteInfo("Branch from build environment: " + currentBranch);
7575

7676
return currentBranch;
7777
}
7878

7979
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot, IBuildServer buildServer)
8080
{
81-
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));
81+
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch, gitPreparer.IsDynamicGitRepository));
8282

8383
var versionFinder = new GitVersionFinder();
8484
var configuration = ConfigurationProvider.Provide(projectRoot, fileSystem);

0 commit comments

Comments
 (0)