0
votes

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. I've tried all kinds of variations: SET KEY=, SET $KEY=, SET $(KEY)=, $KEY=, $(KEY)=, KEY=, none of it works. Likewise I've tried referencing the variable differently in the echo statements with no luck. If I just run the az storage account command, I do get back the connection string. 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.

I can accomplish most of this, including saving to variable, in Azure Cloud Shell (via syntax $KEY= and 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?

1
Looks like you're mixing Batch and Powershell syntax.Powershell is probably easiestjessehouwing
Does the PowerShell task log you in to Azure CLI though? I thought I tried that previously and it didn't log in. Azure CLI takes care of that automatically so I'd like to use that if possible. How can I figure out the correct Batch syntax?billoverton
To pass it into' echo %key%jessehouwing
Not sure whether the Azure Powershell task logs az in as well.jessehouwing

1 Answers

1
votes

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

If you are using Azure CLI version 1.*, try to use following scripts:

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:

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

enter image description here

Check this thread for some more details.

Hope this helps.