Azure Devops-通过代理池ID获取发布定义

时间:2019-04-05 14:29:14

标签: c# .net azure-devops tfs-sdk

我正在尝试使用.NET客户端库查找配置为使用特定代理程序池的所有构建和发行版。

假设agentPoolId,我可以得到所有这样的构建定义:

// _connection is of type VssConnection
using (var buildClient = _connection.GetClient<BuildHttpClient>())
{
    List<BuildDefinitionReference> allBuilds = await buildClient.GetDefinitionsAsync(projectName, top: 1000, queryOrder: DefinitionQueryOrder.DefinitionNameAscending);
    List<BuildDefinitionReference> builds = allBuilds.Where(x => HasAgentPoolId(x, agentPoolId)).ToList();
}

private bool HasAgentPoolId(BuildDefinitionReference buildDefinition, int agentPoolId)
{
    TaskAgentPoolReference pool = buildDefinition?.Queue?.Pool;

    if (pool == null)
    {
        return false;
    }

    return pool.Id.Equals(agentPoolId);
}

但是我找不到找到具有配置为使用特定代理的一个或多个环境的发行版定义的方法。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我被要求通过Rest Api而不是通过NET Client Libraries通过Agent Pool ID获取所有版本。希望能有所帮助。

C#代码段:

public class ReleaseResponse
{
     [JsonProperty("value")]
     public List<ReleaseItem> Value { get; set; }
}

public class ReleaseItem
{
     [JsonProperty("name")]
     public string Name { get; set; }

     [JsonProperty("Id")]
     public int Id { get; set; }
}

static void Main(string[] args)
{
     string tfsURL = "TFS URL";
     string releaseDefurl = $"{tfsURL}/_apis/release/definitions?$expand=artifacts&api-version=3.2-preview.3";
     const int agentPoolID = "AGENT Pool ID";
     List<string> relevantReleases = new List<string>(); 
     WebClient client = new WebClient();
     client.UseDefaultCredentials = true;
     client.Headers.Add("Content-Type", "application/json");
     var releaseList = client.DownloadString(releaseDefurl);
     var allReleases = JsonConvert.DeserializeObject<ReleaseResponse>(releaseList).Value;
     foreach (var release in allReleases)
     {
           string releaseInfoApi = $"{tfsURL}/_apis/Release/definitions/{release.Id}";
           var getReleseInfo = client.DownloadString(releaseInfoApi);
           var releaseInfo = JsonConvert.DeserializeObject<TFSLogic.RootObject>(getReleseInfo);
           var deploymentAgents = releaseInfo.environments.ToList().Where(e => e.deployPhases.FirstOrDefault().deploymentInput.queueId == agentPoolID).Count();
           if (deploymentAgents > 0)
           {
               relevantReleases.Add(release.Name);
           }

     }

}

在此处找到TFSLogic:https://codebeautify.org/online-json-editor/cb7aa0d9

Powershell代码段:

$tfsUrl = "TFS URL"
$releaseDefurl = $tfsUrl + '/_apis/release/definitions?$expand=artifacts&api-version=3.2-preview.3'
$agentPoolID = "Agent Pool ID"
$relevantReleases = @();
$allReleasesID = (Invoke-RestMethod -Uri ($releaseDefurl) -Method Get -UseDefaultCredentials).value.id

function getReleaseByAgentPoolID($releaseID,$agentPoolID)
{
    $ReleaseInfo = Invoke-RestMethod -Uri "$tfsUrl/_apis/Release/definitions/$releaseID" -Method Get -UseDefaultCredentials

    $deploymentAgents = $ReleaseInfo.environments | % {$_.deployPhases.deploymentInput.queueId} | where {$_ -eq $agentPoolID}

    if($deploymentAgents.Count -gt 0)
    {
        return $ReleaseInfo.name
    }
}


foreach ($releaseID in $allReleasesID)
{
    $relevantReleases += getReleaseByAgentPoolID -releaseID $releaseID -agentPoolID $agentPoolID   
}

更新:

我花了一些时间,但是我能够通过azure-devops-dotnet-samples实现这一目标 我希望这个例子最终是您想要的。

using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Linq;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts;
using Microsoft.VisualStudio.Services.Common;
using System.Collections.Generic;

namespace FindReleaseByAgentPoolID
{
    class Program
    {
        const int agentPoolID = 999;
        static void Main(string[] args)
        {
            var relevantReleases = new List<string>();
            VssCredentials c = new VssCredentials(new WindowsCredential(System.Net.CredentialCache.DefaultNetworkCredentials));
            var tfsURL = new Uri("TFS URL");
            var teamProjectName = "PROJECT";

            using (var connection = new VssConnection(tfsURL, c))
            using (var rmClient = connection.GetClient<ReleaseHttpClient2>())
            {
                var releases = rmClient
                    .GetReleaseDefinitionsAsync(teamProjectName, string.Empty, ReleaseDefinitionExpands.Environments)
                    .Result.ToArray();

                foreach (var release in releases)
                {
                    var r = rmClient.GetReleaseDefinitionAsync(teamProjectName, release.Id);
                    var deploymentAgents = r.Result.Environments.SelectMany(e =>
                     e.DeployPhases.Select(dp =>
                     dp.GetDeploymentInput()).Cast<DeploymentInput>()).Where(di =>
                     di.QueueId == agentPoolID).Count();

                    if (deploymentAgents > 0)
                    {
                        relevantReleases.Add(release.Name);
                    }
                }
            }
        }
    }
}

答案 1 :(得分:0)

找到了一个解决方案,非常感谢@ amit-baranes为我指明了正确的方向。

我将他的代码示例更改为使用await关键字而不是.Result,并使用.OfType<DeploymentInput>()而不是.Cast<DeploymentInput>()(这引发了一些异常)。

哦,最重要的一点是我学到的:代理程序池ID和队列ID是不同的!! 如果您打算使用代理程序池ID来获取发行版定义,将需要获取代理代理队列。

代码示例:

// set agent pool Id and project name
int agentPoolId = 123456; 
string teamProjectName = ".....";

// _connection is of type VssConnection
using (var taskAgentClient = _connection.GetClient<TaskAgentHttpClient>())
using (var releaseClient = _connection.GetClient<ReleaseHttpClient2>())
{
    // please note: agent pool Id != queue Id
    // agent pool id is used to get the build definitions
    // queue Id is used to get the release definitions
    TaskAgentPool agentPool = await taskAgentClient.GetAgentPoolAsync(agentPoolId);
    List<TaskAgentQueue> queues = await taskAgentClient.GetAgentQueuesByNamesAsync(teamProjectName, queueNames: new[] { agentPool.Name });
    TaskAgentQueue queue = queues.FirstOrDefault();

    List<ReleaseDefinition> definitions = await releaseClient.GetReleaseDefinitionsAsync(teamProjectName, string.Empty, ReleaseDefinitionExpands.Environments);

    foreach (ReleaseDefinition definition in definitions)
    {
        var fullDefinition = await releaseClient.GetReleaseDefinitionAsync(teamProjectName, definition.Id);

        bool hasReleasesWithPool = fullDefinition.Environments.SelectMany(GetDeploymentInputs)
                                                              .Any(di => di.QueueId == queue.Id);

        if (hasReleasesWithPool)
        {
            Debug.WriteLine($"{definition.Name}");
        }
    }
}

private IEnumerable<DeploymentInput> GetDeploymentInputs(ReleaseDefinitionEnvironment environment)
{
    return environment.DeployPhases.Select(dp => dp.GetDeploymentInput())
                                   .OfType<DeploymentInput>();
}