Set Output Variable in Azure CLI task on VSTS

前端 未结 4 1403
终归单人心
终归单人心 2020-12-07 01:48

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 d

相关标签:
4条回答
  • 2020-12-07 02:28

    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

    0 讨论(0)
  • 2020-12-07 02:41

    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")
    
    0 讨论(0)
  • 2020-12-07 02:44

    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
    
    0 讨论(0)
  • 2020-12-07 02:52

    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"
    
    0 讨论(0)
提交回复
热议问题