Trim Old Branches #944
This file contains 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
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 |