Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide a pipeline queue time variable with default value

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
like image 239
sschmeck Avatar asked Oct 17 '19 11:10

sschmeck


People also ask

What is settable at queue time?

Through the setting "settable at queue time", the variable can be visualized and set by the user on the window when a new Build is queued. This is really handy. Now I would define multiple Builds and each of them can share the same variables for example, queue a clean Build or not on a self-hosted agent.

How do you pass variables in Azure pipeline?

Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.

How do I set environment variables in Azure pipeline?

To set environment variables in Azure Pipelines, open the project in Azure Pipelines. In the left pane, click the environment variable name you want to set.


1 Answers

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.

like image 125
Mengdi Liang Avatar answered Sep 22 '22 07:09

Mengdi Liang