Sync Leaderboard Data #24
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: Sync Leaderboard Data | |
| on: | |
| schedule: | |
| - cron: '0 */3 * * *' | |
| workflow_dispatch: | |
| concurrency: | |
| group: daily-fetch | |
| cancel-in-progress: true | |
| permissions: | |
| contents: write | |
| jobs: | |
| run-fetch: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout logic repo | |
| uses: actions/checkout@v3 | |
| - name: Checkout data repo | |
| uses: actions/checkout@v3 | |
| with: | |
| repository: codepvg/leetcode-ranking-data | |
| token: ${{ secrets.DATA_REPO_TOKEN }} | |
| path: db-repo | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: Install dependencies | |
| run: npm install | |
| - name: Configure Git | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| - name: Run sync loop | |
| run: | | |
| MAX_ITERATIONS=72 | |
| INTERVAL=300 | |
| for i in $(seq 1 $MAX_ITERATIONS); do | |
| echo "--- Iteration $i/$MAX_ITERATIONS ---" | |
| # 1. Pull latest changes (especially users.json) before starting | |
| cd db-repo | |
| git pull --rebase -q || true | |
| cd .. | |
| # 2. Run sync | |
| export DATA_DIR=db-repo | |
| node scripts/sync-leaderboard.js | |
| # 3. Stage changes (everything except users.json) | |
| cd db-repo | |
| git add . | |
| git reset -q users.json | |
| # Debug: show exactly what is staged | |
| echo "--- Git Status ---" | |
| git status | |
| echo "------------------" | |
| # 4. Commit and push normally if there are changes | |
| if ! git diff --staged --quiet; then | |
| SYNC_TIME=$(date -u +'%Y-%m-%d %H:%M') | |
| git commit -q -m "Sync: $SYNC_TIME" | |
| # Rebase again right before pushing in case a user registered during the script run | |
| git pull --rebase origin main -q || true | |
| git push -q | |
| echo "Stats updated and pushed." | |
| else | |
| echo "No new changes detected in the data directory." | |
| fi | |
| cd .. | |
| if [ $i -lt $MAX_ITERATIONS ]; then | |
| sleep $INTERVAL | |
| fi | |
| done | |
| continue-on-error: true |