gpt4 book ai didi

c# - 使用 VSTS/TFS 的 .NET 客户端库检索构建定义的任务列表

转载 作者:行者123 更新时间:2023-12-02 03:29:31 35 4
gpt4 key购买 nike

我正在使用 VSTS/TFS 的 .NET 客户端库 ( https://learn.microsoft.com/en-us/vsts/integrate/concepts/dotnet-client-libraries?view=vsts ) 来检索所有团队项目的所有构建定义的任务列表。我正在使用 NuGet 包 Microsoft.TeamFoundation.ExtendedClient 的 v16.139.0 预览版(我需要这样做,因为我还需要检索发布定义工作流程,为此您需要 Microsoft.VisualStudio.Services.Release.Client,它有一个ExtendedClient 的依赖性要求)。服务器(本地)是 TFS 2017.2。我无论如何都无法检索任务/阶段/流程。这是我的代码:

VssConnection connection = new VssConnection(new Uri("http://tfsserver:8080/tfs/defaultcollection"), new VssClientCredentials());
ProjectHttpClient projectClient = connection.GetClient<ProjectHttpClient>();
IEnumerable<TeamProjectReference> projects = projectClient.GetProjects().Result;
BuildHttpClient buildClient = connection.GetClient<BuildHttpClient>();
foreach (var project in projects)
{
IPagedList<BuildDefinition> buildDefinitions = buildClient.GetFullDefinitionsAsync2(project: project.Name, name: null).Result;
foreach (BuildDefinition buildDefinition in buildDefinitions)
{
// get the tasks
}
}

有人知道如何解决这个问题吗?

最佳答案

只需使用 .NET 客户端库尝试下面的 C# 示例,在 TFS 2017.3VSTS 上进行测试,两者都可以工作。 (我这边没有 TFS 2017.2,如果我没记错的话,TFS 2017.2 具有与 TFS 2015 类似的构建过程,它没有“Process”和“phases”属性.)

using System;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;

namespace RetrieveTaskList
{
class Program
{
static void Main(string[] args)
{
//For TFS :
var tfsUrl = "http://ws-tfs2017:8080/tfs/DefaultCollection";
var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssAadCredential());

//For VSTS:
//var tfsUrl = "https://{account}.visualstudio.com";
//var buildClient = new BuildHttpClient(new Uri(tfsUrl), new VssBasicCredential(string.Empty, "PAT here"));

var definitions = buildClient.GetFullDefinitionsAsync(project: "ScrumProject");

foreach (var definition in definitions.Result)
{
Console.WriteLine(string.Format("\n {0} - {1}:", definition.Id, definition.Name));

// Get BuildDefinitionStep to array, each of which has a task property that contains things like the name of the task and the inputs.
var tasks = definition.Steps.ToArray();

//Get each step/task from the array
foreach (var task in tasks)
{
Console.WriteLine(task.DisplayName);
}
}
Console.ReadLine();
}
}
}

enter image description here


您还可以使用REST API从构建定义中检索任务列表。

例如 PowerShell:

Param(
[string]$baseurl = "http://server:8080/tfs/DefaultCollection",
[string]$projectName = "ProjectName",
[string]$buildDefinitionID = "26",
[string]$user = "domain\user",
[string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


$uri = "$baseurl/$($projectName)/_apis/build/definitions/$buildDefinitionID"
Write-Host $uri
$result = (Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
$tasks = $result.process.phases.steps.displayName

foreach ($task in $tasks)
{
write-host $task
}

enter image description here

您还可以尝试 REST 客户端,请引用此线程:Retrieve VSTS/TFS Build task name list

关于c# - 使用 VSTS/TFS 的 .NET 客户端库检索构建定义的任务列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52205058/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com