name: Docker Pulls History on: schedule: - cron: "0 0 * * *" # 每天 UTC 00:00(台灣 08:00) workflow_dispatch: permissions: contents: write pull-requests: write jobs: track: runs-on: ubuntu-latest steps: - name: Checkout repo uses: actions/checkout@v4 with: ref: main - 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 = ` Docker Pull History · convertx/convertx-cn ${dates[0]} ${dates[dates.length - 1]} ${max.toLocaleString()} `; fs.writeFileSync(outPath, svg.trim()); EOF - name: Create Pull Request uses: peter-evans/create-pull-request@v7 with: token: ${{ secrets.GITHUB_TOKEN }} branch: chore/update-docker-pulls-metrics delete-branch: true commit-message: "chore(metrics): update docker pull history" title: "chore(metrics): update docker pull history" body: | 自動更新 Docker Pull 統計資料 - 更新 `metrics/docker-pulls.csv` - 更新 `metrics/docker-pulls-history.svg` labels: automated,metrics