git - 如何设置 TFS 2013 构建定义以从 Git 标签构建?

标签 git tfs tfsbuild git-tag

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

那么,假设我有一个名为 v1.0 的标签。我希望此构建定义提取与该标记对应的源并运行构建。触发器现在并不重要——它甚至可以是手动的。这怎么可能?

我可以看到您只有在“源设置”选项卡上选择分支的选项...

考虑高级场景:创建新标签时触发构建,并从新创建的标签中获取源以运行构建。那可能吗?如果是,怎么办?

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

提前致谢。

最佳答案

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

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

enter image description here

此字段接受标签名称或您要构建的修订版本的 ID:

enter image description here

这里的好处是这个设置覆盖了(就像名字暗示的那样:))默认分支。我的意思是,如果您从 master 分支创建了一个标签,但在构建定义的 Source 选项卡上指定了另一个分支,那没关系 - Checkout override 优先。

我将尝试研究高级场景(在我的问题中描述)。我想会有一些自定义代码...将在此处发布更新。

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

因此,首先您应该为一个字段创建一个自定义编辑器。基本上,创建一个类,从 System.Drawing.Design.UITypeEditor 类继承它并覆盖几个方法。 This walkthrough帮助很大,还有this book (第 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>();
}

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

enter image description here

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

enter image description here

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

关于git - 如何设置 TFS 2013 构建定义以从 Git 标签构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19434527/

相关文章:

git - 在 Git 中的根提交之前插入一个提交?

eclipse - 在 Eclipse IDE 的 EGit 中提交时选择较早的提交消息?

mysql - 如何在 WordPress 上设置网站更新(开发 -> 登台 -> 生产)?

git - 使现有的 Git 分支跟踪远程分支?

tfs - Visual Studio Online Team Build 上的 TFS 映射中的变量

tfs - 将来自另一个解决方案的 wixlibs 与 TFS 构建一起使用

tfs - 基本的 Team Foundation 服务器问题

tfs - 有没有办法通过一次操作从 TFS 中的各个文件夹中 check out 多个文件

msbuild - 在 ASP.NET 5 的 Visual Studio Online 构建期间未定义或导入预定义类型 'System.Void'

tfs - 使用 "The shelveset ... could not be found for check-in"的前提门控登机失败