Variables in Resources:Repositories:Repository:Ref

Copper Contributor

According to this documentation, the "Resources:Repositories:Ref" property supports template expressions, so that one can parameterize the ref property as follows:

- repository: templates
   type: git
   name: Templates
   ref: ${{ variables['Build.SourceBranch'] }}

For our use case, this is so that we can check out the same branch across dependency repos in the pipeline:


MainRepo (develop)
DependencyRepo1 (develop)
DependencyRepo2 (develop)


MainRepo (release)
DependencyRepo1 (release)
DependencyRepo2 (release)


Using ref: ${{ variables['Build.SourceBranch'] }} works great when the pipeline is triggered manually through the ADO GUI: if the develop branch is selected, the dependency repos also check out the develop branch and likewise for release.


The problem occurs with PR build triggers, since ${{ variables['Build.SourceBranch'] }} resolves to refs/pull/1/merge, rather than develop or release.


The dependency repos, naturally, do not have a corresponding refs/pull/1/merge branch, but even if they did, we want ${{ variables['Build.SourceBranch'] }} to resolve to the PR's target branch (either develop or release, so that the dependency repo's develop or release branch will also be checked out).


So, we have tried many ways to conditionally set the value for ref, for example, using a variable:

- name: DependencyBranch
${{ if eq(variables['Build.Reason'], 'Manual') }}:
value: $[variables.SourceBranchName]
${{ elseif eq(variables['Build.Reason'], 'PullRequest') }}:
value: $[variables.System.PullRequest.targetBranch] *this sets the correct value for PRs
${{ else }}:
value: develop 
- repository: templates
   type: git
   name: Templates
   ref: $(DependencyBranch)
This also works great for manual builds, but PR triggers still do not resolve the correct value.
Is there any way to implement support for checking out the same branch across dependency repos, for both Manual and PR builds?
3 Replies

Hi @blashmet ,


Thanks for your great question. I see in your example that in the Manual case your reference SourceBranchName and in the PR case your reference targetBranch without the Name suffix in the variable name. In the documentation on system variables it describes how the `System.PullRequest.targetBranchName` variable contains the branch name and the `System.PullRequest.TargetBranch` variable contains the full ref. This behaviour is the same for the `Build.SourceBranch` and `Build.SourceBranchName` variables. Where again the first contains the full ref and the other just the branch name.


So in your case I would try to use `System.PullRequest.targetBranchName` instead of `System.PullRequest.targetBranch`. Hope that helps in solving your issue.


See Predefined variables - Azure Pipelines | Microsoft Learn for details on the variations

This is a great catch. I did however try all combinations of:

SourceBranch / SourceBranchName
TargetBranch/ TargetBranchName

Apologies if the example misled, it was the last combination that I had tried.

Were you able to test and create the desired behavior? (i.e., parameterize the ref for both Manual builds and PR builds?) If so I would love to see the actual code, but I will double check on my end to ensure that I used all combinations of SourceBranch/Name TargetBranch/Name.

Thanks for the help :)