Skip to content

Commit 8f34583

Browse files
YanaXuCopilot
andauthored
Add a github action to remove old branches (#27906)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent e5557ad commit 8f34583

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

.github/workflows/clean-branches.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Clean Old Branches
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '5 18 * * *'
7+
8+
jobs:
9+
clean-old-branches:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repo
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Clean old branches
19+
shell: pwsh
20+
env:
21+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22+
run: |
23+
$cutoffDate = (Get-Date).AddDays(-30)
24+
$repo = gh repo view --json nameWithOwner --jq ".nameWithOwner"
25+
26+
Write-Host "Checking repo: $repo"
27+
$branches = gh api "repos/$repo/branches" --paginate | ConvertFrom-Json
28+
Write-Host "Total branches: $($branches.Count)"
29+
30+
foreach ($branch in $branches) {
31+
$name = $branch.name
32+
if ($name -match "^Daily/Release_(\d{14})$") {
33+
$timestamp = $matches[1]
34+
try {
35+
$branchDate = [datetime]::ParseExact($timestamp, "yyyyMMddHHmmss", [System.Globalization.CultureInfo]::InvariantCulture)
36+
} catch {
37+
Write-Warning "Failed to parse timestamp for $name"
38+
continue
39+
}
40+
41+
if ($branchDate -lt $cutoffDate) {
42+
Write-Host "Deleting old branch: $name (created $branchDate)"
43+
gh api -X DELETE "repos/$repo/git/refs/heads/$name"
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)