简体   繁体   English

无法在 DevOps Release Pipeline 中使用 Azure CLI 设置变量

[英]Can't set variable using Azure CLI within DevOps Release Pipeline

I'm trying to save a key value to pass on to the next step in my release pipeline, but no matter what I do I can't save the result of my command to a variable.我正在尝试保存一个键值以传递到我的发布管道中的下一步,但无论我做什么,我都无法将命令的结果保存到变量中。 I've already checked many of the articles here dealing with this with no success.我已经检查了这里的许多文章,但没有成功。 Here is what I am trying:这是我正在尝试的:

$KEY=(az storage account show-connection-string --key primary -n myStorageAccount -g myResourceGroup --query "connectionString" -o tsv)

echo "Attempting to set variable"
echo $KEY
echo ##vso[task.setvariable variable=AZURE_STORAGE_CONNECTION_STRING;]$KEY
echo $AZURE_STORAGE_CONNECTION_STRING

Running on Windows Agent by the way.顺便说一下,在Windows Agent上运行。 I've tried all kinds of variations: SET KEY=, SET $KEY=, SET $(KEY)=, $KEY=, $(KEY)=, KEY=, none of it works.我尝试了各种变体:SET KEY=, SET $KEY=, SET $(KEY)=, $KEY=, $(KEY)=, KEY=,没有一个有效。 Likewise I've tried referencing the variable differently in the echo statements with no luck.同样,我尝试在 echo 语句中以不同的方式引用变量,但没有运气。 If I just run the az storage account command, I do get back the connection string.如果我只运行az storage account命令,我会返回连接字符串。 But either I get that $KEY is not a recognizeable command or if I'm using SET, echo simply gives me back $KEY and the vso line gives me nothing.但是,要么我发现$KEY is not a recognizeable command要么如果我使用的是 SET,echo 只会将$KEY返回给我,而 vso 行却什么也没给我。

I can accomplish most of this, including saving to variable, in Azure Cloud Shell (via syntax $KEY= and echo $KEY ).我可以在 Azure Cloud Shell 中完成大部分工作,包括保存到变量(通过语法$KEY=echo $KEY )。 But of course that doesn't help my pipeline.但这当然对我的管道没有帮助。 Any idea the proper syntax to get this value into my next release pipeline step, or is the another method to accomplish this?知道将这个值放入我的下一个发布管道步骤的正确语法,还是另一种方法来实现这一点?

Can't set variable using Azure CLI within DevOps Release Pipeline无法在 DevOps Release Pipeline 中使用 Azure CLI 设置变量

If you are using Azure CLI version 1.* , try to use following scripts:如果您使用的是Azure CLI 版本 1.* ,请尝试使用以下脚本:

for /f "tokens=1 USEBACKQ" %%F in (`Yourcommand`) do echo ##vso[task.setvariable variable=AZURE_STORAGE_CONNECTION_STRING;]%%F

If you are using Azure CLI version 2.* , you can also use a powershell command:如果使用的是Azure CLI 版本 2.* ,还可以使用 powershell 命令:

$KEY= & YourCommand
Write-Output("##vso[task.setvariable variable=AZURE_STORAGE_CONNECTION_STRING;]$KEY")

在此处输入图片说明

Check this thread for some more details.检查此线程以获取更多详细信息。

Hope this helps.希望这可以帮助。

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

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