From f0c9dfc0d2f29c015c083f8c2e85e08a1f150007 Mon Sep 17 00:00:00 2001 From: shlee9999 <95556588+shlee9999@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:46:12 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8Feat:=20=EB=B8=8C=EB=9E=9C?= =?UTF-8?q?=EC=B9=98=20=EC=9E=90=EB=8F=99=20=EC=82=AD=EC=A0=9C=20Github=20?= =?UTF-8?q?Action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/delete_branch.yml | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/workflows/delete_branch.yml diff --git a/.github/workflows/delete_branch.yml b/.github/workflows/delete_branch.yml new file mode 100644 index 0000000..88fc491 --- /dev/null +++ b/.github/workflows/delete_branch.yml @@ -0,0 +1,55 @@ +name: Delete Merged Branches + +on: + schedule: + - cron: '0 0 * * *' # 매일 자정에 실행 + workflow_dispatch: # 수동 실행 가능 + +jobs: + delete-merged-branches: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Delete old merged branches + run: | + # 시작 로그 + echo "Starting branch cleanup process..." + + git config --global user.name 'github-actions' + git config --global user.email 'github-actions@github.com' + + # develop 브랜치로 전환 + echo "Switching to develop branch..." + git checkout develop + + # 현재 시간 기록 + echo "Current time: $(date)" + + # 병합된 브랜치 검사 및 삭제 + echo "Checking for merged branches older than 7 days..." + git branch -r --merged develop | \ + grep -v 'main\|develop' | \ + grep "origin/" | \ + cut -d "/" -f 2- | \ + while read branch; do + echo "Checking branch: $branch" + last_commit_date=$(git log -1 --format=%cd origin/$branch) + echo "Last commit date: $last_commit_date" + + if [[ $(git log -1 --since='7 days ago' origin/$branch) == "" ]]; then + echo "Branch '$branch' is older than 7 days and will be deleted" + git push origin --delete $branch + echo "Successfully deleted branch: $branch" + else + echo "Branch '$branch' is still active (less than 7 days old)" + fi + done + + # 완료 로그 + echo "Branch cleanup process completed" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From b5f155ed5f0522e8fde78a3218e047f39568f681 Mon Sep 17 00:00:00 2001 From: shlee9999 <95556588+shlee9999@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:27:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8Feat:=20=EA=B6=8C=ED=95=9C=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/delete_branch.yml | 36 +++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/.github/workflows/delete_branch.yml b/.github/workflows/delete_branch.yml index 88fc491..f85481a 100644 --- a/.github/workflows/delete_branch.yml +++ b/.github/workflows/delete_branch.yml @@ -2,8 +2,11 @@ name: Delete Merged Branches on: schedule: - - cron: '0 0 * * *' # 매일 자정에 실행 - workflow_dispatch: # 수동 실행 가능 + - cron: '0 0 * * *' + workflow_dispatch: + +permissions: + contents: write jobs: delete-merged-branches: @@ -13,43 +16,42 @@ jobs: 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 'github-actions@github.com' - # develop 브랜치로 전환 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 'main\|develop' | \ - grep "origin/" | \ - cut -d "/" -f 2- | \ + grep -v 'origin/main\|origin/develop' | \ while read branch; do - echo "Checking branch: $branch" - last_commit_date=$(git log -1 --format=%cd origin/$branch) + 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' origin/$branch) == "" ]]; then - echo "Branch '$branch' is older than 7 days and will be deleted" - git push origin --delete $branch - echo "Successfully deleted branch: $branch" + 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' is still active (less than 7 days old)" + 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 }}