簡體   English   中英

如何設置TFS 2013構建定義以從Git標記構建?

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

我想在TFS 2013中創建一個特殊的構建定義,以便從標記構建。 該項目中使用的源代碼控制是Git。

所以,假設我有一個名為v1.0的標簽。 我希望此構建定義拉出與該標記對應的源並運行構建。 觸發器現在無關緊要 - 它甚至可以是手動的。 怎么可能?

我可以看到你只能在Source Settings選項卡上選擇分支...

考慮高級方案:在創建新標記時觸發構建,並從新創建的標記中獲取源以運行構建。 那可能嗎? 如果是這樣,怎么樣?

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

提前致謝。

我做了一些調查,並使用了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>();
}

具有自定義編輯器的DLL必須對VS可見(在我的例子中,我只是將程序集放到我的VS安裝的Common7\\IDE\\PrivateAssemblies\\文件夾中)。 然后,在字段元數據編輯器中,您應該為所需字段指定自定義編輯器:

在此輸入圖像描述

現在,如果我們編輯構建定義或排隊新構建,我們可以從下拉列表中選擇必要的標記:

在此輸入圖像描述

希望這能為您節省一些時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM