47

My git repo is hosted inside devops and I would like the code to be built and tests run when I create a pull request. However I can't see how to do this. I read about pull request triggers etc but I can not see how to add these as no option appear to create them.

Once the pull request is completed and it merges to master I have a pipeline that builds and tests that code etc but I also want this to happen before anyone can complete a pull request.

riQQ
  • 9,878
  • 7
  • 49
  • 66
coolblue2000
  • 3,796
  • 10
  • 41
  • 62

4 Answers4

64

How do I trigger build and test on a pull request in azure devops?

Build validation should be exactly what you are looking for.

Set a policy requiring changes in a pull request to build successfully with the protected branch before the pull request can be completed. Build policies reduce breaks and keep your test results passing. Build policies help even if you're using continuous integration (CI) on your development branches to catch problems early.

enter image description here

With this setting, once you initiate a PR on the target branch, the Build validation will trigger the build pipeline that you set, only after the build is successful, the PR will be allowed to complete.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 2
    So I assume need to create a new pipeline to build master and run the tests? (My current pipeline also deploys the code etc) Also how do I stop that pipeline from triggering again when the master branch is then updated with the PR code after completion? – coolblue2000 Apr 22 '20 at 07:01
  • @coolblue2000, Yes, you could create a new pipeline to build master and run the tests, so that it does not mix with your current pipeline. That pipeline will not be trigger again after PR complete, unless you are enable the CI for this pipeline. – Leo Liu Apr 22 '20 at 07:08
  • 3
    There is a way to do it with yaml, this is a good practice to have all pipeline configuration just right inside the same yaml definition. Here is the [docs link](https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#pr-trigger) – Juan-Kabbali Jan 10 '22 at 14:27
9

On main page of Azure DevOps go to Repos -> Branches -> (for example) master -> Branch Policies

You can link here build that will that will need to end successfully before you will be able to merge new changes to your branch by pull request.

enter image description here

Kontekst
  • 946
  • 8
  • 17
7

If you use Azure Repos for storing code:
It is not supported to define PR trigger in your YAML pipeline. For users of Azure Repos, use branch policies instead to trigger a pipeline. More info: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/pr?view=azure-pipelines#remarks

YAML PR Triggers for Azure Pipelines (only works if you store code in GitHub or Bitbucket Cloud):

pr:
- main
- develop
Joris
  • 71
  • 1
  • 3
1

Correction: this works for code hosted in Github and Bitbucket only.

We can do this using code inside the yaml file itself, like so:

# trigger this pipeline if there's a PR to any of these branches
pr:
- master
- main
- staging
- releases/*

More here: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/pr?view=azure-pipelines#examples-2

Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154