In Azure Pipeline is possible use predefined build variables (DevOps Services) Build.SourceBranch and Build.SourceBranchName but their values at case commit with tag are tags.
In doc. https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services is written
As it is described in the document. If the pipeline is triggered by a tag. Then variables Build.SourceBranch and Build.SourceBranchName will be the tag name.
However, you can use git commands(git branch -r --contains $(Build.SourceBranchName) | grep -v $(Build.SourceVersion)
) to retrieve the branch name that the tag points to.
If you want to use the branch name as variable, You can define a variable use the logging commands(echo "##vso[task.setvariable variable=CurrentBranch]$branch"
). For below example:
- powershell: |
#get the branch name
$branch = git branch -r --contains $(Build.SourceBranchName) | grep -v $(Build.SourceVersion)
#define varialbe CurrentBranch to hold the value.
echo "##vso[task.setvariable variable=CurrentBranch]$branch"
- powershell: echo "$(CurrentBranch)" #use the branch name in the following steps by referring to $(CurrentBranch)