Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Output Variable in Azure CLI task on VSTS

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?

like image 901
user2297037 Avatar asked Mar 14 '18 16:03

user2297037


People also ask

How do I set variables in Azure CLI?

Use shell variables You can use variables in Bash to pass values for parameters to commands. Using variables with the Azure CLI also allows reuse of commands, either piecemeal or in scripts. This example creates a new storage disk of the same type as the storage disk on an existing virtual machine.

How do you pass variables in Azure pipelines Yml tasks?

Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.

How are output variables used in release pipeline?

Just specify the reference name in the blank of task, and then, you can call this output variable abc by using the format: <reference name>. <variable name> . For example, if you specify the reference name as mycustom , just call the output variable by using $(mycustom. abc) .


2 Answers

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

like image 62
dwp4ge Avatar answered Oct 03 '22 23:10

dwp4ge


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

like image 38
starian chen-MSFT Avatar answered Oct 03 '22 23:10

starian chen-MSFT