Skip to content

Commit e4bb9af

Browse files
committed
feat(badges): GHCR total downloads badge fed by a scheduled scraper
GitHub shows the container package's total downloads on its public page but exposes no API for it, so a daily workflow scrapes the count and force-pushes a shields.io endpoint JSON to the orphan badges branch. The README renders it next to the release downloads badge.
1 parent 2463e51 commit e4bb9af

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: GHCR downloads badge
2+
3+
# GitHub shows the total download count on the public package page but does
4+
# not expose it through any API, so this scheduled job scrapes the page and
5+
# publishes a shields.io endpoint JSON to the orphan `badges` branch. The
6+
# README renders it with:
7+
# https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/mateof/TelegramFileManager/badges/ghcr-downloads.json
8+
9+
on:
10+
schedule:
11+
- cron: '17 3 * * *'
12+
workflow_dispatch:
13+
14+
permissions:
15+
contents: write
16+
17+
jobs:
18+
badge:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Scrape total downloads from the package page
22+
id: scrape
23+
run: |
24+
COUNT=$(curl -sL "https://github.com/mateof/TelegramFileManager/pkgs/container/telegramfilemanager" \
25+
| perl -0777 -ne 'print $1 if /Total downloads.*?title="([\d,\.]+)"/s')
26+
if [ -z "$COUNT" ]; then
27+
echo "Could not scrape the download count (page layout changed?)"
28+
exit 1
29+
fi
30+
echo "Scraped count: $COUNT"
31+
echo "count=$COUNT" >> "$GITHUB_OUTPUT"
32+
33+
- name: Publish endpoint JSON to the badges branch
34+
run: |
35+
mkdir out && cd out
36+
cat > ghcr-downloads.json <<EOF
37+
{"schemaVersion": 1, "label": "ghcr downloads", "message": "${{ steps.scrape.outputs.count }}", "color": "2496ED", "namedLogo": "docker"}
38+
EOF
39+
git init -b badges
40+
git add ghcr-downloads.json
41+
git -c user.name=github-actions[bot] \
42+
-c user.email=41898282+github-actions[bot]@users.noreply.github.com \
43+
commit -m "update GHCR downloads badge data"
44+
git push --force \
45+
"https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" \
46+
badges

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<a href="https://github.com/mateof/TelegramFileManager/pkgs/container/telegramfilemanager"><img alt="GHCR image" src="https://img.shields.io/badge/ghcr.io-image-2496ED?logo=docker&amp;logoColor=white"></a>
77
<a href="https://dotnet.microsoft.com/"><img alt=".NET" src="https://img.shields.io/badge/.NET-Blazor%20Server-512BD4?logo=dotnet&amp;logoColor=white"></a>
88
<a href="https://github.com/mateof/TelegramFileManager/releases"><img alt="Downloads" src="https://img.shields.io/github/downloads/mateof/TelegramFileManager/total?label=downloads&amp;color=success"></a>
9+
<a href="https://github.com/mateof/TelegramFileManager/pkgs/container/telegramfilemanager"><img alt="GHCR downloads" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fmateof%2FTelegramFileManager%2Fbadges%2Fghcr-downloads.json"></a>
910
<a href="https://github.com/mateof/TelegramFileManager/stargazers"><img alt="Stars" src="https://img.shields.io/github/stars/mateof/TelegramFileManager?style=flat&amp;logo=github&amp;color=yellow"></a>
1011
<a href="LICENSE"><img alt="License" src="https://img.shields.io/github/license/mateof/TelegramFileManager"></a>
1112
<a href="https://github.com/mateof/TelegramFileManager/commits/main"><img alt="Last commit" src="https://img.shields.io/github/last-commit/mateof/TelegramFileManager"></a>

0 commit comments

Comments
 (0)