Glenn
Glenn

Reputation: 655

Azure DevOps pipeline conditions for source branch and build reason

I have a multi-staged pipeline in which I want a specific stage's jobs only to run when a pull request is made to the Development branch.

The below YAML works yet the PS tasks are skipped even when I make a pull request to the Development branch. I also tried removing the source branch condition but to no avail.

trigger:
  - Development
  - Testing
  - Acceptance

stages:

  - stage: Development
    condition: and(eq(variables['Build.SourceBranch'], 'refs/heads/Development'), eq(variables['Build.Reason'], 'PullRequest'))
    jobs:
      - job: "FirstValidationJob"
        steps: 
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: 'Write-Host "Hello World!"'
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: 'Write-Host "Hi mom, this is me running PowerShell code on Azure!"'

Upvotes: 2

Views: 12077

Answers (1)

milbrandt
milbrandt

Reputation: 1486

You need to check the System.PullRequest.TargetBranch of your PR, not the feature branch Build.SourceBranch which triggered the build.

condition: and(eq(variables['Build.Reason'], 'PullRequest'), eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/Development'))

Upvotes: 4

Related Questions