Skip to content

Commit

Permalink
use an existing github action for that
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelopv committed Jul 20, 2023
1 parent 6df2887 commit d8459c1
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions .github/workflows/delete-old-branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,16 @@ on: [push]

jobs:
delete-old-branches:
name: Delete old branches
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
- name: Checkout repository
uses: actions/checkout@v2

- name: Fetch All Branches
run: git fetch --prune --all

- name: Delete branches older than 7 days
run: |
# Get the current date in Unix timestamp format
current_date=$(date +%s)
# Get the list of remote branches and iterate over each branch
git branch -r | while read branch; do
# Extract the branch name from the full reference (e.g., "origin/branch-name")
branch_name=${branch#"origin/"}
# Skip the 'master' branch
if [ "$branch_name" = "master" ]; then
continue
fi
# Get the last commit date of the branch in Unix timestamp format
last_commit_date=$(git show -s --format=%ct "origin/$branch_name")
# Calculate the time difference in seconds between the current date and last commit date
time_difference=$((current_date - last_commit_date))
# Define the threshold in seconds (7 days: 7 days * 24 hours/day * 60 minutes/hour * 60 seconds/minute)
threshold=$((7 * 24 * 60 * 60))
# Check if the branch is stale (not updated in the last 7 days) and list it if so
if [ "$time_difference" -gt "$threshold" ]; then
echo "Stale branch: $branch_name"
# git push --delete origin "$branch_name"
# git branch -d "$branch_name"
fi
done
- name: Run delete-old-branches-action
uses: beatlabs/[email protected]
with:
repo_token: ${{ github.token }}
date: '7 days ago'
default_branches: main,master,staging
dry_run: true
exclude_open_pr_branches: true

0 comments on commit d8459c1

Please sign in to comment.