简体   繁体   English

如何设置TFS 2013构建定义以从Git标记构建?

[英]How to setup TFS 2013 build definition to build from Git tag?

I want to create a special build definition in TFS 2013 to build from tag. 我想在TFS 2013中创建一个特殊的构建定义,以便从标记构建。 The source control used in that project is Git. 该项目中使用的源代码控制是Git。

So, let's say I have a tag called v1.0 . 所以,假设我有一个名为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... 我可以看到你只能在Source Settings选项卡上选择分支...

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. 除了在MSDN上解释的普通默认方案之外,我找不到任何信息。 Probably, because the configuration (TFS 2013 in Git mode) is quite new... 可能是因为配置(Git模式下的TFS 2013)很新......

Thanks in advance. 提前致谢。

I have done some investigations and played with what TFS offers by default. 我做了一些调查,并使用了TFS默认提供的功能。 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 : Git的默认构建模板包含一个名为Checkout override的构建参数:

在此输入图像描述

This field accepts either tag name, or simply ID of the revision you'd like to build from: 此字段接受标记名称,或者只是您要构建的修订版ID:

在此输入图像描述

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. 我的意思是,如果您从master分支创建了一个标记,但在构建定义的Source选项卡上指定了另一个分支,则无关紧要 - Checkout override优先。

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. 更新2013年12月23日正如预期的那样,为了能够选择要构建的标记,需要一些自定义代码。 I ended up creating a custom editor and assigning it to the Checkout override field. 我最终创建了一个自定义编辑器并将其分配给Checkout override字段。 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. 因此,没有选项可以粘贴任何修订版ID,只选择列表中的标记 - 但这对我的情况来说很好。

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. 基本上,创建一个类,从System.Drawing.Design.UITypeEditor类继承它并覆盖几个方法。 This walkthrough helped a lot, as well as this book (Chapter 18 "Customizing the Build Process"). 本演练以及本书 (第18章“自定义构建过程”)提供了很多帮助。

The useful code which gets the list of tags from a specific Git repo of a specific TFS team project is here: 获取特定TFS团队项目的特定Git仓库中的标签列表的有用代码如下:

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). 具有自定义编辑器的DLL必须对VS可见(在我的例子中,我只是将程序集放到我的VS安装的Common7\\IDE\\PrivateAssemblies\\文件夹中)。 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. 希望这能为您节省一些时间。

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

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