How to get the source and target branch name in Release Pipelines in Azure DevOps by bash script ?
All the available links and blogs in google only shows the answers for build pipelines but I need to get the branches name in release pipeline by bash script.
`"System.PullRequest.SourceBranch" and "System.PullRequest.TargetBranch" variables.
"Build.SourceBranch"
"$(Build.SourceBranchName)"`
Nothing worked.
2
Answers
This was not working as our pipeline were running on creation of Pull request so it returns
If you create a manual release then run the pipeline then only it return the branch name.
that's why above things was not working for our process.
Now I have tried below commands and that worked for me
When Pull request is created and build job runs- SourcebranchName=$(Build.PullRequest.SourceBranchName) echo "SourcebranchName -->$SourcebranchName"
TargetbranchName=$(Build.PullRequest.TargetBranchName) echo "TargetbranchName -->$TargetbranchName"
But when pull request is completed/merged then it is returning the target branch name as source branch by using below command. Branchname=$(Build.SourceBranch)
echo "Branchname -->$Branchname" returning the target branch as source.
So the conclusion of the story we can get only those variables values that shows on 'Initialize Job' task of release pipeline.
On 'Pull Request creation' the available release pipeline variable will be Initialize jobs variables on Pull request creation
On 'Pull Request completion' the available release pipeline variable will be Initialize jobs variables on Pull request completion
I assume you mean the old/classical release pipeline. Then the $(Build.SourceBranchName) should work.
See below:
If it does not work could you provide more evidence/details?
NOTE: The release pipeline can contain more than one artifacts and then you need to use a variable with the artifacts alias inside
Release.Artifacts.{alias}.SourceBranchName
see the documentation for details.