首页 > 解决方案 > 可以在 Azure Pipelines 中完成条件变量赋值吗?

问题描述

Azure Pipelines 有ExpressionsConditions,但我找不到根据条件将两个值之一分配给variable的方法。

有什么方法可以完成这个伪代码的功能吗?

    ${{ if endsWith( variables['Build.SourceBranchName'], '/master' ) }}: 
      buildVersion: variables['mavenVersion']
    ${{ else }}: 
      buildVersion: variables['Build.SourceBranchName']

标签: azure-devopsyamlazure-pipelines

解决方案


作为@Mike Murray 答案的扩展,如果您使用变量组,则必须将其他变量定义为名称值对。在这种情况下使用条件变量赋值如下:

variables:
- group: 'my-variable-group'
- name: myfirstadditionalvariable
  value: 100
- name: myconditionalvariable
  ${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}: 
    value: masterBranchValue
  ${{ if ne( variables['Build.SourceBranchName'], 'master' ) }}: 
    value: featureBranchValue

推荐阅读