Set Output Variable in Azure CLI task on VSTS
Asked Answered
S

4

9

I am getting crazy to achieve this very simple task. I need to set an Output Variable in an Azure CLI task on Visual Studio Team Services, because next task in the Release definition will be executed according to the value of this variable.
I wrote this simple code

call az extension add --name azure-cli-iot-ext
call echo ##vso[task.setvariable variable=iotEdgeExists;]$(az iot hub query -n $(iotHub) -q "select * from devices.modules where devices.deviceId ='$(iotEdge)'")

which works, but not as exepected, in fact when I read the Ouput Variable in the next Azure CLI task and I try to print it on the screen I get the command string instead of the output...

call echo"$(az iot hub query -n <IOT_HUB> -q "select * from devices.modules where devices.deviceId ='<IOT_EDGE>'")"

What am I doing wrong?

Scarlettscarp answered 14/3, 2018 at 16:27 Comment(0)
H
10

Refer to this code 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%"

Mechaflash's answer in How to set commands output as a variable in a batch file

Heirdom answered 15/3, 2018 at 5:47 Comment(3)
Expect it to be very hard to make it xplat though.Publish
When using the second command, it seems that (in my case) an extra double-quote is added at the end of the variable. So I just use echo ##vso[task.setvariable variable=testvar;]%var%Canaveral
With the second method I was able to use this to grab an accessToken to connect to Azure SQL for Dacpac deployment, which is exactly what I needed. I also had the issue described by @StefHeyenrath and had to remove the quotes. For those looking for Dacpac deployment with access token, just pass /at:$(testvar) of whatever in the arguments and use ConnectionString as the Authentication Type dropdown value. In your connection string don't have any user/password or authentication= specified.Mofette
S
14

If using Azure CLI version 2.* , you can use a powershell command rather than batch script wizardry. Microsoft's documentation found here

For example if you needed access token to update azure database it would look like so:

$token= & az account get-access-token --resource=https://database.windows.net --query accessToken
Write-Output("##vso[task.setvariable variable=sqlToken;]$token")

Don't forget to login first if testing locally:

az login

Install Azure cli here

Shumaker answered 14/11, 2019 at 14:32 Comment(2)
To access azure sql you need --resource=https://database.windows.net/ (i.e. with a slash on the end)Fecundate
@MattSullivan that is incorrect buddy. Tested with and without slash both get token successfullyShumaker
H
10

Refer to this code 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%"

Mechaflash's answer in How to set commands output as a variable in a batch file

Heirdom answered 15/3, 2018 at 5:47 Comment(3)
Expect it to be very hard to make it xplat though.Publish
When using the second command, it seems that (in my case) an extra double-quote is added at the end of the variable. So I just use echo ##vso[task.setvariable variable=testvar;]%var%Canaveral
With the second method I was able to use this to grab an accessToken to connect to Azure SQL for Dacpac deployment, which is exactly what I needed. I also had the issue described by @StefHeyenrath and had to remove the quotes. For those looking for Dacpac deployment with access token, just pass /at:$(testvar) of whatever in the arguments and use ConnectionString as the Authentication Type dropdown value. In your connection string don't have any user/password or authentication= specified.Mofette
J
1

Inspired from the answer above but with some variation.

Works in an Azure CLI task on a Hosted Ubuntu agent in Microsoft DevOps as of July 2019.

This example runs an az command to fetch the full resource name of a storage account and sets it in the variable _StorageAccountNameVar for use in another pipeline task.

myvar=`az storage account list -g MyResourceGroup --query "[?contains(name, 'config')].name" -o tsv`
echo $myvar
echo "##vso[task.setvariable variable=_StorageAccountNameVar;]$myvar"
Juna answered 12/7, 2019 at 18:7 Comment(0)
W
0

I've got some trouble (error code 255) with

    FOR /F “tokens=* USEBACKQ” %%F IN (`az account get-access-token --resource=https://database.windows.net/ -query accessToken`) DO (
SET var=%%F
)
echo ##vso[task.setvariable variable=sqlToken;]%var%

So I used this (and that work for me)

for /f "tokens=1 USEBACKQ" %%F in (`az account get-access-token --resource=https://database.windows.net/ --query accessToken`) do echo ##vso[task.setvariable variable=sqltoken;]%%F
Westerman answered 9/1, 2020 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.