Coder Social home page Coder Social logo

branch-enforcement's Introduction

Branch Enforcement Action

CodeQL Continuous Integration Package Check Super Linter Code Coverage

This action lets you enforce a merge policy between branches in your repository. This action lets you specify branch names or glob patterns to compare the base and head refs of a pull request. If an incoming pull request does not match at least one of the specified patterns, the action will fail.

For example, if you want to enforce a policy where developers can merge their feature branches into dev, but only the dev branch can be merged into qa, and only qa into main, you can use this action with the following policy:

policy: |
  *:dev
  dev:qa
  qa:main

Using this policy, and the appropriate branch protection rules, if you create a new branch and try to merge it into any branch besides dev, the pull request will not pass the required status check.

Supported Patterns

Note

Because of the security implications of allowing arbitrary regular expressions, this action only supports a subset of glob-like patterns. The following table describes those patterns:

Pattern Example Definition
* * Any branch
Doesn't match slashes (/)
**/* **/* Any branch
Matches slashes (/)
<string>/* users/* Any branch starting with <string>/
Doesn't match multiple slashes (/)
<string>/**/* users/**/* Any branch starting with <string>/
Matches multiple slashes (/)

Usage

To use this action and enforce your merging policy, you will need to do the following:

Step 1: Configure Branch Protection

Create a branch protection rule for each branch you want to restrict. At minimum, the rule must enforce the following:

  • Require a pull request before merging
  • Require status checks to pass before merging (the workflow that calls this action must be included in the list of required checks)

Example branch protection settings

Step 2: Create a Workflow File

Create a new workflow file in your repository. You can do this by creating a new file in the .github/workflows directory. Your workflow must specify the following:

Key Description
on: pull_request Trigger the workflow when a PR is opened / updated
Sets github.head_ref / github.base_ref variables
permissions The workflow must have write permissions to statuses
This is required to set the status of the pull request

For example, you can create a file called branch-enforcement.yml with the following contents.

Note

The policy should be specified as a YAML string, with each entry on a separate line. The format of each entry is <base>:<head>, where <base> and <head> are glob-like patterns.

name: Branch Enforcement

on:
  pull_request:

jobs:
  check-branch:
    name: Check Branches
    runs-on: ubuntu-latest

    permissions:
      statuses: write

    env:
      # The branch being merged.
      HEAD_REF: ${{ github.head_ref }}
      # The target branch.
      BASE_REF: ${{ github.base_ref }}

    steps:
      # Policy:
      #   - Any branch can be merged into dev (e.g. feature branches)
      #   - dev can be merged into qa
      #   - qa can be merged into main
      - name: Enforce Merge Policy
        id: enforce-policy
        uses: ncalteen/[email protected]
        with:
          policy: |
            *:dev
            dev:qa
            qa:main
          head_ref: ${{ env.HEAD_REF }}
          base_ref: ${{ env.BASE_REF }}

      # This step should not run if the branch policy is not met.
      - name: Policy Passed
        id: passed
        run: echo "This PR passes the merge policy!"

branch-enforcement's People

Contributors

ncalteen avatar dependabot[bot] avatar gclhub avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.