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?
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.
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.
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) .
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With