0

I have my source code repository in github and my pipeline in Azure devops. I am trying to execute certain tasks in my pipeline based on the source branch of a pull request. The pipeline gets triggered on PR. However, when I try to get the below attributes of a Pull request from my yml pipeline, I get the message shown in the screenshot below. It basically states command not found for all the values. Is there anything obvious that could cause this. Or is this not how these values are expected to be fetched? Any help is much appreciated.

trigger:
  branches:
    include:
    - feature/azure-pipeline
    - develop
    - release/*
    exclude:
    - features/*
    - master
pr:
  branches:
    include:
      - develop
      - main

stages:

  - stage: TestStage 
    jobs:

    - job: unit_test
      displayName: 'Unit test Job'
      pool:
       vmImage: 'macos-latest'
      

      variables:
      - name: currentBranch
        ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
         value: $(System.PullRequest.TargetBranch)
        ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
         value: $(Build.SourceBranch)

      steps:
      - task: DownloadSecureFile@1
        displayName: 'Download CSSM secrets'
        name: secureKeys
        inputs:
          secureFile: 'cssmkeys.properties'
      - script: |
         echo Target Branch is $(System.PullRequest.TargetBranch)
         echo Source Repository URI is $(System.PullRequest.SourceRepositoryURI)    
         echo PullRequest Id is $(System.PullRequest.PullRequestId)
         echo Source Branch is $(System.PullRequest.SourceBranch)   
         echo Current Branch is $(value)

Output screenshot enter image description here

EDIT Link to the system variables page - https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

torek
  • 448,244
  • 59
  • 642
  • 775
user264953
  • 1,837
  • 8
  • 37
  • 63
  • 1
    Did you check the documentation for what system variables are available in this scenario? Where do you have the PR behavior defined in your YAML? – Daniel Mann Aug 23 '22 at 15:05
  • Yes, updated the question with the link @DanielMann – user264953 Aug 23 '22 at 15:41
  • You're using a POSIX-style shell for your command here (note that this has nothing to do with Git, it's strictly up to whatever is interpreting the YAML here). That shell is trying to *execute* `$(foo)` as the *command* foo; to expand a variable, you'd need `$foo` or `${foo}`, but dots are not valid in variable names in POSIX shells. – torek Aug 23 '22 at 23:15
  • The link you've added mention that to get PowerShell invoked you must use `powershell` (not `script`) in your YAML. – torek Aug 23 '22 at 23:17
  • - task: PowerShell@2 inputs: targetType: 'inline' script: | # Write your PowerShell commands here. Write-Host "Hello World" Write-Host "$(Build.SourceBranch)" Write-Host "$(System.PullRequest.SourceBranch)" – user264953 Aug 24 '22 at 17:23
  • Hello World refs/heads/develop System.PullRequest.SourceBranch: /Users/runner/work/_temp/6cdb0545-33ad-4846-92b1-fa518c861b77.ps1:6 Line | 6 | Write-Host "$(System.PullRequest.SourceBranch)" | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 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. – user264953 Aug 24 '22 at 17:25
  • @torek - I have tried powershell, still the same error – user264953 Aug 24 '22 at 17:26
  • I don't use Windows and don't really know anything about PowerShell, so all I can tell you is that `${var.i.able}` is not valid in POSIX shells. Reading that same documentation page you linked, though, I note the following quote: `In YAML pipelines, you can reference predefined variables as environment variables. For example, the variable Build.ArtifactStagingDirectory becomes the variable BUILD_ARTIFACTSTAGINGDIRECTORY.` – torek Aug 24 '22 at 18:49
  • Based on these samples, that documentation seems not to be particularly good quality, but you should probably read it very closely anyway. – torek Aug 24 '22 at 18:51

0 Answers0