问题
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:
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.
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:
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.
来源:https://stackoverflow.com/questions/58431947/provide-a-pipeline-queue-time-variable-with-default-value