Azure DevOps YAML流水线双参数条件匹配选择变量组问题咨询
问题原因
你当前的逻辑错误是对Azure DevOps YAML模板表达式里的in()函数用法理解有误:in()函数的标准语法为 in(待检测值, 候选值1, 候选值2, ...),作用是判断待检测值是否匹配任意一个候选值。你写的in(parameters.environment, 'global', parameters.deployment_type, 'identity')实际逻辑是判断environment参数是否等于global、deployment_type的当前值、identity这三个值的任意一个,完全不符合你要的「两个参数同时满足指定值」的需求。
可行修复方案
用and()逻辑运算符连接两个eq()等值判断,即可实现两个参数同时满足条件才加载对应变量组的效果,修正后的代码如下:
parameters: - name: environment displayName: environment type: string default: dev values: - global - dev - pre - perf - prod - name: deployment_type displayName: infrastructure to run type: string default: sandbox values: - identity - landing-zone - management - connectivity - sandbox - services/devops - applications/application-demo variables: - ${{ if and(eq(parameters.environment, 'global'), eq(parameters.deployment_type, 'identity')) }}: - group: infrastructure-subscription-identity - ${{ if and(eq(parameters.environment, 'global'), eq(parameters.deployment_type, 'management')) }}: - group: infrastructure-subscription-management - ${{ if and(eq(parameters.environment, 'global'), eq(parameters.deployment_type, 'connectivity')) }}: - group: infrastructure-subscription-connectivity - ${{ if and(eq(parameters.environment, 'global'), eq(parameters.deployment_type, 'landing-zone')) }}: - group: infrastructure-subscription-landing-zone - ${{ if and(eq(parameters.environment, 'global'), eq(parameters.deployment_type, 'sandbox')) }}: - group: infrastructure-subscription-sandbox
如果后续需要匹配非global环境的变量组,直接在and()里扩展对应的条件即可,比如and(eq(parameters.environment, 'dev'), eq(parameters.deployment_type, 'sandbox'))。
内容的提问来源于stack exchange,提问作者Simon Taylor
相关产品推荐
相关产品推荐

