10

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
sschmeck
  • 7,233
  • 4
  • 40
  • 67
  • 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:-) – Mengdi Liang Oct 21 '19 at 14:04
  • @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 Nov 04 '19 at 07:41
  • I have the exact same problem. Essentially, what we need is a variable behave like parameter. A parameter can be defined in yaml with default value, and overriden at queue time. So we need a variable defined in yaml, with default value, but can be overriden at queue time by setting it from variable panel, or variable tabs as mentioned by @MengdiLiang. A few years passed, I still don't see an answer for that. – tigerpaw Aug 19 '22 at 13:27

1 Answers1

6

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.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • 4
    Maybe I don't understand the answer. But setting a pipeline variable (1) and overriding it with a queue time variable doesn't work in my enviroment. – sschmeck Nov 04 '19 at 10:10
  • 4
    @sschmeck According to the [MS docs on expansion of variables](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#expansion-of-variables), "When you set a variable in the YAML file, do not define it in the web editor as 'settable at queue time'. You cannot currently change variables that are set in the YAML file at queue time. If you need a variable to be settable at queue time, then do not set it in the YAML file." So the behavior you saw matches the docs. – user221592 Jan 07 '20 at 23:21