如何设置TFS 2013构建定义从Git标记构建? [英] How to setup TFS 2013 build definition to build from Git tag?

查看:293
本文介绍了如何设置TFS 2013构建定义从Git标记构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在TFS 2013中创建一个特殊的构建定义以从标记构建。在这个项目中使用的源代码控制是Git。



所以,假设我有一个名为 v1.0 。我想要这个构建定义拉对应于该标记的源并运行构建。触发器现在无关紧要 - 甚至可能是手动的。这可能如何?



我可以看到你只有一个选项可以选择源设置选项卡上的分支...



考虑高级方案:创建新标记时触发构建,并从新创建的标记中获取源以运行构建。那可能吗?如果是这样,怎么样?



除了在MSDN上解释的普通默认场景外,我找不到任何信息。可能是因为配置(Git模式下的TFS 2013)是相当新的......



感谢您的提前。 h2_lin>解决方案

我已经做了一些调查,并且默认使用TFS提供的功能。说实话,它几乎涵盖了我描述的基本场景。



Git的默认构建模板包含一个名为 Checkout override 的构建参数。 code>:





此字段接受标记名称,或者只是您希望从其构建的修订版的ID:





好东西这里是这个设置覆盖(就像名字建议:))默认分支。我的意思是,如果您从master分支创建了一个标签,但在构建定义的Source选项卡上指定了另一个分支,则无关紧要 - Checkout覆盖会优先选择。



我会尝试调查高级方案(在我的问题中描述)。我想会有一些自定义代码......会在这里发布更新。



更新2013年12月23日正如预期的那样,为了能够选择要构建的标记,需要一些自定义代码。我最终创建了一个自定义编辑器并将其分配给 Checkout覆盖字段。因此,没有选择在其中粘贴任何修订ID,只能从列表中选择一个标签 - 但这对我的情况来说很好。

因此,首先您应该创建一个字段的自定义编辑器。基本上,创建一个类,从 System.Drawing.Design.UITypeEditor 类继承它,并重写一些方法。 本演练帮助了很多,以及本书(第18章定制构建过程)。

获取特定TFS团队项目的特定Git仓库标签列表的有用代码位于以下位置:

 私人列表< string> GetAvailableTags(IServiceProvider provider)
{
//获取当前构建定义对象
var buildDefinition =(IBuildDefinition)provider.GetService(typeof(IBuildDefinition));
//获取构建定义(Git或TFVC)的当前源提供者
var sourceProvider = buildDefinition.GetDefaultSourceProvider();

//获取项目集合
var teamProjectCollection = buildDefinition.BuildServer.TeamProjectCollection;
//获取服务对象以与Git repo进行通信
var gitRepoService = teamProjectCollection.GetService< GitRepositoryService>();

//这将获得Git仓库的部分URL(以< teamproject> /<仓库>的形式)
var repoUrl = sourceProvider.Fields [BuildSourceProviders.GitProperties.RepositoryName ]。

string projectName;
字符串repoName;

//这是将上面获得的部分URL解析为项目名称和回购名称
if(BuildSourceProviders.GitProperties.ParseUniqueRepoName(repoUrl,out projectName,out repoName))
{
//这将获得当前团队项目的所有Git仓库
var source = gitRepoService.QueryRepositories(projectName);
//这将使用当前的Git仓库,我们使用
var repo = source.First(x => x.Name.Equals(repoName,StringComparison.OrdinalIgnoreCase));
//这将获得此Git仓库中的所有标签
var tags = gitRepoService.QueryRefs(repo.Id,tags);

//最后,返回纯标签名称列表
return tags.Select(gitRef => gitRef.Name.Substring(refs / tags /。Length)) .ToList();
}

返回新的List< string>();





$ b

使用自定义编辑器的DLL必须让VS可见(在我的情况下,我只是将程序集放到我的VS安装的 Common7 \IDE\PrivateAssemblies\ 文件夹中)。然后,在字段元数据编辑器中,您应该为所需的字段指定自定义编辑器:





希望这可以节省您一些时间。


I want to create a special build definition in TFS 2013 to build from tag. The source control used in that project is Git.

So, let's say I have a tag called v1.0. I want this build definition to pull the sources corresponding to that tag and run a build. Triggers don't matter for now - it could be even manual. How is that possible?

I can see you only have an option to choose branch on the Source Settings tab...

Consider the advanced scenario: a build is triggered when a new tag is created, and takes the sources from that newly created tag to run a build. Is that possible? If so, how?

I failed to find any information besides plain default scenarios explained on MSDN. Probably, because the configuration (TFS 2013 in Git mode) is quite new...

Thanks in advance.

解决方案

I have done some investigations and played with what TFS offers by default. To be honest, it pretty much covers the basic scenario I described.

The default build template for Git contains a build argument called Checkout override:

This field accepts either tag name, or simply ID of the revision you'd like to build from:

The good thing here is that this setting overrides (just like the name suggests :)) the default branch. I mean, if you created a tag from master branch, but specified another branch on the Source tab of the build definition, it doesn't matter - the Checkout override takes preference.

I'll try to investigate the advanced scenario (described in my question). I suppose there will be some amount of custom code... will post the update here.

UPDATE DEC 23, 2013 As expected, in order to be able to choose the tag to build from, some custom code is required. I ended up creating a custom editor and assigning it to the Checkout override field. As a result, there's no option to paste any revision id there, only choose a tag from the list - but that's fine for my case.

So, first you should create a custom editor for a field. Basically, create a class, inherit it from System.Drawing.Design.UITypeEditor class and override a couple of methods. This walkthrough helped a lot, as well as this book (Chapter 18 "Customizing the Build Process").

The useful code which gets the list of tags from a specific Git repo of a specific TFS team project is here:

private List<string> GetAvailableTags(IServiceProvider provider)
{
  // obtain the current build definition object
  var buildDefinition = (IBuildDefinition)provider.GetService(typeof(IBuildDefinition));
  // obtain the current source provider for the build definition (Git or TFVC)
  var sourceProvider = buildDefinition.GetDefaultSourceProvider();

  // obtain the project collection
  var teamProjectCollection = buildDefinition.BuildServer.TeamProjectCollection;
  // obtain a service object to communicate with Git repo
  var gitRepoService = teamProjectCollection.GetService<GitRepositoryService>();

  // this will get the partial URL of the Git repo (in a form <teamproject>/<repo>)
  var repoUrl = sourceProvider.Fields[BuildSourceProviders.GitProperties.RepositoryName];

  string projectName;
  string repoName;

  // this is the way to parse the partial URL obtained above, into project name and repo name
  if (BuildSourceProviders.GitProperties.ParseUniqueRepoName(repoUrl, out projectName, out repoName))
  {
    // this will get all Git repos of the current team project
    var source = gitRepoService.QueryRepositories(projectName);
    // this will take the current Git repo we work with
    var repo = source.First(x => x.Name.Equals(repoName, StringComparison.OrdinalIgnoreCase));
    // this will get all the tags in this Git repo
    var tags = gitRepoService.QueryRefs(repo.Id, "tags");

    // and finally, the list of pure tag names is returned
    return tags.Select(gitRef => gitRef.Name.Substring("refs/tags/".Length)).ToList();
  }

  return new List<string>();
}

The DLL with the custom editor must be made visible to VS (in my case, I just put the assembly to the Common7\IDE\PrivateAssemblies\ folder of my VS installation). Then, in the field metadata editor you should specify the custom editor for the desired field:

And now if we edit the build definition, or queue a new build, we can select the necessary tag from the dropdown:

Hope this saves you some time.

这篇关于如何设置TFS 2013构建定义从Git标记构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆