Search code examples
azure-devopsyamlazure-pipelinesazure-yaml-pipelines

Azure DevOps pipeline build name based on branch of different repository


We'd like to name our builds in such a way that it includes the date and the branch name, so that we can easily find the correct artifact when we want to deploy to one of our environments.

We have a setup where we have two repositories: YAML and Code. The pipelines live in the YAML repo and the code that we want to build and deploy lives in the Code repo. I've got the build pipeline set up in such a way that the build gets triggered when changes are made to the codebase in the Code repository. However, when the build gets triggered, the build name contains the branch name of the YAML repo. Since we use the main branch of this repo for CI builds, the name of the build will be something like 2021-01-01 103015 main.

This makes it difficult to find the correct artifact when we want to deploy, since everything seems to point to a main branch. Instead, we'd like the build name to be along the lines of 2021-01-01 103015 Feature1 where Feature1 is the branch of the Code repo.

This is how we set our build name in YAML:

name: $(Date:yyyy-MM-dd HHmmss) $(SourceBranchName)

Is there a way to get the branch name of the Code repo in the build name?


Solution

  • - task: PowerShell@2
      name: 'updateBranchName'
      displayName: 'Update Build Name'
      inputs:
        targetType: inline
        script: |
          echo "*** Build.SourceBranch: $(Build.SourceBranch)"
          $sourceBranch = $("$(Build.SourceBranch)" -replace "refs/heads/", "")
          echo "*** Strip branch name from SourceBranch: $sourceBranch"
          echo "##vso[build.updatebuildnumber]$((get-date).tostring("yyyy-MM-dd HHmmss")) - $sourceBranch"