本文介绍了可以在 Azure Pipelines 中完成条件变量赋值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Azure Pipelines 具有

master 分支打印 1.0,即 mavenVersion

Azure Pipelines has Expressions and Conditions, but I can find no way to assign one of two values to a variable, based on a condition.

Is there any way to accomplish what this pseudo-code would?

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

This should do the trick....

BuildVersion is initialised as $(Build.SourceBranch)if it's the master branch you change that to the $(mavenVersion)else no change.

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'

non-master branches prints 'refs/heads/branch_name' which is mavenVersion

master branch prints 1.0 which is mavenVersion

这篇关于可以在 Azure Pipelines 中完成条件变量赋值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:19