Can't determine pipeline which triggered a build

Jeremy Morton 1 Reputation point

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


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:

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

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.
23,957 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tymofii Orlov 1 Reputation point

    You need to specify it as env variable in script:

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