💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to Use Automatic Rebase with GitHub Action

cirrus-actions-rebase -
GitHub Action
1.8
682
Contributors
Contributor - fkorotkovContributor - sschuberthContributor - vorburger
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
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-action logo

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.