Edit

Share via


Configure Git repository policies using a configuration file

Azure DevOps Services

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.

{
  "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.