microsoft / azure-pipelines-yaml

Azure Pipelines YAML examples, templates, and community interaction
MIT License
1.21k stars 934 forks source link

System.PullRequest.SourceBranch not available at compile time #474

Closed jason-johnson closed 4 years ago

jason-johnson commented 4 years ago

For PR builds variables['system.pullRequest.sourceBranch'] should be set but is actually null at template compile time. I have some additional jobs that should run only when the pull request is coming from a branch following a convention defined within my team so I tried to do the following:

stages:
  - stage: TestingStage
    # definition elided

  - ${{ if and(eq(variables['Build.Reason'], 'PullRequest'), startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/features/')) }}:
    - stage: AditionalWorkStage
      # definition elided

The problem is that the second part will never be included no matter what the build is. I checked the logs from the run and in initializeLog is see the following:

Evaluating: and(eq(variables['Build.Reason'], 'PullRequest'), startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/features/'))
Expanded: and(eq('PullRequest', 'PullRequest'), startsWith(Null, 'refs/heads/features/'))

So here we see that variables['System.PullRequest.SourceBranch'] was Null when this evaluation happened but in the Azure Devops build screen I see it set at "queue time".

There are at least 3 other issues that are a result of this problem but I decided to open this as a new issue because those all describe a symptom while this appears to be the root problem each has in common. The duplicates are #274, #435 and #276.

StephenHodgson commented 4 years ago

Same can be said for System.PullRequest.PullRequestNumber

vtbassmatt commented 4 years ago

In order to consolidate to fewer feedback channels, we've moved suggestions and issue reporting to Developer Community. Sorry for the delay/confusion.

StephenHodgson commented 4 years ago

@jason-johnson I was finally able to get my use case working by setting some variables to a string, then coalesce to either choose the macro value or keep the string null so it'll collapse correctly:

variables:
  ${{ if eq( variables['build.reason'], 'PullRequest' ) }}:
    date: ''
  ${{ if ne( variables['build.reason'], 'PullRequest' ) }}:
    date: ${{ '$(Date:yyyyMMdd)' }}

name: ${{ coalesce( variables['date'], '$(System.PullRequest.PullRequestNumber)', 'Yet another fallback value' ) }}$(Rev:.r)

Hope this helps