首页 > 解决方案 > 使用 QueryBuilds 从 TFS 查询构建的性能非常低

问题描述

BuildDetail.KeepForever我正在尝试访问具有“无限期保留”标志(属性)的本地 TFS 2015 服务器中的每个构建,但该QueryBuilds()函数需要很长时间才能获取所有构建。

TFS 2015“无限期保留”Web GUI 菜单选项

我真正需要的是KeepForeverDropLocation属性。

我发现您可以使用该IBuildDetailSpec界面获得更高的效率,但我找不到我将获得该KeepForever属性的选项。

我当前的代码片段:

public void BackupOnlyRetainedBuilds(string TeamProjectName, string DestinationPath)
{
    defs[...]

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

    //get builds server
    buildServer = (IBuildServer)server.GetService(typeof(IBuildServer));

    //set up an array of all build definition from a spcific team project.
    IBuildDefinition[] bda = buildServer.QueryBuildDefinitions(TeamProjectName);

    //check each build definition.
    foreach (var buildDefinition in bda)
    {
        //set an array of builds history.
        IBuildDetail[] bha = buildDefinition.QueryBuilds();

        //check each build from build history build details.
        foreach (var buildDetails in bha)
        {
            //check if build is retained.
            if (buildDetails.KeepForever == true)
            {
                string dropLocationPath = buildDetails.DropLocation;

                //check if drop folder exists.
                if (Directory.Exists(dropLocationPath))
                {
                    //create all of the directories.
                    foreach (string dirPath in Directory.GetDirectories(dropLocationPath, "*",
                        SearchOption.AllDirectories))
                        Directory.CreateDirectory(dirPath.Replace(dropLocationPath, DestinationPath));

                    //copy all the files & Replaces any files with the same name.
                    foreach (string newPath in Directory.GetFiles(dropLocationPath, "*.*",
                        SearchOption.AllDirectories))
                        File.Copy(newPath, newPath.Replace(dropLocationPath, DestinationPath), true);
                }
            }
        }
    }
}

标签: c#tfsbuildazure-devopsazure-pipelines

解决方案


您可以告诉QueryBuilds不要从我的修补版本中获取所有构建细节,而只获取您感兴趣的那些tfsbuild.exe

buildDetailSpec = this.BuildServer.CreateBuildDetailSpec(...)
buildDetailSpec.QueryDeletedOption = !forDestroy ? QueryDeletedOption.IncludeDeleted : QueryDeletedOption.OnlyDeleted;
buildDetailSpec.InformationTypes = null;
IBuildQueryResult buildQueryResult = this.BuildServer.QueryBuilds(buildDetailSpec);

或者使用QueryBuildsUri并传递它(string[]) null, QueryOptions.None

IBuildDetail[] buildDetailArray = this.BuildServer.QueryBuildsByUri(list2.ToArray(), (string[]) null, QueryOptions.None, QueryDeletedOption.IncludeDeleted);

您可能需要将一组特定InformationTypes的数据传递给它,具体取决于您在备份工具中所需的数据。


推荐阅读