This Github action automates the creation of branch protection rules. It can also handle branch protection rule deletion as well.
In order to use the action, you need to generate a new personal access token from your Account's settings. This is needed because the default Github token provided for Github actions, does not have the required permissions to create/delete branch protection rules.
The idea for creating the Action, was generated due to the way we work. We usually branch-off from develop or master, in order to start building our new features and usually the base feature branch is following the pattern: feature/feature-name. Above this branch we start building the sub-features and in order to ensure the quality of our code and prevent some mistakes, we use branch
protection rules on the main feature branch.
-
Generate personal access token.
We suggest to use it with GitHub's secrets! To do that go on your project's settings under
Secrets, add aPERSONAL_ACCESS_TOKENwith the token you just created! We will use it later on! -
Choose desired action to run (e.g.: create and/or delete)
Create
Key Description PERSONAL-ACCESS-TOKENPersonal access token to create/delete branch protection rules REQUIRED-NUMBER-OF-REVIEWERSThe number of required reviewers when creating a branch protection rule. Defaults to 1.REQUIRED-STATUS-CHECKSThe status check that need to be successful before a PR can be merged. REQUIRE-REVIEW-FROM-CODEOWNERSSet to true if you need to require an approved review in pull requests including files with a designated code owner. Defaults to false.DISMISS-STALE-PR-APPROVALS-ON-NEW-COMMITSSet to true if you need new reviewable commits pushed to a matching branch to dismiss pull request review approvals. REQUIRE-LINEAR-HISTORYSet to true if you need linear history on your Pull Requests. ALLOW-FORCE-PUSHESSet to true if you want to permit force pushes for all users with push access. ALLOW-DELETIONSSet to true if you want to allow users with push access to delete matching branches. INCLUDE-ADMINISTRATORSSet to true if you want to enforce all configured restrictions for administrators, as well. RESTRICTIONS-USERSList of users allowed to push on the protected branch. Defaults to ''.RESTRICTIONS-TEAMSList of teams allowed to push on the protected branch. Defaults to ''.RESTRICTIONS-APPSList of apps allowed to push on the protected branch. Defaults to ''.A sample of rule branch creation can be found here.
Delete
Key Description PERSONAL-ACCESS-TOKENPersonal access token to create/delete branch protection rules RULES-LIMITThe number of branch protection rules to check in order to find and delete. Defaults to 100.A sample of rule branch deletion can be found here.
-
Specify the desired branch pattern that you want to use!
The key for the pattern of the base branch is
BASE-BRANCH-PATTERN.For example:
BASE-BRANCH-PATTERN: '^feature\/[a-zA-Z0-9]+$'