首页 > 解决方案 > 将输出保存到变量在 Azure-CLI DevOps 任务中不起作用

问题描述

尝试将 azure advisor 推荐的输出保存到变量中,以便我可以传递到下一个任务。

但是,无论我尝试什么语法,相信我已经尝试了所有可能的组合,变量都不会被保存。有趣的是,这些在 Cloud shell (bash) 中为 Eg 工作

rgName="$(az group list --query "[?tags.Test=='yes'].name" --output tsv)" 
az group show -n $rgName

这在云壳中工作得很好。但不在 DevOps azurecli 任务中。我还提到了堆栈溢出本身给出的多个示例,但它们都不起作用。使用任务版本 1.*

rgName' is not recognised as an internal or external command,

有人可以举一个 DevOps Task 的工作示例吗?

注意:顺便说一句,我使用 cli 的全部原因是因为找不到 Advisor RM 模块,并且 Az 模块无法在任务版本 4 中正确加载。*

标签: azureazure-devopsazure-cliazure-cli2

解决方案


As Shayki mentioned above, task.setvariable can help with setting a variable from a script. The same has been detailed in this document. In a nutshell, you would have to do this:

rgName=$(az group list --query "[?tags.Test=='yes'].name" -o tsv | tr '\n' ' ')
echo "##vso[task.setvariable variable=RESULT]$rgName"

The task.setvariable is a logging command and does not update the environment variables, but it does make the new variable available to downstream steps within the same job. Notice that the results are separated by CRLF and not spaces, and hence the trimming tr '\n' ' '. Now, in the subsequent tasks where you need the variable, you may use it in this way:

echo "Result: $(RESULT)"

Refer to this blog to get a detailed walkthrough. Hope this helps!


推荐阅读