更新
我将在这里提出一个功能提供者作为我帖子的更新。 如果您需要更多详细信息,请告诉我。
我们目前在代理源代码中提供了一堆附带的功能提供程序:
https://github.com/microsoft/azure-pipelines-agent/tree/master/src/Microsoft.VisualStudio.Services.Agent/Capabilities
- 代理
- 环境
- 尼克斯
- PowerShell
建议的是一个名为ExecutableCapabilitiesProvider
的附加提供程序。
这个新的可执行功能提供程序可能有一个可以在代理计算机上编辑的配置文件。 此文件的格式可能是:
#name,executable
pip,pip3 freeze
xyz,/usr/bin/xyz-runner
abc,sh -C "ls -l /blah/blah"
作为自承载池的维护者,我会使用适合我的条目来配置此文件,并让代理在启动时运行它。这样,我就不会对我的能力进行任何硬编码,而是在启动时确定这些值。
我会更进一步,添加一个新的 API 调用来添加功能,这比当前要求名称/值的功能更灵活。例如,将参数更改为Name, Provider, Params
:
efg, NixProvider, /path/to/file/efg
klm, ExecutableCapabilitiesProvider, /usr/bin/klm -a -b -c
原始帖子
我想让我的代理报告新功能,这些功能不是静态的,而是命令或类似内容的结果?我该怎么做? 我们的代理在 Linux 机器上运行。 具体来说,我想要一个名为pip-packages
的新功能,其值是在 shell 上执行pip freeze
命令的结果。
如果要添加用户定义的功能,则可以编写脚本来调用 REST API 以更新代理功能。
PUT https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolid}/agents/{agentid}/usercapabilities?api-version=5.0
Request body:
{"pip-packages": "xxxx"}
例如,您可以设置变量并运行命令pip freeze
并将响应导出为该变量的值,然后通过调用 REST API 更新代理功能:
以下PowerShell示例供您参考:
Param(
[string]$collectionurl = "https://dev.azure.com/{organization}",
[string]$poolid = "14",
[string]$agentid = "16",
[string]$user = "user",
[string]$token = "PAT/Password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
# Run pip freeze command and get response as the value of the variable $pipfreeze (Just for your reference here, you need to extract the value with running the commands)
$pipfreeze = "response of pip freeze"
# Create json body with that value
$baseUri = "$collectionurl/_apis/distributedtask/pools/$poolid/agents/$agentid/usercapabilities?api-version=5.0"
function CreateJsonBody
{
$value = @"
{"pip-packages":"$pipfreeze"}
"@
return $value
}
$json = CreateJsonBody
# Update the Agent user capability
$agentcapability = Invoke-RestMethod -Uri $baseUri -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
write-host "=========================================================="
Write-host "userCapabilities :" $agentcapability.userCapabilities.'pip-packages'
write-host "=========================================================="