skip to Main Content

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.

Logs Image

Bash Command Image

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. I assume you mean the old/classical release pipeline. Then the $(Build.SourceBranchName) should work.
    See below:
    enter image description here
    enter image description here
    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search