Get the name of the source branch from which the PR was merged

Viewed 1137

I use Azure DevOps as my CI/CD platform and I am currently working on developing a YAML file that requires the name of the source branch from which the PR was merged.

For example: If I open a PR from branch feature to branch master; then when I merge this PR; my pipeline gets triggered. In this trigger, I would like to get the source branch name which in my case is feature.

I've looked into System.PullRequest.SourceBranch and also Build.SourceBranch; but these options are only useful if I either have a PR open or when to get the target branch name upon merging (at least in my case)

Is there some documentation that I can refer to for getting the source branch name in the YAML when a PR is merged?

1 Answers

There's no such variable available in DevOps, but you could try a workaround:

  1. After you merge a PR, the merge commit will say something like "Merged PR XXXX". When building from master branch, you can get that commit message (i.e. via git log) and parse it to find PR Id.
  2. Knowing PullRequest's Id, you can use DevOps Rest API to get its details. One of the properties will be sourceRefName - which is the source branch you're looking for.
Related