How to add branch level restriction using Bitbucket Command Line Interface (CLI)

This article explains how to add branch level restriction using the Bitbucket Command Line Interface (CLI) app.

Instructions

Use the runFromBranchList CLI action to get the list of branches for a repository and the addBranchRestriction action to add branch level restriction.

  1. Ensure that the CLI client file is updated with the required information.
  2. Execute the following CLI action: 

    --action runFromBranchList --project "project1" --repository "repository1" --common "-a addBranchRestriction --project "project1" --repository "repository1" --branch @branch@ --group "group1""

    The output of the action is similar to:

    Run: -a addBranchRestriction --project project1 --repository repository1 --branch admin/COTtxt-1585834077505 --group group1
    Branch restriction 'read-only' added with id 1.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch master --group group1
    Branch restriction 'read-only' added with id 2.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch ABCD5-1-testing-4132 --group group1
    Branch restriction 'read-only' added with id 3.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch develop --group group1
    Branch restriction 'read-only' added with id 4.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch develop1 --group group1
    Branch restriction 'read-only' added with id 5.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch developer --group group1
    Branch restriction 'read-only' added with id 6.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch admin/COTtxt-1585649449127 --group group1
    Branch restriction 'read-only' added with id 7.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch admin/COTtxt-1585647043498 --group group1
    Branch restriction 'read-only' added with id 8.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch admin/COTtxt-1585646764820 --group group1
    Branch restriction 'read-only' added with id 9.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch testbranch --group group1
    Branch restriction 'read-only' added with id 10.
    
    Run: -a addBranchRestriction --project project1 --repository repository1 --branch branch1 --group group1
    Branch restriction 'read-only' added with id 11.
    
    Run completed successfully. 11 actions were successful 
    
    
  3. Verify the newly added restrictions in the Branch permissions page.


It is recommended to test the commands in a non-production environment or run the action with the --simulate parameter to verify the behaviour before deploying.