英文:
Azure Devops Parameter condition
问题
parameters:
- name: environment
type: string
default: dev
- name: region
type: string
default: ${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}central${{ else }}${{ if eq(parameters.environment, 'both') }}central, east${{ else }}east${{ endif }}${{ endif }}
当参数environment为dev或test时,参数region将为central;对于其他环境,参数region将为central, east, both。
但与此同时,我收到错误消息,指令不支持嵌入式表达式。
英文:
parameters:
- name: environment
type: string
default: dev
- name: region
type: string
default: ${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}central${{ else }}${{ if eq(parameters.environment, 'both') }}central, east${{ else }}east${{ endif }}${{ endif }}
I am putting condition as if the parameter environment is dev or test then the parameter region will be central and for other environments, the region will be central, east, both
But at the same time I am getting error as directive are not supported for the expression that are embedded
答案1
得分: 1
我不相信你可以在参数中运行表达式。为了实现所需的行为,我认为你更应该运行表达式以根据条件设置变量值。
所以应该是这样的:
parameters:
- name: environment
type: string
default: dev
variables:
${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}:
region: 'central'
${{ if eq(parameters.environment, 'both') }} :
region: 'central, east'
${{ else }} :
region: 'east'
我没有测试过这个方法,但这个方法应该可以工作。你可以引用你的区域,然后作为${{ variables.region }}
传递给未来的输入/模板。
这里还有一个提供示例的stackoverflow帖子。
英文:
I don't believe you can run an expression in a parameter. To achieve the desired behavior I think you'd rather run your expression to set a variable value based on the condition.
So it would be:
parameters:
- name: environment
type: string
default: dev
variables:
${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}:
region: 'central'
${{ if eq(parameters.environment, 'both') }} :
region: 'central, east'
${{ else }} :
region: 'east'
I have not tested this; however, this approach should work. You'd reference your region then as ${{ variables.region}}
and can pass it along to future inputs/templates.
Here's a stackoverflow post as well that provides an example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论