convertor/.github/workflows/docker-pulls-history.yml

124 lines
3.8 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Docker Pulls History
on:
schedule:
- cron: "0 0 * * *" # 每天 UTC 00:00台灣 08:00
workflow_dispatch:
permissions:
contents: write
jobs:
track:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Fetch Docker Hub pull count
run: |
set -e
# 1⃣ 確保 metrics 資料夾存在runner 是全新的)
mkdir -p metrics
# 2⃣ 若 CSV 不存在,建立並寫入 header
if [ ! -f metrics/docker-pulls.csv ]; then
echo "date,pulls" > metrics/docker-pulls.csv
fi
# 3⃣ 抓 Docker Hub 總下載數(不限版本)
DATE=$(date -u +"%Y-%m-%d")
PULLS=$(curl -s https://hub.docker.com/v2/repositories/convertx/convertx-cn/ | jq '.pull_count')
# 4⃣ 避免同一天重複寫入
if ! grep -q "^$DATE," metrics/docker-pulls.csv; then
echo "$DATE,$PULLS" >> metrics/docker-pulls.csv
fi
- name: Generate SVG chart (star-history style)
run: |
node <<'EOF'
const fs = require('fs');
const csvPath = 'metrics/docker-pulls.csv';
const outPath = 'metrics/docker-pulls-history.svg';
const rows = fs.readFileSync(csvPath, 'utf8')
.trim()
.split('\n')
.slice(1)
.map(r => {
const [d, p] = r.split(',');
return { date: d, pulls: Number(p) };
});
const dates = rows.map(r => r.date);
const pulls = rows.map(r => r.pulls);
const max = Math.max(...pulls);
const w = 800, h = 400;
const padX = 60, padY = 60;
const plotW = w - padX * 2;
const plotH = h - padY * 2;
const points = pulls.map((v, i) => {
const x = padX + (i / (pulls.length - 1)) * plotW;
const y = h - padY - (v / max) * plotH;
return `${x},${y}`;
}).join(' ');
const svg = `
<svg width="${w}" height="${h}" viewBox="0 0 ${w} ${h}"
xmlns="http://www.w3.org/2000/svg">
<style>
text {
font-family: -apple-system, BlinkMacSystemFont,
Segoe UI, Helvetica, Arial;
fill: #24292f;
}
</style>
<rect width="100%" height="100%" fill="#ffffff"/>
<text x="${w/2}" y="28" text-anchor="middle"
font-size="18" font-weight="600">
Docker Pull History · convertx/convertx-cn
</text>
<!-- grid -->
<line x1="${padX}" y1="${h-padY}" x2="${w-padX}" y2="${h-padY}"
stroke="#d0d7de"/>
<line x1="${padX}" y1="${padY}" x2="${padX}" y2="${h-padY}"
stroke="#d0d7de"/>
<!-- line -->
<polyline fill="none" stroke="#2f81f7"
stroke-width="2"
points="${points}" />
<!-- labels -->
<text x="${padX}" y="${h-20}" font-size="12">
${dates[0]}
</text>
<text x="${w-padX}" y="${h-20}" font-size="12" text-anchor="end">
${dates[dates.length - 1]}
</text>
<text x="${padX}" y="${padY-10}" font-size="12">
${max.toLocaleString()}
</text>
</svg>
`;
fs.writeFileSync(outPath, svg.trim());
EOF
- name: Commit and push
run: |
git config user.name "github-actions"
git config user.email "actions@github.com"
git add metrics
git commit -m "chore(metrics): update docker pull history" || exit 0
git push