简体   繁体   English

在TFS内部获取git分支名称

[英]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). 我有一个CI构建定义(TFS 2013),是由对多个分支的提交触发的(它是git存储库)。 I would like to use the branch name inside build process to copy results to the right place. 我想在构建过程中使用分支名称将结果复制到正确的位置。 How can I do this? 我怎样才能做到这一点?

Make your own CodeActivity Like this... 像这样制作自己的CodeActivity ...

you need the IBuildDetail BuildDetail as InArgument. 您需要将IBuildDetail BuildDetail作为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));

There is whole .git folder accessible during the build, so I can look into the file .git/HEAD using PowerShell. 在构建过程中可以访问整个.git文件夹,因此我可以使用PowerShell查看.git/HEAD文件。 There may be only commit hash (detached HEAD regime), and then some more git calls are required to find out where I really am. 可能只有提交哈希(独立的HEAD体制),然后需要更多git调用来找出我的真实位置。 All in all, the other answer is probably cleaner. 总而言之,另一个答案可能更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM