Enforce Build Success and Minimum Reviews with Azure DevOps Branch Policies
Learn how to combine a build validation policy and a required reviewers policy to block merges until both conditions are satisfied, with a concrete YAML pipeline example and common pitfalls to avoid.
18 Jan 2026, 04:46 UTC

Useful answer
To prevent code from being merged into a protected branch unless it passes a build and receives at least two approvals, create a branch policy that combines a Build validation policy and a Required reviewers policy. The server evaluates both conditions each time a pull request (PR) is created or updated, and the merge button remains disabled until both are satisfied.
How the mechanism works
Branch policies are stored in the repository under the .azuredevops folder and are versioned with Git. When a user opens a PR targeting a branch with policies enabled, Azure DevOps:
- Checks each attached policy.
- For a Build validation policy, it queues the selected pipeline and waits for a successful run.
- For a Required reviewers policy, it counts approvals from distinct users, blocking the author from approving their own change.
- Only when all policies report success does the service enable the Merge button.
If a policy fails, the PR page shows a red banner with the specific reason (e.g., "Build failed" or "Insufficient approvals").
Worked configuration
1. Define a build pipeline
Add a YAML file (e.g., azure-pipelines.yml) to the repository root. This example builds a .NET project and runs unit tests:
trigger:
- main
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.x'
includePreview: false
- script: dotnet build --configuration Release
displayName: 'Build solution'
- script: dotnet test --configuration Release --logger trx
displayName: 'Run tests'
Save and commit the file; Azure Pipelines will automatically detect it and create a build pipeline named after the repository (you can rename it later).
2. Attach the Build validation policy
- Navigate to Repos → Branches.
- Locate the target branch (e.g.,
main) and click the ellipsis → Branch policies. - Under Build validation, click + Add.
- Select the pipeline you just created (or an existing one).
- Set Trigger to Automatic so the build runs on every PR update.
- Optionally enable Display build status on the pull request.
- Save.
3. Add a Required reviewers policy
- Still in the branch policies view, under Required reviewers, click + Add.
- Enter the GitHub usernames or Azure AD groups that should be able to approve (e.g.,
[contact removed],[contact removed]). - Set Minimum number of reviewers to
2. - Check Require reviewers to vote and Allow requestor to approve their own changes (leave unchecked to enforce the rule).
- Save.
4. Verify the combined policy
Create a new PR from a feature branch into main. The PR page will show:
- A yellow banner indicating the build is queued.
- A section listing the required reviewers with zero approvals.
Once the build succeeds, the banner turns green. After two distinct approvers submit their approvals, the merge button becomes active.
Limits and common mistakes
Limitations
- Branch policies are evaluated only at PR creation and on each update; if you change a policy after a PR is opened, the PR must be refreshed (e.g., by adding a dummy comment) to pick up the new settings.
- Policies cannot block direct pushes to the branch unless the Force push permission is denied. Users with that permission can bypass policy checks entirely.
- The build validation policy requires a valid build definition. If the pipeline is deleted or renamed, the policy silently ignores the missing build, allowing merges without validation.
Common pitfalls
- Mis‑linked build definition: After creating a policy, verify the pipeline name appears correctly. If you see a warning like "Build definition not found", the policy will not enforce a build.
- Insufficient permissions: Ensure the project collection administrators (or the group managing the branch) have Edit policies permission. Contributors without this cannot modify policies, leading to drift between intended and actual settings.
- Force push oversight: A user with Force push on the repository can push directly to
main, ignoring all policies. Audit the Security tab for the repository and remove Force push from groups that should not bypass policies. - Stale policy updates: After changing the required reviewer count, existing PRs may still show the old requirement until refreshed. Encourage teams to add a comment or push a trivial change to trigger re‑evaluation.
- Over‑lapping policies: Adding multiple build validation policies can cause confusing status messages. Stick to one build policy per branch; combine it with other policy types (reviewers, work item linking, etc.) as needed.
Practical verification steps
- Create a PR targeting the protected branch.
- Confirm the build runs and shows a green check when successful.
- Attempt to merge with only one approval; the button should stay disabled.
- Add a second approval from a different user; the button should enable.
- To test a failure case, temporarily introduce a failing step in the YAML (e.g.,
exit 1) and verify the PR blocks merge until the build passes again.
These steps let you observe the policy behavior without claiming any specific test run.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.