Created
June 6, 2025 17:20
-
-
Save danielrothfus/c2b6e8bba3b9f0a4ffbd898c6227cf9f to your computer and use it in GitHub Desktop.
A Github action to clean out branches that have been merged into develop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This task cleans out all branches that have been merged into develop, leaving | |
# stage and main alone. It compares by commit ID, so branches that have been | |
# squash merged or combined using some other way that alters commit IDs will not | |
# be detected as having been merged. | |
name: Cleanup Branches | |
on: | |
workflow_dispatch: | |
inputs: | |
dry_run: | |
description: 'Run in dry-run mode (preview changes without deleting)' | |
required: false | |
default: 'true' | |
type: choice | |
options: | |
- 'true' | |
- 'false' | |
jobs: | |
cleanup: | |
runs-on: ubuntu-latest | |
container: python:2.7-alpine | |
permissions: | |
contents: write | |
steps: | |
- name: Install git | |
run: | | |
apk add --no-cache git | |
# Cloning the repository using git command using GITHUB_TOKEN generated | |
# for this action invocation so that we know the repository looks valid to | |
# git-sweep. git-sweep does not work right if we use the checkout action. | |
- name: Clone repository | |
run: | | |
git clone https://oauth2:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git . | |
- name: Install git-sweep | |
run: | | |
pip install git-sweep | |
- name: Run git-sweep cleanup | |
run: | | |
if [ "${{ github.event.inputs.dry_run }}" == "true" ]; then | |
echo "Running in dry-run mode..." | |
git-sweep preview --master develop --skip main,stage | |
else | |
echo "Running cleanup..." | |
git-sweep cleanup --force --master develop --skip main,stage | |
fi | |
- name: Show results | |
run: | | |
echo "Current branches:" | |
git branch -r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment