This repository was archived by the owner on Jul 6, 2025. It is now read-only.
Branch Maintenance and Alignment #21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Branch Maintenance and Alignment | |
| on: | |
| schedule: | |
| - cron: '0 0 * * SUN' # Run weekly on Sunday | |
| workflow_dispatch: # Allow manual trigger | |
| inputs: | |
| force_cleanup: | |
| description: 'Force cleanup of stale branches' | |
| required: false | |
| default: 'false' | |
| jobs: | |
| branch-maintenance: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history | |
| - name: Setup Git | |
| run: | | |
| git config user.name "GitHub Actions" | |
| git config user.email "actions@github.com" | |
| - name: Analyze Branches | |
| id: analyze | |
| run: | | |
| # List all branches with their last commit info | |
| echo "Analyzing branches and their commit history..." | |
| git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)|%(committerdate:iso8601)|%(committername)|%(subject)' > branch_analysis.txt | |
| # Identify stale branches (no commits in 30 days, except protected branches) | |
| echo "Identifying stale branches..." | |
| while IFS='|' read -r branch date author message; do | |
| if [[ ! "$branch" =~ ^(main|development|master|release)$ ]] && [[ $(date -d "$date" +%s) -lt $(date -d "30 days ago" +%s) ]]; then | |
| echo "$branch" >> stale_branches.txt | |
| fi | |
| done < branch_analysis.txt | |
| # Identify merged branches | |
| echo "Identifying merged branches..." | |
| git branch --merged development | grep -v "^\*" | grep -vE "^(\s*development|\s*main|\s*master)$" > merged_branches.txt | |
| - name: Generate Branch Report | |
| run: | | |
| echo "# Branch Analysis Report" > branch_report.md | |
| echo "## Active Branches" >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| git branch -r --sort=-committerdate | grep -v HEAD >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| echo "## Stale Branches" >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| cat stale_branches.txt >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| echo "## Merged Branches" >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| cat merged_branches.txt >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| echo "## Recent Commits per Branch" >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| cat branch_analysis.txt >> branch_report.md | |
| echo "\`\`\`" >> branch_report.md | |
| - name: Create Issue with Branch Report | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const report = fs.readFileSync('branch_report.md', 'utf8'); | |
| await github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: 'Weekly Branch Maintenance Report', | |
| body: report, | |
| labels: ['maintenance', 'branch-cleanup'] | |
| }); | |
| - name: Cleanup Approved Branches | |
| if: github.event.inputs.force_cleanup == 'true' | |
| run: | | |
| # Delete merged branches | |
| while read branch; do | |
| if [ -n "$branch" ]; then | |
| git push origin --delete $branch | |
| echo "Deleted merged branch: $branch" | |
| fi | |
| done < merged_branches.txt | |
| # Delete stale branches | |
| while read branch; do | |
| if [ -n "$branch" ]; then | |
| git push origin --delete $branch | |
| echo "Deleted stale branch: $branch" | |
| fi | |
| done < stale_branches.txt | |
| - name: Align Branch Structure | |
| run: | | |
| # Ensure development branch exists | |
| if ! git show-ref --verify --quiet refs/remotes/origin/development; then | |
| git checkout -b development main | |
| git push origin development | |
| fi | |
| # Ensure proper branch protection | |
| gh api repos/$GITHUB_REPOSITORY/branches/development/protection --method PUT \ | |
| -f required_status_checks[0].context='test-release-candidate' \ | |
| -f enforce_admins=true \ | |
| -f required_pull_request_reviews[0].required_approving_review_count=1 | |
| gh api repos/$GITHUB_REPOSITORY/branches/main/protection --method PUT \ | |
| -f required_status_checks[0].context='test-release-candidate' \ | |
| -f enforce_admins=true \ | |
| -f required_pull_request_reviews[0].required_approving_review_count=1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |