gpt4 book ai didi

azure - 如何知道管道使用的是 Microsoft 托管代理还是自托管代理

转载 作者:行者123 更新时间:2023-12-03 00:19:06 25 4
gpt4 key购买 nike

在运行 azure 构建管道(yml 版本)时,是否有任何编程方式(在管道本身内部)来了解当前管道是否在 ms 托管代理或自托管代理上运行?

我们有一个名为“Agent.Name”的预定义变量,它给出代理名称。

但它不断更改代理名称(托管、Azure)Agent.Name=托管,Agent.Name=Azure

有什么方法可以确定管道运行时代理的类型。

      - task: Bash@3
displayName: Show Agent Name
inputs:
targetType: 'inline'
script: |
echo $(Agent.Name)

最佳答案

没有内置功能可以满足您的要求。

但是我们可以“for-each”“azure pipeline 代理池”来获取其中的所有 Microsoft 代理名称。然后比较。

trigger:
- none

# pool:
# name: VMAS

# 1
stages:
- stage: s1
displayName: get the Microsoft host agents' names
jobs:
- job: testJob
steps:
- task: PowerShell@2
name: setvar
inputs:
targetType: 'inline'
script: |
# logic here. For example you get the vars and put it into this format:
$PAT = "<Your Personal Access Token>"
$org_name = "<Your Organization Name>"
$pool_id = <The microsoft hosted agent pool id>
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic "+$PAT)

$url = "https://dev.azure.com/"+$org_name+"/_apis/distributedtask/pools/"+$pool_id+"/agents?api-version=6.0"

$response = Invoke-RestMethod $url -Method 'GET' -Headers $headers
$response | ConvertTo-Json


$str = "";

foreach ($item in $response.value) {
$str += $item.name
$str += ","
}

Write-Host $str

Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$str"


# 2
- stage: s2
displayName: check whether current agent name is one of the above
dependsOn: s1
variables:
vars: $[ stageDependencies.s1.testJob.outputs['setvar.outputvars'] ]
jobs:
- job:
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$varsArr = "$(vars)".Split(',')
$microsofthostagent = 0
foreach ($var in $varsArr)
{
if ($var -eq "$(Agent.Name)")
{
$microsofthostagent = 1
}
else
{
}

}
if( $microsofthostagent -eq 1){
Write-Host "This pipeline is based on Microsoft Host Agent."
}else{
Write-Host "This pipeline is based on Self Host Agent."
}

默认情况下,自主机代理不会与 Microsoft 主机代理同名。

您只需注意不要将自主机代理命名为与 Microsoft 代理池中的代理相同的名称(例如“托管代理”、“Azure Pipelines”)

在我这边,它有效:

Microsoft 托管代理

enter image description here

自托管代理

enter image description here

关于azure - 如何知道管道使用的是 Microsoft 托管代理还是自托管代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73228838/

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