我做了一些调查并使用了 TFS 默认提供的功能。说实话,它几乎涵盖了我描述的基本场景。
Git 的默认构建模板包含一个名为Checkout override
:
该字段接受标签名称或您想要构建的修订版本的 ID:
这里的好处是这个设置会覆盖(就像名字所暗示的:))默认分支。我的意思是,如果您从 master 分支创建了一个标签,但在构建定义的“源”选项卡上指定了另一个分支,则没关系 -Checkout override
优先。
我将尝试调查高级场景(在我的问题中描述)。我想会有一些自定义代码...将在此处发布更新。
2013 年 12 月 23 日更新正如预期的那样,为了能够选择要构建的标签,需要一些自定义代码。我最终创建了一个自定义编辑器并将其分配给Checkout override
场地。因此,没有选项可以粘贴任何修订版本,只能从列表中选择一个标签 - 但这适合我的情况。
因此,首先您应该为字段创建一个自定义编辑器。基本上,创建一个类,继承它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>();
}
带有自定义编辑器的 DLL 必须对 VS 可见(在我的例子中,我只是将程序集放入Common7\IDE\PrivateAssemblies\
我的 VS 安装文件夹)。然后,在字段元数据编辑器中,您应该为所需字段指定自定义编辑器:
现在,如果我们编辑构建定义,或者对新构建进行排队,我们可以从下拉列表中选择必要的标签:
希望这能为您节省一些时间。