【问题标题】:Can Conditional Variable Assignment be Done in Azure Pipelines?可以在 Azure Pipelines 中完成条件变量赋值吗?
【发布时间】:2019-12-23 05:18:32
【问题描述】:

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

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

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

【问题讨论】:

标签: azure-devops yaml azure-pipelines


【解决方案1】:

作为@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

【讨论】:

  • 您好!此语法在 Azure 的在线编辑器中为我提供了 Unexpected property ${{ if .... }}。当我无论如何保存文件并点击Validate / Run 按钮时,我会弹出一个指示'value' is already defined ?
  • 没关系,我的两个条件被评估为真,因此验证/运行时出错。在线编辑器仍然给我警告,但这没什么大不了的。
  • @Hellium 你是如何修复'value' is already defined的?我也在使用eqne,但我不明白它们是如何评估为真的。
  • 就我而言,条件比这个答案的条件更复杂,我搞混了。我已经修复了条件,使它们相互排斥。您的情况如何?
  • 非常感谢!试图弄清楚这一点:如何使用名称值语法的条件。
【解决方案2】:

我比我想象的更接近。这并不漂亮,但它有效。 (带有更多的 yaml 上下文)

variables:
  ${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}: 
    buildVersion: ${{ variables['mavenVersion'] }}
  ${{ if ne( variables['Build.SourceBranchName'], 'master' ) }}: 
    buildVersion: ${{ variables['Build.SourceBranchName'] }}

  buildKey: ${{ format('{0}_{1}', variables['supportReleaseNumber'], variables['buildVersion']) }}
  buildNum: $[counter(variables['buildKey'], 1)]  # same as $(Rev:r), but more widely usable 

name: $(buildKey)_$(buildNum)  # build run name

【讨论】:

  • 嗨,我正在按照您的示例进行操作,但遇到了类似Unexpected value 'env' 的错误,env 是我分配值的变量。我认为这是因为我将它与变量组一起使用,所以我不能按原样使用该示例。你有这个 IF ELSE 表达式(?)参考网址,我可以参考我的案例吗?
  • 我在原始帖子中包含的三个文档链接是我能找到的与该主题相关的全部内容。没有'else',所以你需要链接'if's。不同的变量类型需要不同的语法。检查Understand variable syntax 部分。
  • 谢谢。我最后从模板部分找到了if。我试图不使用模板来简化,所以不知道是否有解释。
【解决方案3】:

这应该可以解决问题....

BuildVersion 被初始化为 $(Build.SourceBranch) 如果它是主分支,则将其更改为 $(mavenVersion) 否则没有变化。

variables:
  mavenVersion: '1.0'
  buildVersion: $(Build.SourceBranch)

pool:
  vmImage: 'ubuntu-latest'

steps:

- script: echo '##vso[task.setvariable variable=buildVersion]$(mavenVersion)'
  displayName: "Set the buildVersion as mavenVersion if the Build.SourceBranch = 'refs/heads/master' "
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

- script: echo $(buildVersion)
  displayName: 'Printing the variable'

非主分支打印 'refs/heads/branch_name' 这是 mavenVersion

master 分支打印 1.0,即 mavenVersion

【讨论】:

  • 谢谢。这是一个很好的答案,我将来也许可以使用这种类型的解决方案。我忘了提到我需要使用这个变量来构造构建运行名称,所以在运行任何步骤之前我都需要这个变量。 name: $(buildKey)_$(buildNum) # 构建运行名称
  • 您可能有两个具有分支触发器的构建。一个包含,另一个包含排除。不知道这是否是你所追求的......
  • 我发布的代码位于管道的变量部分,解决了我对用于定义管道的构建运行名称的变量的需求。谢谢。
【解决方案4】:

@Mike Murray,谢谢你!多年来,我一直试图解决这个问题。 当从拉取请求触发构建时,SourceBranchName 始终是“合并”。您的回答帮助我想出了这个解决方案,用于获取两种场景的目标分支名称,手动构建和由拉取请求触发的构建:

${{ if ne( variables['Build.SourceBranchName'], 'merge' ) }}: 
    environment: ${{ variables['Build.SourceBranchName'] }}
  ${{ if endsWith( variables['System.PullRequest.TargetBranch'], 'dev' ) }}: 
    environment: dev
  ${{ if endsWith( variables['System.PullRequest.TargetBranch'], 'staging' ) }}: 
    environment: staging
  ${{ if endsWith( variables['System.PullRequest.TargetBranch'], 'master' ) }}: 
    environment: prod

不是很漂亮,但终于可以了。

【讨论】:

【解决方案5】:

有了这个update,你的 YAML 就有效了:

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

一切正常

trigger: none

name: if-else

pool:
  vmImage: ubuntu-latest

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

steps:
- script: |
    echo "$(Build.SourceBranchName)"
    echo "$(buildVersion)"
  displayName: 'Display buildVersion'

Build.SourceBranchName 只包含 master。

Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/1121efc8-6445-4e19-be5a-8d525a76467e.sh
master
master
Finishing: Display buildVersion

【讨论】:

  • 你能分享一下这个工作的截图,这样我就可以准确地看到配置了什么?我无法让它在我的variables 部分工作。
  • @kayleeFrye_onDeck 请检查我的编辑。
  • 非常感谢!
猜你喜欢
  • 2020-06-02
  • 1970-01-01
  • 2021-02-10
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2015-01-26
  • 1970-01-01
相关资源
最近更新 更多