-1

Can anyone help me to understand the below query,Actually in our tfs we have huge number of build definition those are not in use so far hence i want to delete old build definition by seraching all the builds those are older more than one year.

how to search build definitions in TFS2013 those are no longer in use??

Please help.

Thanks,

Dhurva
  • 41
  • 1
  • 6

1 Answers1

0

You can query out the definitions those are older more than one year by this query in SQL Server.

 SELECT [DefinitionId],[DefinitionVersion],[DefinitionName]   FROM [Tfs_CollectionLC].[Build].[tbl_Definition]
 WHERE [CreatedOn] < '2016-06-12 00:00:00.000'

But for the definitions are no longer in use, you have to get the last build for each definition and check whether the build FinishTime is before a year ago (Where [FinishTime] < '2016-06-12 00:00:00.000' ).

You can use TFS API/Client API to get the build details which including the build FinishTime, then filter them accordingly.You can reference below code which enumerates each team project and gets the latest build status for each of the definitions:

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080"));

var vcs = tfs.GetService<VersionControlServer>();

var teamProjects = vcs.GetAllTeamProjects(true);

IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));

foreach (TeamProject proj in teamProjects)
{
    var defs = buildServer.QueryBuildDefinitions(proj.Name);

    System.Console.WriteLine(string.Format("Team Project: {0}", proj.Name));

    foreach(IBuildDefinition def in defs)
    {
        IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec(proj.Name, def.Name);
        spec.MaxBuildsPerDefinition = 1;
        spec.QueryOrder = BuildQueryOrder.FinishTimeDescending;

        var builds = buildServer.QueryBuilds(spec);

        if (builds.Builds.Length > 0)
        {
            var buildDetail = builds.Builds[0];

            System.Console.WriteLine(string.Format("   {0} - {1} - {2}", def.Name, buildDetail.Status.ToString(), buildDetail.FinishTime));
        }                
    }

    System.Console.WriteLine();
}

Reference this thread:TFS API - How to query builds independent of which build definition they belong to

Update: Based on @Dhurva's comments below:

We can also use the utility from github TFS Manager that finds all build definition from team project: https://github.com/jelledruyts/TfsTeamProjectManager

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Thanks a lot Mate..but i have found one utility from github TFS Manager that finds all build definition from team project.. – Dhurva Jun 16 '17 at 06:54
  • @Dhurva Glad to know that you have resolved the issue by the utility. Could you please share the link of the utility and the workaround to achieve the requirement here, then Mark it as Answer? As this can be beneficial to other community members reading this thread – Andy Li-MSFT Jun 30 '17 at 06:54
  • Sure @Andy. Please find the link of this Utility. https://github.com/jelledruyts/TfsTeamProjectManager. Simple download this this code from git hub & build it & use this utility. Thanks. – Dhurva Jun 30 '17 at 10:15
  • @Dhurva Thank you for sharing, I have updated the answer above, you can and [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), This can be beneficial to other community members reading this thread. – Andy Li-MSFT Jul 05 '17 at 02:28