Skip to content

Trim Old Branches

Trim Old Branches #955

name: Trim Old Branches
on:
push:
branches:
- trim**
repository_dispatch:
types:
- all
- trim
schedule:
- cron: "0 0 * * *" # every day at midnight
jobs:
trim_branches:
runs-on: ubuntu-latest
name: Trim Old Branches
steps:
- uses: actions/checkout@v4
name: GitHub Pull
# https://github.com/actions/checkout/pull/112/files#diff-04c6e90faac2675aa89e2176d2eec7d8R194-R200
- name: Fetch all branches
run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
- name: Trim Branches
shell: bash
# delete branches starting with `gha-`
# do not delete the most recent 5 branches
# delete all branches whose latest commit is older than a week.
run: |
git config --local --unset http.https://github.com/.extraheader
git config --local --list
echo ""
echo "All Branches:"
git branch -r --sort=-committerdate
echo ""
echo "Branches older than most recent 5 branches"
git branch -r --sort=-committerdate | awk -F/ '/\/gha-/{print $2}' | tail -n+6
echo ""
for k in $(git branch -r --sort=-committerdate | awk -F/ '/\/gha-/{print $2}' | tail -n+6); do
echo "Branch: $k"
if [ -z "$(git log -1 --since='1 week ago' -s origin/$k)" ]; then
echo "-- Deleting branch: $k"
git push https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/rstudio/shinycoreci.git --delete $k
fi
done