簡體   English   中英

通過API刪除TFS中的構建定義

[英]Delete build definitions in TFS via API

通過API創建TFS構建定義時,如果定義預先存在,則需要先刪除:

if (BuildServer.QueryBuildDefinitions(teamProject).Any(d => d.Name == buildDefinitionName))
{
    buildDefinition = BuildServer.GetBuildDefinition(teamProject, buildDefinitionName);
    var builds = BuildServer.QueryBuilds(buildDefinition);
    if (builds != null && builds.Any())
    {
        Console.WriteLine("delete {0} builds for build definition: {1}", builds.Count(), buildDefinition.Name);
        BuildServer.DeleteBuilds(builds);
    }
    if (buildDefinition.Workspace.Mappings.Any())
    {
        var mappings = buildDefinition.Workspace.Mappings.Select(m => m.ServerItem).ToArray();
        foreach (var mapping in mappings)
        {
            Console.WriteLine("remove workspace mapping: {0}", mapping);
            buildDefinition.Workspace.RemoveMapping(mapping);
        }
    }
    Console.WriteLine("delete build definition: {0}", buildDefinition.Name);
    BuildServer.DeleteBuildDefinitions(new[] { buildDefinition });
}

這和后續的一樣:

buildDefinition = BuildServer.CreateBuildDefinition(teamProject);
buildDefinition.Name = buildDefinitionName;

但是,當運行第一個構建時,它會引發有關工作空間沖突的錯誤:

Exception Message: Unable to create the workspace 'some-new-workspace' due to a mapping conflict. You may need to manually delete an old workspace. You can get a list of workspaces on a computer with the command 'tf workspaces /computer:%COMPUTERNAME%'.
Details: The path C:\some-path is already mapped in workspace some-old-workspace. (type MappingConflictException)

如您在第一個代碼段中看到的那樣,我嘗試使用.Workspace.RemoveMapping()刪除工作區沒有任何效果。 工作區仍然存在於構建控制器上。 我可以手動刪除它們,但是當我刪除構建定義時,它們確實應該被刪除。 API中是否還有其他DeleteWorkspace()機制?

更完整的代碼要點在這里: https//gist.github.com/grenade/cce374cb4e27e366bc5b

事實證明,它之所以復雜,是因為構建所創建的各種工作空間的所有者可能是其他用戶(構建代理在其下運行)。 我找到了一種方法,可以依靠先前在工作空間命名約定[build definition id]_[build agent id]_[workspace host]使用的[build definition id]_[build agent id]_[workspace host]

var workspaceNamePrefix = string.Concat(buildDefinition.Id, '_');
var workSpaces = VersionControlServer.QueryWorkspaces(null, null, null).Where(w => w.Name.StartsWith(workspaceNamePrefix)).ToArray();
for (var i = workSpaces.Count() - 1; i > -1; i--)
{
    try
    {
        workSpaces[i].Delete();
        Console.WriteLine("delete workspace: {0}", workSpaces[i].Name);
    }
    catch (ResourceAccessException rae)
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine(rae.Message);
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("workspace needs to be deleted by an administrator using the following command:");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("tf workspace /delete {0};{1}", workSpaces[i].Name, workSpaces[i].OwnerName);
        Console.ResetColor();
    }
}

我已經更新了要點: https : //gist.github.com/grenade/cce374cb4e27e366bc5b

暫無
暫無

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

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