Git Optimizer #1
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: Git Optimizer | |
| on: | |
| schedule: | |
| - cron: "30 18 * * 6" # Runs every Sunday at 00:00 IST (18:30 UTC Saturday) | |
| workflow_dispatch: | |
| permissions: | |
| actions: write | |
| contents: read | |
| jobs: | |
| optimize: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout full history | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Skip if no commits in last 7 days | |
| run: | | |
| LAST_COMMIT_DATE=$(git log -1 --pretty="%ct") | |
| SEVEN_DAYS_AGO=$(date -d "7 days ago" +%s) | |
| if [ "$LAST_COMMIT_DATE" -lt "$SEVEN_DAYS_AGO" ]; then | |
| echo "⏩ No activity in last 7 days. Skipping optimization." | |
| exit 0 | |
| fi | |
| echo "✔ Recent commits detected. Continuing..." | |
| - name: Lightweight Git GC | |
| run: git gc --auto | |
| - name: Compact loose objects | |
| run: git repack -d | |
| - name: Update commit-graph | |
| run: git commit-graph write --reachable --changed-paths | |
| - name: Cleanup GitHub Actions caches | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const caches = await github.rest.actions.getActionsCacheList({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| }); | |
| for (const cache of caches.data.actions_caches) { | |
| await github.rest.actions.deleteActionsCacheById({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| cache_id: cache.id | |
| }); | |
| } | |
| console.log("Done."); |