How to Leverage the Strength of Branch Policies

Create branch policies to tie your branch, pull requests, and build into a powerful automated experience

Branch policies can act as a sort of glue to combine a branch, a build, and pull requests. Many options are available to you when configuring branch policies. First, make sure you require a pull request. Next, you’ll need to create a build in Azure DevOps to leverage when configuring a build policy.

Make sure you have at least one reviewer:

Require a minimum number of reviewers for pull requests
Require a minimum number of reviewers for pull requests

Pull Requests are required and at least one approval is needed to complete them.

A build policy can be added too. Let’s do that. Click the Add build policy button and fill in the form:

Add build policy
Add build policy

The build pipeline is specified. The trigger should be Automatic. The build should be required and have an expiration. Give your build policy a name that describes its purpose. In this case, I called it Develop-Build-Policy.

Now, let’s look at other configuration options. One option is to Limit merge types. I will choose Squash Merge to help keep my Git history clean. I’ll also add myself as an automatically included code reviewer.

Branch policies setup
Branch policy setup

With a build policy added, we have an automated build set up to run after a pull request is created. When a feature branch needs to merge to develop, a pull request is required. When a pull request is created, the automated build will run. The pull request cannot be completed (which would cause a merge to the develop branch) until it receives approval from at least one required approver and the automated build succeeds.

Pull Request Policy status
Pull Request Policy status

Assuming the in-progress build succeeds, I could approve this pull request which would allow it to complete. After completing the pull request, the code in my feature branch would merge to the develop branch.

Require Pull Requests in Azure Repos

Pull requests offer another layer of defense against poor quality code. Enforcing them with Azure Repos is easy. Here’s how.

Git Repository

First, set up a Git repository in Azure DevOps. The user interface is generally straightforward but Microsoft describes how to create a new Git repo if one has not already been created for you.

Cloning the repository takes the usual steps – using the command line or GUI tools in Visual Studio or VS Code.

Now you are ready for your first commit.

A default branch is available with Azure Repos. It is called master. You’ll want at least one more. You will push code to the new branch during development.

To create the branch, navigate to Branches in Azure DevOps and select the button New branch:

A form will appear where further information about the branch can be specified:

  1. You’ll be able to name it. I would call it dev or develop – something that describes the nature of the branch and its purpose without being too long.
  2. It needs a Based on branch – the contents in the branch will be copied to your new branch.
  3. You may have the opportunity to link a work item. This is optional but worth it for tracking purposes.

Pull Requests

Don’t push code without it being reviewed. Pull Requests are the next line of defense before code enters a branch.

This is simple to set up in Azure DevOps. From the branches list in Azure Repos, click the ellipses next to the desired branch (in this case, develop) and select Branch Policies.

You will be taken to a screen with a lot of options. There is one checkbox to require pull requests:

From now on code will be entering the branch through a pull request.

Before writing code, create a feature branch off develop. Perform commits and push your feature branch. When you are ready to add your code to develop, create a new pull request and choose your feature branch. Make sure you create a pull request for the develop branch.

To do that, make sure the develop branch is selected in Azure Repos. Your new feature branch should display with a note that it’s available to add to develop with a pull request:

Simply click the Create a pull request link to start the process. Any automated build can run if a Build Policy is set for the develop branch. We’ll discuss that in a separate article.