0

I'm a bit confused with how to set this workflow up using pull requests.

I have in place an existing multi-stage YAML build pipeline that in summary does the following:

  1. Runs a build from any branch
  2. Runs a deployment job to a Development Environment/Resource, if the source branch is feature/* or release/* and the build succeeds
  3. Runs a deployment job to a UAT Environment/Resource, if the source branch is release/*
  4. Runs a deployment job to Live/Production Environment/Resource, if the source branch is master

So off the back of CI this workflow seems to work fine, the correct stages are run depending on the branch etc.

I then decided that branch policies and pull requests might be a better option for code quality rather than let any of the main branches be direct committed to, so I started to rework the YAML to account - mainly by removing the trigger to

trigger: none

This now works correctly in line with a branch policy, the build only gets kicked off when a pull request on to develop or master is opened.

However this is then where I'm a bit confused with how this is supposed to work and how I think it works ....

Firstly - is it not possible to trigger the multi-stage YAML off the back of pull requests (using Azure Repos) ? In my head all I want to do is introduce the pull request and branch policies but keep the multi-stage deployments to environments as is. However, the deployment job stages all get skipped now - but this might be to do with my conditions within the YAML, which are as follows:

- stage: 'Dev'
  displayName: 'Development Deployment'
  dependsOn: 'Build'
  condition: |
    and
    (
      succeeded()
      eq(variables['Build.Reason'], 'PullRequest'),
      ne(variables['System.PullRequest.PullRequestId'], 'Null'),
      or
      (
        startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/'),
        startsWith(variables['Build.SourceBranch'], 'refs/heads/release/')
      )
    )
  jobs:
  - deployment: Deploy
    pool: 
      name: 'Development Server Agent Pool'
    variables:
      Parameters.WebsitePhysicalPath: '%SystemDrive%\inetpub\wwwroot\App'
      Parameters.VirtualPathForApplication: ''
      Parameters.VirtualApplication: ''
    environment: 'Development.Resource-Name'
.....

Is there something I am missing? Or do I have to remove the multi-stage deployments from YAML and revert back to using Release Pipelines for pull requests (maybe with approval gates??)

Thanks in advance!

John Joseph
  • 921
  • 5
  • 14

1 Answers1

1

It looks like the issue of the conditions within the YAML.

If the pipeline is triggered by a PR. the value variables['Build.SourceBranch'] will be refs/pull/<PR id>/merge. The express in above condtion startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/') will be false, which caused the stage to be skipped. See build variables for more information.

You can try using variables['System.PullRequest.SourceBranch'], which will be evaluated to the value of the source Branch of the PR. Check System variables for more information. See below:

condition: |
    and
    (
      succeeded(),
      eq(variables['Build.Reason'], 'PullRequest'),
      ne(variables['System.PullRequest.PullRequestId'], ''),
      or
      (
        startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/feature/'),
        startsWith(variables['System.PullRequest.SourceBranch'], 'refs/heads/release/')
      )
    )
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Thanks - it was indeed incorrect conditions. I also needed to add the trigger back in but restricted these to specific branch wildcards and all works as expected now :) – John Joseph Aug 10 '20 at 07:56