name: Automatic Rebase
on:
issue_comment:
types: [created]
jobs:
rebase:
name: Rebase
runs-on: ubuntu-latest
if: >
github.event.issue.pull_request != '' &&
(
contains(github.event.comment.body, '/rebase') ||
contains(github.event.comment.body, '/autosquash')
)
steps:
- name: Checkout the latest code
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Automatic Rebase
uses: cirrus-actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Automatic Rebase
GitHub Action to automatically rebase pull requests when triggered by a comment.
What is Automatic Rebase GitHub Action?
This GitHub Action automatically rebases pull requests when a comment triggers it. It can simplify your workflow by ensuring PRs are up-to-date without manual intervention. Supports /rebase
and /autosquash
commands to trigger the action.
How to Trigger a Rebase
Once the action is configured, simply comment /rebase
or /autosquash
on a pull request to trigger the rebase.
Example:
/rebase
How to Handle Autosquash
You can trigger an autosquash by commenting /autosquash
or /rebase-autosquash
on the PR. The action will handle rebasing and squashing the commits automatically.
with:
autosquash: ${{ contains(github.event.comment.body, '/autosquash') || contains(github.event.comment.body, '/rebase-autosquash') }}
How to Use Personal Access Token (PAT)
To ensure GitHub Actions re-run after a successful rebase, use a Personal Access Token (PAT) instead of the default GITHUB_TOKEN
:
- name: Checkout the latest code
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_TOKEN }}
fetch-depth: 0
- name: Automatic Rebase
uses: cirrus-actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
How to Rebase a Specific PR
You can specify the pull request number in the workflow if it isn't directly referred to by the action:
- name: Automatic Rebase
uses: cirrus-actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
PR_NUMBER: 1245
Restricting Who Can Call the Action
You can restrict who can trigger the rebase by checking the author_association
field in the comment:
if: github.event.comment.author_association == 'MEMBER'
This ensures that only certain users, such as repository members, can call the action.
Handling Review Dismissals After Rebase
To automatically dismiss existing reviews after a rebase and require re-approval, enable this setting in GitHub:
- Go to Settings > Branches > Branch protection rules > Require pull request reviews before merging.
- Check the option Dismiss stale pull request approvals when new commits are pushed.
This will ensure reviews are dismissed after a successful rebase and will require re-approval before merging.
Monitoring GitHub Actions Workflows
CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!