Skip to content

Commit 75e4836

Browse files
authored
Merge pull request #61 from prgrms-web-devcourse-final-project/12-feature/delete-branch-github-action
[Feature] #12 Delete Branch GitHub Action
2 parents 983f403 + b5f155e commit 75e4836

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

.github/workflows/delete_branch.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Delete Merged Branches
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
delete-merged-branches:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 0
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: Delete old merged branches
22+
run: |
23+
echo "Starting branch cleanup process..."
24+
25+
git config --global user.name 'github-actions'
26+
git config --global user.email '[email protected]'
27+
28+
echo "Switching to develop branch..."
29+
git checkout develop
30+
31+
echo "Current time: $(date)"
32+
33+
# 원격 브랜치 최신 정보 업데이트
34+
git fetch --prune origin
35+
36+
echo "Checking for merged branches older than 7 days..."
37+
git branch -r --merged develop | \
38+
grep -v 'origin/main\|origin/develop' | \
39+
while read branch; do
40+
branch_name=${branch#origin/}
41+
echo "Checking branch: $branch_name"
42+
43+
# 마지막 커밋 날짜 확인
44+
last_commit_date=$(git log -1 --format=%cd $branch)
45+
echo "Last commit date: $last_commit_date"
46+
47+
if [[ $(git log -1 --since='7 days ago' $branch) == "" ]]; then
48+
echo "Branch '$branch_name' is older than 7 days and will be deleted"
49+
git push origin --delete $branch_name || echo "Failed to delete branch: $branch_name"
50+
else
51+
echo "Branch '$branch_name' is still active (less than 7 days old)"
52+
fi
53+
done
54+
55+
echo "Branch cleanup process completed"
56+
env:
57+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)