5
votes

In Azure Pipelines you can set pipeline variables at queue time. You can use such a variable the same way as variables defined by the pipeline itself.

Example:

# pipeline.yml
steps:
- checkout: none
- template: steps/some.yml
  parameters:
    name: $(queueTimeVar)

# steps/some.yml
parameters:
  name: 'World'

steps:
  - bash: |
      echo "Hello ${{ parameters.name }}!"

But if the variable isn't set explicitly, the pipeline evaluates this expresstion to the string itself. The step template would be called with name: '$(queueTimeVar)' and print Hello $(queueTimeVar)!.

How could I set a default value if the variable wasn't set?


I tried adding the default value as variable but it didn't work as expected.

variables:
  queueTimeVar: MyDefault

Afterwards the queue time variable had no effect. The variable was always the YAML value.

As workaround I had to add the default handling to every task which uses the value.

# bash task
value="MyDefault"
if [ -n "$QUEUETIMEVAR" ]; then
  value="$QUEUETIMEVAR"
fi
1
Hi, how the things going? Does the below explanation could help you solve the puzzle? If you are still facing any puzzle about it, feel free to leave comment here:-)Merlin Liang - MSFT
@MerlinLiang-MSFT Maybe my question is not clear enough. Sorry. I thougt that I set the default variable as variable within the YAML and in particlar cases (e.g. test/error analysis) I can override that value with a queue time variable. But I didn't work.sschmeck

1 Answers

6
votes

How could I set a default value if the variable wasn't set?

If what you mean is does not set this variable queueTimeVar in anywhere, including in Variables tab in trigger page, or Variables tab on YAML configuration page. Unfortunately to say, No, without the variable set explicitly, the server could not know where should get the value.

Until now, if the pipeline configure type you are using is YAML, The server can only recognize the variables which defined in three places: (1) Variable block in YAML script, (2) Variables panel in the configuration page, (3) Variables tab in the Triggers setting.

enter image description here

Any variables which does not defined in one of these three locations are not recognized by the server, even just create one new variable in the below location:

enter image description here

In one word, if you just create a new variable in queue time and have not define it in that three location firstly, the server still could not recognize the variable and its value.

So, you must set variables in one of locations I mentioned previously. Or the pipeline would not get it.