简体   繁体   English

无法在 Azure DevOps 管道中运行嵌套的 az cli 命令

[英]Unable to run nested az cli commands in Azure DevOps pipeline

I am trying to run an Azure DevOps Task with nested az cli command.我正在尝试使用嵌套的 az cli 命令运行 Azure DevOps 任务。 I have defined the variables at the beginning of the azure-pipelines.yaml我已经在 azure-pipelines.yaml 开头定义了变量

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  resourceGroup: test1234
  acrName: taerarawrr2.azurecr.io

Within tasks I can access the values by using $(resourceGroup) and $(acrName).在任务中,我可以使用 $(resourceGroup) 和 $(acrName) 访问这些值。 Now I want to run an az cli task which uses these two variables, and saves the output of the command in a new variable (ACR_LOGIN_SERVER), which can be later used in the task.现在我想运行一个使用这两个变量的 az cli 任务,并将命令的 output 保存在一个新变量 (ACR_LOGIN_SERVER) 中,以后可以在任务中使用。

- task: AzureCLI@2
  displayName: Run docker image
  inputs:
    azureSubscription: serviceprincipaltest
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      $ACR_LOGIN_SERVER= $(az acr show --name $(acrName) --resource-group $(resourceGroup) --query "loginServer" --output tsv)
      echo $(ACR_LOGIN_SERVER)

This however fails for some reason.然而,由于某种原因,这失败了。 I guess there is something wrong with the syntax of this nested command.我猜这个嵌套命令的语法有问题。 I've tried using parentheses, $ and quotation marks in various locations but nothing seems to work.我尝试在不同的位置使用括号、$ 和引号,但似乎没有任何效果。 I also didn't manage to find any example how such an inline script should be formatted correctly.我也没有找到任何示例如何正确格式化这样的内联脚本。 Any help would be greatly appreciated.任何帮助将不胜感激。

Here is the link of Azure CLI task .这是Azure CLI 任务的链接。

You have $() around az acr command , which will try to find a variable named like that, which is not there.您在az acr command周围有$() ,它将尝试找到一个这样命名的变量,但该变量不存在。 Pls remove that and use the script like below:请删除它并使用如下脚本:

call {your command}>tmpFile1
set /p myvar= < tmpFile1 
echo "##vso[task.setvariable variable=testvar;]%myvar%"

OR或者

FOR /F "tokens=* USEBACKQ" %%F IN (`{your command}`) DO (
SET var=%%F
)
echo "##vso[task.setvariable variable=testvar;]%var%"

Check out this thread: Set Output Variable in Azure CLI task on VSTS查看此线程: 在 VSTS 上的 Azure CLI 任务中设置 Output 变量

There are two problems with your script你的脚本有两个问题

  • The command substituion using $(command) does not work in Azure DevOps as it tries to replace that with a variable.使用$(command)命令替换在 Azure DevOps 中不起作用,因为它试图用变量替换它。 use the backtick notation Instead: `command`使用反引号代替: `command`
  • When assigning a variable in bash, you should not prefix the name with $ , that is for referencing.在 bash 中分配变量时,不应在名称前加上$ ,这是为了引用。
ACR_LOGIN_SERVER=`az acr show --name $(acrName) --resource-group $(resourceGroup) --query "loginServer" --output tsv`

echo $ACR_LOGIN_SERVER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM