How to Implement a /rebase Command in GitHub Actions
A /rebase comment can rebase the PR branch onto its base without the author touching their local checkout.
Trigger on issue_comment with body /rebase, verify the commenter permission, then run cirrus-actions/rebase with a token that can push to the PR branch.
Steps
- Guard on
/rebaseand the PR context. - Verify the commenter has write access.
- Check out with a PAT and run the rebase action.
Workflow
.github/workflows/rebase.yml
on:
issue_comment:
types: [created]
jobs:
rebase:
if: github.event.issue.pull_request && github.event.comment.body == '/rebase'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.PAT }}
fetch-depth: 0
- uses: cirrus-actions/rebase@1.8
env:
GITHUB_TOKEN: ${{ secrets.PAT }}Gotchas
- A rebase rewrites history and force-pushes the PR branch; only authorized maintainers should trigger it.
fetch-depth: 0is required so the full history is available for the rebase.- GITHUB_TOKEN cannot push to a fork branch; use a PAT with the right scope.
Related guides
How to Implement /retest and /rerun-failed in GitHub ActionsAdd /retest and /rerun-failed slash commands in GitHub Actions that re-run all or only the failed jobs of a w…
How to Restrict Who Can Run Slash Commands in GitHub ActionsRestrict who can run chat-triggered CI commands in GitHub Actions by checking the commenter repository permis…