提供具有默认值的管道队列时间变量 [英] Provide a pipeline queue time variable with default value

查看:87
本文介绍了提供具有默认值的管道队列时间变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Azure管道中,您可以

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.

示例:

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

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

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

但是,如果未显式设置变量,则管道会将此表达式评估为字符串本身.步骤模板将用name: '$(queueTimeVar)'调用并显示Hello $(queueTimeVar)!.

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

此后,队列时间变量无效.该变量始终是YAML值.

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

推荐答案

如果未设置变量,如何设置默认值?

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

如果您的意思是不在任何地方设置此变量queueTimeVar,包括在触发器页面的变量"选项卡或YAML配置页面的变量"选项卡中.不幸的是,不,没有显式设置变量,服务器将无法知道应该从何处获取该值.

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.

直到现在,如果您使用的管道配置类型是YAML,则服务器只能识别在三个位置定义的变量:(1) YAML脚本中的变量块,( 2)配置面板中的变量"面板,触发器"设置中的(3)变量选项卡.

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.

这篇关于提供具有默认值的管道队列时间变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆