Configure Branch Protection

Action ID: github:branch-protection:create
NPM Package:

@backstage/plugin-scaffolder-backend-module-github

Description

Configures Branch Protection

Input Schema

PropertyTypeDescriptionRequired
tokenstringThe token to use for authorization to GitHub
branchstring-
repoUrlstringAccepts the format `github.com?repo=reponame&owner=owner` where `reponame` is the new repository name and `owner` is an organization or username
restrictionsobject-
enforceAdminsbooleanEnforce admins to adhere to default branch protection. The default value is `true`
blockCreationsboolean-
dismissStaleReviewsbooleanNew reviewable commits pushed to a matching branch will dismiss pull request review approvals.
requiredCommitSigningboolean-
requiredLinearHistoryboolean-
requireCodeOwnerReviewsbooleanRequire an approved review in PR including files with a designated Code Owner
requireLastPushApprovalbooleanWhether the most recent push to a PR must be approved by someone other than the person who pushed it. The default value is `false`
bypassPullRequestAllowancesobject-
requireBranchesToBeUpToDatebooleanRequire branches to be up to date before merging. The default value is `true`
requiredStatusCheckContextsarrayThe list of status checks to require in order to merge into this branch
requiredApprovingReviewCountnumberSpecify the number of reviewers required to approve pull requests. Use a number between `1` and `6` or `0` to not require reviewers. Defaults to `1`.
requiredConversationResolutionbooleanRequires all conversations on code to be resolved before a pull request can be merged into this branch

Output Schema

No output schema defined for this action.

Usage Examples

Protect the default branch with required reviews and status checks

Use this after publishing a new repository with fetch:template and publish:github. It enforces admin rules, requires two approvals with CODEOWNERS, and blocks merges unless CI checks pass.

Copy
steps:
  - id: fetch-base
    action: fetch:template
    input:
      url: ./skeleton

  - id: publish
    action: publish:github
    input:
      allowedHosts:
        - github.com
      repoUrl: github.com?owner=${{ parameters.owner }}&repo=${{ parameters.repoName }}
      defaultBranch: ${{ parameters.defaultBranch }}
      description: ${{ parameters.description }}
      repoVisibility: private

  - id: protect-default-branch
    action: github:branch-protection:create
    input:
      repoUrl: github.com?owner=${{ parameters.owner }}&repo=${{ parameters.repoName }}
      branch: ${{ parameters.defaultBranch }}
      rules:
        enforce_admins: true
        required_linear_history: true
        required_conversation_resolution: true
        allow_force_pushes: false
        allow_deletions: false
        required_status_checks:
          strict: true
          contexts:
            - ci/build
            - ci/test
        required_pull_request_reviews:
          dismiss_stale_reviews: true
          require_code_owner_reviews: true
          required_approving_review_count: 2

Restrict who can push to the release branch

Apply this after the repo exists to lock down a protected release branch to a specific team. It restricts direct pushes to the release branch to the release-managers team and requires a clean history.

Copy
steps:
  - id: protect-release-branch
    action: github:branch-protection:create
    input:
      repoUrl: github.com?owner=${{ parameters.owner }}&repo=${{ parameters.repoName }}
      branch: ${{ parameters.releaseBranch }}
      rules:
        enforce_admins: true
        required_linear_history: true
        allow_force_pushes: false
        allow_deletions: false
        required_pull_request_reviews:
          required_approving_review_count: 1
          require_last_push_approval: true
        restrictions:
          teams:
            - release-managers
          users: []
          apps: []