7

I have an azure-pipelines.yml file at the root of my project in git. In this file I would like to use the output from one task in the next task, as a variable. My task

- task: AzureCLI@2
            displayName: 'List info on read policy in DEV'
            name: myOutput
            inputs:
              azureSubscription: mySub
              scriptType: ps
              scriptLocation: inlineScript
              inlineScript: az servicebus topic authorization-rule keys list --resource-group myRG --namespace-name mySB --topic-name myTopic --name Listen

When running this az command in powershell i get this in return:

{
  "aliasPrimaryConnectionString": null,
  "aliasSecondaryConnectionString": null,
  "keyName": "Listen",
  "primaryConnectionString": "Endpoint=sb://someKey",
  "primaryKey": "somePrimaryKey",
  "secondaryConnectionString": "Endpoint=sb://another key",
  "secondaryKey": "someSecondaryKey"
}

I do get this output in the logs in the pipeline as well. I did expect, according to documentation to be able to use this in the next step. For example:

- script: echo $(myOutput.primaryConnectionString)

Instead of getting the value of the primaryConnectionString this is what the log gives me:

Starting: CmdLine
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.151.2
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
echo $(myOutput.primaryConnectionString)
========================== Starting Command Output ===========================
"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:\a\_temp\3d1130bd-f7f7-4e30-8ae2-31e6f9d0800e.cmd""
$(myOutput.primaryConnectionString)
Finishing: CmdLine

Why is the variable name not replaced with the value of primaryConnectionString?

2 Answers2

6

On Linux with shell you can do it like this:

- task: AzureCLI@2
  displayName: 'List info on read policy in DEV'
  name: myOutput
  inputs:
    azureSubscription: mySub
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      output=$(az servicebus topic authorization-rule keys list --resource-group myRG --namespace-name mySB --topic-name myTopic --name Listen)
      echo $output | jq .
      echo "##vso[task.setvariable variable=testvar;isoutput=true]$output"

The echo $output | jq . ensures that the output of the command is still included in the log.

Later the content can be accessed as follows:

- task: Bash@3
  displayName: 'Do something with the variable'
  inputs:
    targetType: 'inline'
    script: |
      echo "$(myOutput.testvar)"
      the_id="$(echo $(myOutput.testvar) | jq -r '.id')"

(Assuming a distribution with jq installed on the agent.)

kap
  • 1,456
  • 2
  • 19
  • 24
3

Because you missed the variable set in your Azure Cli task.

The life cycle of the variable which generated during the specified task execution is only in the task execution phase. This means, once task finished, the variable will disappear. If you want it available for next task, you need to use scripts to make it as an output variable. This is what you missed.

In fact, in the doc you point out, it use context to mention out this:

enter image description here


To set the command output as variable and used by the next task, please use below script:

FOR /F "tokens=* USEBACKQ" %%F IN (`{your command}`) DO (
SET var=%%F
)
echo "##vso[task.setvariable variable=testvar;]%var%"

Or

call {your command}>tmpFile1
set /p myvar= < tmpFile1 
echo "##vso[task.setvariable variable=testvar;]%myvar%"

See this thread: Set Output Variable in Azure CLI task on VSTS. There has another user raised similar requirement.


Update:

Based on the comment, Yes, please input above scripts into input:inlinescript. Like this:

enter image description here

In addition, not sure whether you are familiar with above script, here I modified some changes into those scripts to make it available for you:

SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`call az servicebus topic authorization-rule keys list --resource-group Wicresoft-Support --namespace-name merlin1120 --topic-name mytopic --name myname`) DO (
  SET var!count!=%%F
  SET /a count=!count!+1
)
ECHO %var5%
echo "##vso[task.setvariable variable=testvar;]%var5%"
ENDLOCAL

What you want to passed to next task is primaryConnectionString and its number is 5. So here the value of i is 5.

See the output of mine in the next task:

enter image description here

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • I was assuming that the Azure CLI task actually was setting the output variables based on the json that the az command returns. I guess I was wrong. Could you please share more detail on how I do enter these examples in to the Azure CLI task? Do I fit all of those lines into the input:inlineScript? – Christer Berglund Nov 19 '19 at 17:01
  • @ChristerBerglund, Yes, you should put above script into inline. See my update. – Mengdi Liang Nov 20 '19 at 07:36
  • Fantastic! Thank you. Now I get the string. I only need to clean it up a bit to get only the value. Thank you very much! – Christer Berglund Nov 21 '19 at 06:47
  • @ChristerBerglund Welcome – Mengdi Liang Nov 21 '19 at 06:58