Search code examples
azureazure-devops

How to get the source and target branch name in Release Pipelines in Azure DevOps by bash script?


How do you 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 show the answers for build pipelines but I need to get the branches' names in the release pipeline by Bash script.

I have tried the following which didn't work.

System.PullRequest.SourceBranch
System.PullRequest.TargetBranch
Build.SourceBranch
Build.SourceBranchName

Logs Image

Bash Command Image


Solution

  • 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