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

Yan*_*nko 4 git tfs tfsbuild git-tag tfs2013

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

所以,假设我有一个名为的标签v1.0.我希望此构建定义拉出与该标记对应的源并运行构建.触发器现在无关紧要 - 它甚至可以是手动的.怎么可能?

我可以看到你只能在Source Settings选项卡上选择分支...

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

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

提前致谢.

Yan*_*nko 13

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

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

在此输入图像描述

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

在此输入图像描述

这里的好处是这个设置会覆盖(就像名字一样:) :)默认分支.我的意思是,如果您从master分支创建了一个标记,但在构建定义的Source选项卡上指定了另一个分支,则无关紧要 - Checkout override获取首选项.

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

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

因此,首先应该为字段创建自定义编辑器.基本上,创建一个类,从System.Drawing.Design.UITypeEditor类继承它并覆盖几个方法.本演练以及本书(第18章"自定义构建过程")提供了很多帮助.

获取特定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>();
}
Run Code Online (Sandbox Code Playgroud)

具有自定义编辑器的DLL必须对VS可见(在我的例子中,我只是将程序集放到Common7\IDE\PrivateAssemblies\我的VS安装的文件夹中).然后,在字段元数据编辑器中,您应该为所需字段指定自定义编辑器:

在此输入图像描述

现在,如果我们编辑构建定义或排队新构建,我们可以从下拉列表中选择必要的标记:

在此输入图像描述

希望这能为您节省一些时间.