GitHub Action
Enable Pull Request Automerge
A GitHub action to enable auto-merge on a pull request.
- uses: peter-evans/enable-pull-request-automerge@v2
with:
token: ${{ secrets.PAT }}
pull-request-number: 1
Name | Description | Default |
---|---|---|
token |
(required) A repo scoped Personal Access Token (PAT). Note: GITHUB_TOKEN does not work here. |
|
repository |
The target GitHub repository containing the pull request. | github.repository (Current repository) |
pull-request-number |
(required) The number of the target pull request | |
merge-method |
The merge method to use. merge , rebase or squash . |
merge |
This action uses a GitHub API that only works under specific conditions. All of the following conditions must be true for this action to succeed.
- The target repository must have Allow auto-merge enabled in settings.
- The pull request
base
must have a branch protection rule with at least one requirement enabled. - The pull request must be in a state where requirements have not yet been satisfied. If the pull request can already be merged, attempting to enable auto-merge will fail.
In the following example create-pull-request action is used to create a pull request containing some changes that we want to merge automatically once requirements have been satisfied.
- uses: actions/checkout@v3
# Make changes to pull request here
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.PAT }}
- name: Enable Pull Request Automerge
if: steps.cpr.outputs.pull-request-operation == 'created'
uses: peter-evans/enable-pull-request-automerge@v2
with:
token: ${{ secrets.PAT }}
pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
merge-method: squash
If the "require pull request reviews" branch protection has been enabled we can optionally auto-approve the pull request by adding the following step to the example above.
The if
condition makes sure we don't approve multiple times if the workflow executes more than once before the pull request merges.
- name: Auto approve
if: steps.cpr.outputs.pull-request-operation == 'created'
uses: juliangruber/approve-pull-request-action@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
number: ${{ steps.cpr.outputs.pull-request-number }}