gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:02:09 25 4
gpt4 key购买 nike

尝试通过 YAML 管道中的 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

我打开了一个issue

与此同时,我尝试运行 commands to install the necessary pieces via scripts

   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.

我打开了一个issue with the documentation team至少提供的步骤不起作用。如有任何帮助,我们将不胜感激!

最佳答案

我遇到了同样的错误,并且能够通过添加以下内容使我的工作正常:

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

...

关于azure-devops - 无法在 Azure DevOps 任务中运行 az 管道命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66039377/

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