Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
jiumos committed Sep 13, 2024
1 parent f56b594 commit 603674a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
46 changes: 36 additions & 10 deletions .github/workflows/clear-branch.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: Clear Branch

on:
schedule:
- cron: '0 8 * * *' # Runs every day at 0:00 Beijing Time (8:00 UTC)
push:
branches:
- ci-test
- test
workflow_dispatch:
inputs:
ref:
Expand All @@ -13,6 +15,10 @@ on:
jobs:
clear_branch:
runs-on: ubuntu-latest
env:
SKIP_PATTERNS: |
v6.*
v5.*
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -22,13 +28,30 @@ jobs:
git config user.name "$GITHUB_ACTOR"
git config user.email "[email protected]"
- name: List all branches
- name: Delete invalid branches
run: |
git fetch --prune
current_branch=$(git rev-parse --abbrev-ref HEAD)
protected_branches=$(gh api /repos/${{ github.repository }}/branches --jq '.[] | select(.protected == true) | .name' | tr '\n' ' ')
protected_branches=""
protected_branches=$(gh api /repos/${{ github.repository }}/branches --paginate --jq '.[] | select(.protected == true) | .name' | tr '\n' ' ')
echo "All protected branch: ${protected_branches}"
# Convert SKIP_PATTERNS into an array
skip_patterns=$(echo "${{ env.SKIP_PATTERNS }}" | tr '\n' ' ')
for branch in $(git branch -r | sed 's/origin\///'|grep -v origin); do
skip_branch=false
# Check if the branch matches any of the skip patterns
for pattern in "${skip_patterns[@]}"; do
echo "pat : ${pattern}"
if [[ $branch =~ $pattern ]]; then
echo "Skipping branch matching pattern: $branch"
skip_branch=true
break
fi
done
if $skip_branch; then
continue
fi
if [[ " ${protected_branches[@]} " =~ " ${branch} " ]]; then
echo "Skipping protected branch: $branch"
continue
Expand All @@ -37,14 +60,17 @@ jobs:
echo "Skipping current branch: $branch"
continue
fi
last_commit_date=$(git log -1 --format=%ci origin/$branch)
# Skip branches with submitted PR
if [[ $(date -d "$last_commit_date" +%s) -lt $(date -d "1 month ago" +%s) ]]; then
if ! gh pr list --head $branch --repo https://github.com//${{ github.repository }} | grep $branch; then
echo "Deleting branch: $branch"
git push origin --delete $branch
fi
if gh pr list --head $branch --repo https://github.com//${{ github.repository }} | grep $branch; then
echo "Skipping branch with submitted PR: $branch"
continue
fi
last_commit_date=$(git log -1 --format=%ci origin/$branch)
if [[ $(date -d "$last_commit_date" +%s) -lt $(date -d "10 seconds ago" +%s) ]]; then
echo "Deleting branch: $branch"
git push origin --delete $branch
fi
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: test
on:
push:
branches:
- 'test'
- 'testmain'

jobs:
test:
Expand Down

0 comments on commit 603674a

Please sign in to comment.