问题描述
我正在设置构建模板,无法弄清楚可选对象类型参数的语法.在我的管道中,我这样调用模板:
I'm setting up a build template and can't figure out the syntax for an optional object type parameter. In my pipeline I'm calling the template like this:
stages:
- template: aspnet-core.yml@templates
parameters:
database:
name: 'SomeDatabase'
server: 'SomeServer'
我在模板中定义了这样的参数:
I have the parameter defined like this in the template:
parameters:
database: null
我想在模板中进行类似的检查,以便可以有条件地运行任务:
I want to do a check like this in the template so I can run a task conditionally:
- ${{ if ne('${{ parameters.database }}', null) }}:
但是,它不喜欢if语句中的关键字null,而且我不知道如何表示它没有传入的事实.在这里我有什么选择?
However, it's not liking the keyword null in the if statement, and I don't know how to represent the fact that it wasn't passed in. What are my options here?
推荐答案
您可以使用以下表达式检查参数是否为空.对于下面的示例
You can use below expression to check if a parameter is empty. For below example
- ${{if parameters.database}}:
下面是我的测试模板和azure-pipeline.yml.
Below is my testing template and azure-pipeline.yml.
仅当database
被评估为true时,脚本任务才会执行.我进行了测试,发现database: ""
和database:
将被评估为false.如果将其定义为database: {}
,它将被评估为true.
the script task will only get executed when database
is evaluated to true. I tested and found database: ""
and database:
will be evalutated to false. If it is defined as database: {}
, it will be evaluated to true.
模板:deploy-jobs.yaml
Template: deploy-jobs.yaml
parameters:
database: {}
stages:
- stage: buildstage
pool: Hosted VS2017
jobs:
- job: secure_buildjob
steps:
- ${{if parameters.database}}:
- script: echo "will run if database is not empty"
displayName: 'Base: Pre-build'
azure-pipeline.yml:
azure-pipeline.yml:
stages:
- template: deploy-jobs.yaml
parameters:
database: ""
要在数据库为空时执行某些任务,可以使用以下语句:
To execute some tasks if database is empty you can use below statement:
steps:
- ${{if not(parameters.database)}}:
- script: echo "will run if database is empty"
displayName: 'Base: Pre-build'
这篇关于在Azure YAML中检查空对象类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!