$(System.PullRequest.SourceBranch) no found

Issue

Referring to the documentation: Microsft Guide

I’m using a Command Line task to be able to create git Tags were I want to target the System.PullRequest.SourceBranch. The problem is that is not letting me use this since is giving me the following error through the Azure pipeline:

System.PullRequest.SourceBranch: /home/vsts/work/_temp/be5fc781-2582-4e49-a126-7e4db3302e75.ps1:7
Line |
   7 |  git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/ …
     |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The term 'System.PullRequest.SourceBranch' is not recognized
     | as a name of a cmdlet, function, script file, or executable
     | program. Check the spelling of the name, or if a path was
     | included, verify that the path is correct and try again.

##[error]PowerShell exited with code '1'.

Even if I try the command above, it will not let me see any values:

echo $(System.PullRequest.SourceBranch)

I’m currently using this command:

git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))

My current file is the following:

trigger:
- none
## notes
pool:
  vmImage: ubuntu-latest

## Job to calculate semantic version
jobs:
  - job: CalculateVersion
    displayName: Semantic versioning
    
    steps:
      # Checkout with persist credentials
      - checkout: self
        persistCredentials: true

      # Install GitVersion
      - task: gitversion/setup@0
        displayName: Install GitVersion
        inputs:
          versionSpec: '5.x'

      # Retrieve Pull Request Description
      - task: PullRequestDescription@0
        name: RetrievePullRequestDescription
        displayName: Retrieve Pull Request description
        inputs:
          action: 'view'
          outputVariable: 'PullRequest.DescriptionContent'
          isOutput: true
          stripIdentifiers: false

      # Add git commit message that will be picked up by GitVersion ("+semver: patch/minor/major")
      # Depending on the Pull Request description, where the developer has marked the type of change
      - task: PowerShell@2
        displayName: Add git commit message for SemVer
        inputs:
          targetType: inline
          script: |
            Write-Host "Configuring git author info.." -ForegroundColor Cyan
          
            git config user.email "alan.haro@blabla.com"
            git config user.name "alan.haro"
            Write-Host "Doing git checkout..." -ForegroundColor Cyan
            git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
            Write-Host "Checking Pull Request description..." -ForegroundColor Cyan
            $PRdesc = "$(RetrievePullRequestDescription.PullRequest.DescriptionContent)"
            if ($PRdesc -match '(\[x\] \bFix\b)') {
              Write-Host "Adding git (empty) commit message to mark this branch as a 'patch' SemVer increment." -ForegroundColor Cyan
              git commit -a -m "+semver: patch [skip azurepipelines]" --allow-empty
            } elseif ($PRdesc -match '(\[x\] \bFeature\b)') {
              Write-Host "Adding git (empty) commit message to mark this branch as a 'minor' SemVer increment." -ForegroundColor Cyan
              git commit -a -m "+semver: minor [skip azurepipelines]" --allow-empty
            } elseif ($PRdesc -match '(\[x\] \bBig\b)') {
              Write-Host "Adding git (empty) commit message to mark this branch as a 'major' SemVer increment." -ForegroundColor Cyan
              git commit -a -m "+semver: major [skip azurepipelines]" --allow-empty
            } else {
              Write-Host "##vso[task.LogIssue type=error;]Please select the type of change in the Pull Request description, and Re-queue the validation." -ForegroundColor Cyan
              $PRdesc
              exit 1
            }
            Write-Host "Doing git push.." -ForegroundColor Cyan
            git push --set-upstream origin $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))
            Write-Host "Done." -ForegroundColor Cyan
          
      # Determine the semantic version
      - task: gitversion/execute@0
        displayName: Determine SemVer

The main idea or concept is to be able to use this tool: Major, Minor, Patch Tags

Does anyone knows how to solve this issue?

Solution

Your script works if you use it for a pull request validation:

enter image description here

It does not work if you trigger your build just for some branch:

enter image description here

I`ve tested only this line:

git checkout -b $("$(System.PullRequest.SourceBranch)".replace('refs/heads/', ''))

Check your build validation policy: Improve code quality with branch policies

Answered By – Shamrai Aleksander

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published