Lukáš Lánský Lukáš Lánský - 2 years ago 188
Git Question

Getting git branch name inside TFS build

I have a CI build definition (TFS 2013) triggered by commits to several branches (it is a git repository). I would like to use the branch name inside build process to copy results to the right place. How can I do this?

Answer Source

Make your own CodeActivity Like this...

you need the IBuildDetail BuildDetail as InArgument.

string branch = null;
string commit;

var environmentVariable = BuildDetail.Get(context);
if (environmentVariable != null && !BuildSourceVersion.TryParseGit(environmentVariable.SourceGetVersion, out branch, out commit))
    var defaultSourceProvider = environmentVariable.BuildDefinition.GetDefaultSourceProvider();
    if (BuildSourceProviders.IsGit(defaultSourceProvider.Name))
        branch = BuildSourceProviders.GetProperty(defaultSourceProvider.Fields, BuildSourceProviders.GitProperties.DefaultBranch);

if (string.IsNullOrEmpty(branch))
    throw new Exception("Could not find Branch");

Branch.Set(context, branch.Substring(branch.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download