Skip to content

Latest commit

 

History

History
74 lines (60 loc) · 2.68 KB

File metadata and controls

74 lines (60 loc) · 2.68 KB
title Configure Branch Policies for Azure DevOps CLI
titleSuffix Azure DevOps
description Learn how to configure branch policies using Azure DevOps CLI to help your team protect their development branches.
ms.topic how-to
ms.subservice azure-devops-reference
ms.manager mijacobs
ms.author chcomley
author chcomley
monikerRange azure-devops
ms.date 06/03/2025

Configure Git repository policies using a configuration file

[!INCLUDE version-eq-azure-devops]

Branch policies help teams protect their important branches of development. Policies enforce your team's code quality and change management standards. For an overview of policy settings you can configure, see Git repository settings and policies.

You can configure branch policies for your repository using the various az repos policy commands. The policy commands accept a single scope. They work on a single combination of repository, branch, and match type. If you want to apply the same policy across various scopes, use a policy configuration file.

Say you want to create a manual queue build policy. It covers all branch folders that start with "release" and also on the main branch.

First, create a policy configuration file for build policy, including the multiple application scopes.

[!div class="tabbedCodeSnippets"]

{
  "isBlocking": true,
  "isDeleted": false,
  "isEnabled": true,
  "revision": 1,
  "settings": {
    "buildDefinitionId": 22,
    "displayName": "Manual Queue Policy",
    "manualQueueOnly": true,
    "queueOnSourceUpdateOnly": false,
    "scope": [
      {
        "matchKind": "Prefix",
        "refName": "refs/heads/release",
        "repositoryId": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
      },
      {
        "matchKind": "Exact",
        "refName": "refs/heads/main",
        "repositoryId": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
      }
    ],
    "validDuration": 0
  },
  "type": {
    "displayName": "Build",
    "id": "bbbbbbbb-1111-2222-3333-cccccccccccc"
  }
}

To learn more about the structure for various policy types, see Policy create.

Then, save the file and run the create policy command:

az repos policy create --policy-configuration C:\policyConfiguration.txt

Note

The path is provided using '\' backslash.

Related content