2
votes

My develop branch is protected, so you have to pull request to it. I tried to create a pipeline to build and run unit tests when I new pull request is created.

However, when I create a feature branch from develop and I push the changes for the feature branch, the pipeline is being trigged, even though I haven't created a pull request yet. The feature branch has the yaml file since is derived from develop.

Yaml file:

pr:
- develop

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    testSelector: testAssemblies
    testAssemblyVer2: |
     **\*unittests**.dll
     !**\*TestAdapter.dll
     !**\obj\**

What should I do to run the pipeline only when there's a pull request from feature branch to develop?

3
When you don't specify the CI trigger branch, it applies to every push in every branch. If you want to use PR triggers only and no CI trigger, you can try adding trigger: none to the YAML file. - Toribio
I didn't have time to test it yet, but sure, it was useful. - Murilo
Hi Murilo, Thanks for your update. Kindly check it when you are free. If you have issue feel free to share. If you have verified my solution which worked, Appreciate for marking it as an answer,which will also help others in the future. - PatrickLu-MSFT

3 Answers

3
votes

as per documentation:

YAML PR triggers are only supported in GitHub and Bitbucket Cloud. If you are using Azure Repos Git, you can configure a branch policy for build validation in order to trigger your pipeline for validation.

3
votes

YAML pipelines are configured by default with a CI trigger on all branches.

If your PR trigger is not work, then the YAML pipeline will trigger by a CI.

There are two possibilities why your PR trigger is not work:

  • First, YAML PR triggers are only supported in GitHub and Bitbucket Cloud.
  • PR and CI triggers that are configured in YAML pipelines can be overridden in the pipeline settings, and by default, new pipelines automatically override YAML PR triggers.

    Kindly check if your YAML trigger be override, to configure this setting, select Triggers from the settings menu while editing your YAML pipeline.

    enter image description here

    Select either the Continuous integration trigger or the Pull request validation trigger, and configure your desired setting by enabling or disabling Override the YAML ... trigger from here.

    enter image description here

Hope this is clearly.

1
votes

Add

trigger: none

to your yml file. Because, as said before, pipelines in ADO are defaulted to run on all branches for all commits. Otherwise, your stuff is right... adding trigger: none will mean that your pipeline will only run when a Pull Request is made with the Develop branch as the target.