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

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

问题描述

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

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

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

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?

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

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...

提前致谢.

推荐答案

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

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.

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

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

该字段接受标签名称,或者只是您想要构建的修订的 ID:

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

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

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.

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

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.

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

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").

从特定 TFS 团队项目的特定 Git 存储库获取标签列表的有用代码如下:

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>();
}

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

The DLL with the custom editor must be made visible to VS (in my case, I just put the assembly to the Common7IDEPrivateAssemblies 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天全站免登陆