Track Mantra App Downloads #57
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: Track Mantra App Downloads | |
| on: | |
| schedule: | |
| - cron: '0 9 * * *' # 每天 UTC 09:00 = 北京时间 17:00 | |
| workflow_dispatch: # 支持手动触发 | |
| jobs: | |
| track-downloads: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Fetch release download counts | |
| id: fetch | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| TODAY=$(date -u +"%Y-%m-%d") | |
| echo "today=$TODAY" >> "$GITHUB_OUTPUT" | |
| RELEASES=$(gh api --paginate \ | |
| -H "Accept: application/vnd.github+json" \ | |
| /repos/mantra-hq/mantra-releases/releases \ | |
| --jq '[.[] | {tag: .tag_name, assets: [.assets[] | {name: .name, count: .download_count}]}]') | |
| echo "releases_json<<EOF" >> "$GITHUB_OUTPUT" | |
| echo "$RELEASES" >> "$GITHUB_OUTPUT" | |
| echo "EOF" >> "$GITHUB_OUTPUT" | |
| - name: Build CSV rows | |
| id: build_csv | |
| env: | |
| TODAY: ${{ steps.fetch.outputs.today }} | |
| RELEASES_JSON: ${{ steps.fetch.outputs.releases_json }} | |
| run: | | |
| python3 - <<'PYEOF' | |
| import json, os, sys | |
| today = os.environ["TODAY"] | |
| try: | |
| releases = json.loads(os.environ["RELEASES_JSON"]) | |
| except Exception as e: | |
| print(f"JSON parse error: {e}", file=sys.stderr); sys.exit(1) | |
| rows = []; agg = {"total":0,"mac":0,"windows":0,"linux":0} | |
| for rel in releases: | |
| version = rel["tag"]; total=mac=windows=linux=0 | |
| for asset in rel["assets"]: | |
| n=asset["name"].lower(); c=asset["count"]; total+=c | |
| if any(k in n for k in ("darwin","macos",".dmg")): mac+=c | |
| elif any(k in n for k in ("windows","win32","win64",".exe","-setup")): windows+=c | |
| elif any(k in n for k in ("linux",".appimage",".deb",".rpm")): linux+=c | |
| rows.append(f"{today},{version},{total},{mac},{windows},{linux}") | |
| for k,v in zip(["total","mac","windows","linux"],[total,mac,windows,linux]): agg[k]+=v | |
| rows.append(f"{today},ALL,{agg['total']},{agg['mac']},{agg['windows']},{agg['linux']}") | |
| with open(os.environ["GITHUB_OUTPUT"],"a") as f: | |
| f.write("csv_rows<<EOF\n"+"\n".join(rows)+"\nEOF\n") | |
| print("Rows built:\n"+"\n".join(rows)) | |
| PYEOF | |
| - name: Checkout private repo | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: mantra-hq/mantra | |
| token: ${{ secrets.PRIVATE_REPO_TOKEN }} | |
| path: private-repo | |
| sparse-checkout: stats | |
| sparse-checkout-cone-mode: false | |
| - name: Update CSV | |
| run: | | |
| CSV="private-repo/stats/downloads.csv" | |
| TODAY="${{ steps.fetch.outputs.today }}" | |
| HEADER="date,version,total_downloads,mac_downloads,windows_downloads,linux_downloads" | |
| mkdir -p "$(dirname $CSV)" | |
| [ ! -f "$CSV" ] && echo "$HEADER" > "$CSV" | |
| { head -1 "$CSV"; tail -n+2 "$CSV" | grep -v "^${TODAY}," || true; } > /tmp/csv_tmp && mv /tmp/csv_tmp "$CSV" | |
| echo "${{ steps.build_csv.outputs.csv_rows }}" >> "$CSV" | |
| echo "--- Last 10 rows ---" | |
| tail -10 "$CSV" | |
| - name: Commit and push | |
| run: | | |
| cd private-repo | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add stats/downloads.csv | |
| git diff --cached --quiet && echo "No changes to commit." && exit 0 | |
| TODAY="${{ steps.fetch.outputs.today }}" | |
| TOTAL=$(grep "^${TODAY},ALL," stats/downloads.csv | cut -d',' -f3 || echo "?") | |
| git commit -m "chore(stats): update downloads for ${TODAY} [total: ${TOTAL}]" | |
| git push |