Working with Branch Policies in Azure DevOps

Introduction

Azure DevOps has many capabilities such as tools for agile project management, version control, and CI/CD, it offers robust features to streamline development workflows. One critical aspect of this is branch policies, which play a pivotal role in ensuring code quality, compliance, and security. We will discuss how to enable branch policies and their use cases.

Use Cases

Branch policies in Azure DevOps have many uses.

  1. Keeping Code Quality High: Making sure changes are reviewed before adding them to the main code keeps the code in good shape.
  2. Following Rules: Making sure everyone follows the rules, like linking tasks to changes or getting approvals, helps us stick to regulations or internal guidelines.
  3. Staying Safe: We prevent mistakes or unauthorized changes by requiring certain security measures, like passing tests or building successfully.
  4. Managing Releases: Controlling how changes move into important release versions helps keep everything stable and avoids causing problems.

Step By Step Process to Enable Branch Policies

Step 1. Go to your Azure DevOps project and select the repository where you want to apply branch policies. Under Repos -> Select Branches -> Select Branch Policies option for the branch which you want it to enable.

 Azure DevOps project

Step 2. A new window will open where we can configure branch policies. In the following steps, we will discuss best practices for branch policies. First of all, we will enable the required reviewers for pull requests. Mention a minimum number of reviewers = 2 and when new changes are pushed, reset all code reviewer votes as shown in the image below.

Branch policies

Step 3. Moving further we will mandate work items to be linked before completing the pull request and also make it compulsory to resolve any comments that are active as shown below.

Work items

Step 4. Next, we will define merge types. Recommended is squash merge(Combines source branch commits into one new commit on the target branch) as shown below:

Merge types

Step 5. Next, we can define Build Validation, which will trigger the pipeline once the new code is pushed into the source branch. It can be configured as shown in the image below.

Build Validation

Step 6. Status checks are optional and recommended mostly in case of release status checks. Automatically included reviewers are used to add important stakeholders by default whenever new changes are pushed as shown in the image below.

Important stakeholders

Step 7. Sample Pull Request Screenshot. In the screenshot below, we can verify all checks applied by branch policies.

Request Screenshot

Step 8. Squash Merge validation. In the screenshot below we can verify default merge type selected is squash commit for PR raised.

Squash Merge validation

Conclusion

Branch policies in Azure DevOps help maintain code quality. When teams set up these rules correctly, they can make fewer mistakes and keep their code in good shape. Knowing how to turn on and check these rules helps teams work well together. Please feel free to reach out in case of any concerns.


Similar Articles