Can't determine pipeline which triggered a build

Jeremy Morton 1 Reputation point
2020-08-23T20:56:58.977+00:00

I'm using Azure DevOps's multiple repository functionality, documented here:

​https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops

I have my YAML file in one repo, and the pipeline points to that YAML. The YAML has a trigger set up for another repository resource, so that when that repo gets updated, the pipeline will be triggered:

resources:  
  repositories:  
  - repository: MyRepo  
    endpoint: 'MyRepos'  
    type: git  
    name: RepoName  
    trigger:  
    - '*'  

The documentation claims that the 'Build.SourceBranch' variable will allow me to determine which branch in MyRepo triggered the pipeline build: "When an update to one of the repositories triggers a pipeline, then the following variables are set based on triggering repository"

However, this doesn't seem to be the case. No matter what branch triggers the build, 'Build.SourceBranch' is always 'refs/heads/master', presumably because the repo that holds the YAML has 'master' as its default branch.

I can't find any environment variable that gets set to the name of the branch that triggered the build, either. So how can I get the name of the branch that triggered the build? If there's no possible way, I think this needs to be added!

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,923 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tymofii Orlov 1 Reputation point
    2020-12-10T09:49:10.227+00:00

    You need to specify it as env variable in script:

    • bash: echo Branch is $MAIN_BRANCH
      env:
      MAIN_BRANCH: $(Build.SourceBranch)
    0 comments No comments