有条件地将分支名称包含在 Azure DevOps 构建名称中

Dav*_*ard 3 expression azure-devops

我正在使用 Azure DevOps 构建管道,但我希望更改构建的名称。根据文档,这是完全可能的。

考虑到这一点,我测试了以下内容,效果很好。到目前为止,一切都很好...

name: '1.0.$(Rev:r)-$(Build.SourceBranchName)'
Run Code Online (Sandbox Code Playgroud)

1.0.1-主版

但是,我的要求之一是排除分支名称(如果它是“master”),所以我尝试了以下操作。这似乎在第一个实例中起作用,因为“主”没有附加分支名称,但是当我运行功能分支时,我发现它$(Build.SourceBranchName)没有被扩展。

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

name: '1.0.$(Rev:r)$(branchName)'
Run Code Online (Sandbox Code Playgroud)

我已经尝试过${{ variables.Build.SourceBranchName }}并按照文档$[variables.Build.SourceBranchName]中的描述进行操作,但正如所写,它们的令牌要么被忽略,要么返回空字符串。这三种格式给我留下了以下构建名称。

1.0.1-$(Build.SourceBranchName)
1.0.1-
1.0.1-$[变量.Build.SourceBranchName]

关于自定义命名的文档提到了一个变量$(SourceBranchName),但我已经尝试过这个,但它也失败了。

我感到困惑的是,段中的表达式可以访问此处描述的variables:变量值,但似乎段本身无法访问。variables:

是否可以有条件地命名构建,以便我可以根据需要包含/排除分支名称?

Eri*_*ith 9

The problem is how you are referencing $(Build.SourceBranchName) in the parse time expression. It is not available in that format under the context. Additional info here

This works for me!

variables:
  system.debug: true
  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    branchName: ''
  ${{ if ne( variables['Build.SourceBranchName'], 'master') }}:
    branchName: -${{ variables['Build.SourceBranchName'] }}

name: '1.0.$(Rev:r)$(branchName)'
Run Code Online (Sandbox Code Playgroud)

Some good additional info in Lance's answer ##vso[build.updatebuildnumber] might be a good path, or you could use GitVersion.