Azure Devops-按代理池ID获取发布定义 [英] Azure Devops - Get release definitions by agent pool ID

查看:192
本文介绍了Azure Devops-按代理池ID获取发布定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to find all the builds and releases that are configured to use a specific agent pool, using the .NET Client Libraries.

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

Assuming agentPoolId, I can get all the build definitions like this:

// _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);
}

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

But I couldn't find a way to find the release definitions that have one or more environments configured to use a particular agent. Any suggestion?

推荐答案

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

Found a solution, many thanks to @amit-baranes for pointing me in the right direction.

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

I've changed his code sample to use the await keyword instead of using .Result, and use .OfType<DeploymentInput>() instead of .Cast<DeploymentInput>() (it was throwing some exceptions).

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

Oh, and the most important thing I've learned: agent pool ID and queue ID are different things!!! If you intend to use the agent pool ID to get the release definitions you'll need to get the correspondent agent queue.

代码示例:

// 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>();
}

这篇关于Azure Devops-按代理池ID获取发布定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆