无法在Azure DevOps任务中运行az管道命令



尝试在YAML Pipeline中通过Azure DevOps任务动态检索变量组中的所有变量。最初尝试使用以下代码利用AzureCLI@2任务来检索variableGroupID,该变量将用于获取其内部的变量:

$variableGroupId = $(az pipelines variable-group list --org $(System.CollectionUri) --project $(System.TeamProject) --query "[?name=='{{ parameters.variableGroupName }}'].id" -o tsv)

此命令在本地工作,但在MS托管代理上执行时不起作用,如下所示:

parameters:
variableGroupName: ''
steps:
- task: AzureCLI@2
displayName: Azure CLI
inputs:
azureSubscription: ${{ parameters.azureSubscriptionName }}
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
az upgrade
$variableGroupId = $(az pipelines variable-group list --org $(System.CollectionUri) --project $(System.TeamProject) --query "[?name=='{{ parameters.variableGroupName }}'].id" -o tsv)
write-Host $variableGroupId
$variables = $(az pipelines variable-group variable list --group-id $variableGroupId  --org $(System.CollectionUri)  --project $(System.TeamProject) -o yaml)
write-Host $variables

失败,报错:

Before you can run Azure DevOps commands, you need to run the login command (az login if using AAD/MSA identity else az devops login if using PAT token) to setup credentials. Please see https://aka.ms/azure-devops-cli-auth for more information

我提出了一个问题

同时,我尝试通过脚本运行命令来安装必要的部分

strategy:
runOnce:
deploy:
steps:
- task: AzureRmWebAppDeployment@3
inputs:
azureSubscription: Example - Dev
WebAppName: wapp-Example-dev-eus
Package: $(Pipeline.Workspace)/drop/Web.Example.zip
TakeAppOfflineFlag: True
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
architecture: 'x64'
- task: CmdLine@2
displayName: 'Upgrade pip'
inputs:
script: python -m pip install --upgrade pip
- task: CmdLine@2
displayName: 'upgrade azure cli'
inputs:
script: pip install --pre azure-cli --extra-index-url https://azurecliprod.blob.core.windows.net/edge
- task: CmdLine@2
displayName: 'Show Azure CLI version'
inputs:
script: az --version
- task: CmdLine@2
displayName: 'Install Azure DevOps Extension'
inputs:
script: az extension add -n azure-devops
- task: CmdLine@2
env:
AZURE_DEVOPS_CLI_PAT: $(patCredential)
displayName: 'Login Azure DevOps Extension'
inputs:
script: echo ${AZURE_DEVOPS_CLI_PAT} | az devops login
- task: CmdLine@2
displayName: 'Show List of Variables'
inputs:
script: |
$variableGroupId = $(az pipelines variable-group list --org $(System.CollectionUri) --project $(System.TeamProject) --query "[?name=='{{ parameters.variableGroupName }}'].id" -o tsv)
write-Host $variableGroupId
$variables = $(az pipelines variable-group variable list --group-id $variableGroupId  --org $(System.CollectionUri)  --project $(System.TeamProject) -o yaml)
write-Host $variables

然而,当使用最新的Ubuntu代理和文档中指定的代理时,得到一个错误:

WARNING: Failed to store PAT using keyring; falling back to file storage.
WARNING: You can clear the stored credential by running az devops logout.
WARNING: Refer https://aka.ms/azure-devops-cli-auth to know more on sign in with PAT.

我已经向文档团队提出了一个问题,因为至少提供的步骤不起作用。任何帮助将不胜感激!

我得到了同样的错误,并且能够通过添加:

来获得我的工作
echo $(System.AccessToken) | az devops login

放到内联脚本的顶部。下面是它的样子:

variables:
variableGroupName: 'my-variable-group'
...
- task: AzureCLI@2
displayName: 'Set environment variables'
inputs:
azureSubscription: '$(azureSubscription)'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
echo $(System.AccessToken) | az devops login
$groupId = (az pipelines variable-group list `
--organization $(System.CollectionUri) `
--project $(System.TeamProject) `
--group-name $(variableGroupName) | ConvertFrom-Json).id

...

您可以使用REST API而不是Azure CLI来获取信息。它可以与Microsoft Hosted代理上已有的标准工具一起使用。它只需要普通的powershell或powershell核心,这意味着可以在windows和linux代理上工作。下面的例子成功地在windows-latest/windows-2019ubuntu-latest/ubuntu-20.04上进行了测试

方法与Azure CLI相同。

  1. 列出按名称过滤的所有可用组以检索所讨论的变量组
  2. 使用步骤
  3. 中的变量组id获取变量组中的所有变量

实际上,管道也有一个开箱即用的PAT令牌,可用于对变量组的读访问。它存储在变量System.AccessToken中。使用它而不是手动管理,将进一步简化事情。

下面的脚本是在pwsh步骤中执行的,这是Powershell核心模式下的内置任务

- pwsh: |
# Construct PAT authentication header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "user",$env:SYSTEM_ACCESSTOKEN)))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}

# Retrieve variable group id. Filter the result by setting the groupName query parameter
$variableGroupId = $(Invoke-RestMethod -Headers $headers "$(System.CollectionUri)$(System.TeamProject)/_apis/distributedtask/variablegroups?groupName=${{ parameters.variableGroupName }}&api-version=6.0-preview.2").value[0].id

# Retrieve variables in variable group with id $variableGroupId
$variables = $(Invoke-RestMethod -Headers $headers "$(System.CollectionUri)$(System.TeamProject)/_apis/distributedtask/variablegroups/${variableGroupId}?api-version=6.0-preview.2").variables

#Print variables as json (for demo purpose)
$variables | ConvertTo-Json
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
displayName: 'Retrieve variables'

在一个包含两个变量的变量组的项目中测试上述管道会产生以下输出:

{
"Variable 1": {
"value":  "Value 1"
},
"Variable 2": {
"value":  "Value 2"
}
}

最新更新