Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] #12 Delete Branch GitHub Action #61

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/delete_branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Delete Merged Branches

on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:

permissions:
contents: write

jobs:
delete-merged-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Delete old merged branches
run: |
echo "Starting branch cleanup process..."

git config --global user.name 'github-actions'
git config --global user.email '[email protected]'

echo "Switching to develop branch..."
git checkout develop

echo "Current time: $(date)"

# 원격 브랜치 최신 정보 업데이트
git fetch --prune origin

echo "Checking for merged branches older than 7 days..."
git branch -r --merged develop | \
grep -v 'origin/main\|origin/develop' | \
while read branch; do
branch_name=${branch#origin/}
echo "Checking branch: $branch_name"

# 마지막 커밋 날짜 확인
last_commit_date=$(git log -1 --format=%cd $branch)
echo "Last commit date: $last_commit_date"

if [[ $(git log -1 --since='7 days ago' $branch) == "" ]]; then
echo "Branch '$branch_name' is older than 7 days and will be deleted"
git push origin --delete $branch_name || echo "Failed to delete branch: $branch_name"
else
echo "Branch '$branch_name' is still active (less than 7 days old)"
fi
done

echo "Branch cleanup process completed"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Loading