DevOps 根据正在构建的分支更改构建的名称

时间:2021-05-05 08:00:24

标签: azure-devops yaml azure-pipelines

对于 Azure DevOps,是否需要根据正在构建的存储库来设置构建和工件的名称?

例如如果我们构建 dev 分支,我们希望名称如示例底部所示

trigger:
- develop
- release/*
- hotfix/*

pool:
  vmImage: 'windows-latest'

variables:
  versionNumber: 1.10.0
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'

name: $(versionNumber)-wip-$(Rev:r)

... etc

在发布分支的情况下,我们希望名称为

name: $(versionNumber)-release-$(Rev:r)

以及我们希望名称为 hotfix 的情况

name: $(versionNumber)-hotfix-$(Rev:r)

除了创建多个 azure-pipelines.yml 文件之外,这意味着我们几乎有重复的文件。

enter image description here

2 个答案:

答案 0 :(得分:1)

你试过了吗:

name: $(versionnumber)-$(Build.SourceBranchName)-$(Rev:r)

那应该注入 the branch name/ 之后的名称的最后一部分):

<块引用>

Build.SourceBranchName

构建排队等待的触发 repo 中的分支名称。 Git repo 分支或拉取请求:引用中的最后一个路径段。例如,在 refs/heads/master 中,此值为 master。在 refs/heads/feature/tools 中,此值为 tools

您也可以使用稍微不同的变量,但它包含所有斜杠和 /refs/heads,在这种情况下,您可能希望在构建运行时使用任务来设置名称。

<块引用>

Build.SourceBranch

构建已排队的触发 repo 的分支。一些例子:

  • Git 存储库分支:refs/heads/master
  • Git 存储库拉取请求:refs/pull/1/merge

我的 Transform Variable task 可能对此有所帮助:

- task: VariableTransformTask@1
  inputs:
    value: '$(Build.BuildNumber)'
    variableName: '$(Build.BuildNumber)'
    IsSecret: false
    transformAction: 'none'
    searchReplace: true
    searchReplaceMethod: 'basic'
    searchValue: '/'
    replacementValue: '-'

或者运行一个自定义脚本来记录一个特殊的字符串来更新变量:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $buildnumber = $env:BUILD_BUILDNUMBER
      $branchname = $env:BUILD_SOURCEBRANCH -replace "/", "-"
      
      Write-Host "##vso[build.updatebuildnumber]$buildnumber-$branchname"

答案 1 :(得分:1)

谢谢你的 jessehouwing。

我设法开始工作的方法如下所示。 Build.SourceBranch 很有用

variables:
  ${{ if contains(variables['Build.SourceBranch'], '/develop') }}:
    branchName: 'wip'
  ${{ if contains(variables['Build.SourceBranch'], '/release') }}:
    branchName: 'release'
  ${{ if contains(variables['Build.SourceBranch'], '/hotfix') }}:
    branchName: 'hotfix'
        
    name: $(versionNumber)-$(branchName)-$(Rev:r)