问题描述
Azure管道具有表达式和条件,但是我找不到办法将两个值之一分配给变量,具体取决于条件.
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初始化为$(Build.SourceBranch)如果是主分支,则将其更改为$(mavenVersion)否则没有变化.
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'
非主分支打印'refs/heads/branch_name
',即mavenVersion
non-master branches prints 'refs/heads/branch_name
' which is mavenVersion
master分支打印1.0,即mavenVersion
master branch prints 1.0 which is mavenVersion
这篇关于可以在Azure管道中完成条件变量分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!