Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps yaml passing variable using "##vso[task.setvariable" not working

In the following yaml stage (code given below), I am trying to convert parameter envList to specific env names. e.g. envList = "NameDevEnv, NameQAEnv, NameStageEnv, NameProdEnv"

I am trying to pass NameDevEnv, NameQAEnv, NameStageEnv and NameProdEnv to separate variable/parameters in yaml and then pass these name to templates so those envs are created with the given names.

Following code is not printing the correct value for variable one. i.e. I am expecting the last statement, echo $(one) to print NameDevEnv.

Please advise / assist. Thanks

stages:
  - stage: preWork
    jobs: 
     - job: convert_input
       continueOnError: false
       steps: 
         - script: |
              htring="${{parameters.envlist}}"
              echo $htring
              IFS=', ' read -r -a array <<< "$htring"
              echo "${array[0]}"
              htringz="${array[0]}"
              
              echo ${{ variables.one }} # outputs initialValue
              echo $(one)
              echo $htringz
             ## $htringz = $htring.split(",")[0]
           displayName: first vairable pass
         - bash: echo "##vso[task.setvariable variable=one;isOutput=true;]$htringz"
           displayName: set new variable value
         - script: |
               echo ${{ variables.one }} # outputs initialValue
               echo $(one)
like image 430
lavoizer Avatar asked Oct 29 '25 17:10

lavoizer


1 Answers

As Microsoft's docs point out, passing a variable between tasks requires setting the isOutput=true parameter on the variable. What they don't really explain is how this shows up in bash in the subsequent tasks. There are two key points:

  1. You MUST give the task that sets the variable a name, for example name: FOO.
  2. The bash task replaces the dot which normally appears between the task name and the variable name with an underscore. So in the example below, instead of $(FOO.BAR) (as would be used in Powershell), you use the bash syntax ${FOO_BAR}.

Example:

- task: Bash@3
  name: FOO
  displayName: Set a variable for passing between tasks.
  inputs:
    targetType: inline
    script: |
      #!/bin/bash
      echo "##vso[task.setvariable variable=BAR;isOutput=true]foobar"

- task: Bash@3
  displayName: Use a variable in another task.
  inputs:
    targetType: inline
    script: |
      #!/bin/bash
      echo "\${FOO_BAR} is '${FOO_BAR}'"

The example will output:

${FOO_BAR} is 'foobar'
like image 182
bleater Avatar answered Nov 01 '25 07:11

bleater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!