growth-chart.yml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Redraws the repository growth chart every three hours and publishes it to an orphan
  2. # branch named `metrics` (issue #294). The README and the wiki show
  3. # https://raw.githubusercontent.com/ChiefGyk3D/Clouds-Against-Humanity/metrics/repo-growth.png,
  4. # so the stars line never waits for a person to refresh a snapshot by hand.
  5. #
  6. # Two jobs, split by what they may do:
  7. # render contents: read. Runs scripts/gen_repo_metrics.py --refresh with the
  8. # default GITHUB_TOKEN. That token cannot read stargazer time stamps, so the
  9. # run never asks for them: it reads the current star count and appends it to a
  10. # ledger of one dated count per day, carried from the last published snapshot
  11. # (the star history before the baseline date is committed in main). It never
  12. # reads a user name (the script validates that on every run). It draws
  13. # the picture with hash-pinned matplotlib, re-validates the two files and
  14. # uploads them as a one-day artifact. It cannot push.
  15. # publish contents: write, no other secrets. Downloads the artifact, validates it
  16. # again (it crossed an artifact boundary; it must descend from the committed
  17. # baseline and keep every earlier ledger point) and pushes a child commit of
  18. # the exact metrics tip the render job read, with an exact lease
  19. # (--force-with-lease=refs/heads/metrics:<that tip>), so it can only be a pure
  20. # fast-forward from it. A moved branch fails the run. The first run creates the branch
  21. # as an orphan root; history is kept, at most one small commit per run (eight a
  22. # day). The push is skipped only when the bundle is byte-identical to the branch
  23. # (as-of.txt carries the fetch time, so in practice every run pushes); the ledger still keeps one
  24. # point per UTC day, and a rerun on the same day replaces that day's point. main is never written, so its branch
  25. # protection is untouched.
  26. #
  27. # Residual risk: GITHUB_TOKEN cannot be scoped to one branch, so the publish job's
  28. # token is repository-wide contents: write. What limits it: the job runs only
  29. # checkout, the artifact download and scripts/publish_metrics_branch.sh from main,
  30. # holds no other secret, hands the token to the three git commands that talk to the
  31. # remote (scoped to the remote URL, never written to disk, never seen by the
  32. # validator), and the script refuses any ref but refs/heads/metrics. The real
  33. # control is a repository ruleset; see docs/wiki/Repository-Growth.md.
  34. #
  35. # The committed docs/wiki/images/repo-growth.png and .metrics-snapshot.json stay in
  36. # main as the offline, hash-checked fallback that CI's --check compares; the scheduled
  37. # picture does not replace them. Only runs on main in this repository.
  38. name: Growth chart
  39. on:
  40. schedule:
  41. - cron: "23 */3 * * *" # every three hours, off the hour (UTC)
  42. workflow_dispatch:
  43. permissions: {}
  44. concurrency:
  45. group: growth-chart
  46. cancel-in-progress: false
  47. jobs:
  48. render:
  49. if: github.repository == 'ChiefGyk3D/Clouds-Against-Humanity' && github.ref == 'refs/heads/main'
  50. runs-on: ubuntu-24.04
  51. timeout-minutes: 15
  52. permissions:
  53. contents: read
  54. outputs:
  55. tip: ${{ steps.carry.outputs.tip }}
  56. steps:
  57. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
  58. with:
  59. persist-credentials: false
  60. - name: Install the plotting libraries (every wheel pinned by hash)
  61. run: |
  62. python3 -m venv "$RUNNER_TEMP/venv"
  63. "$RUNNER_TEMP/venv/bin/pip" install --require-hashes --no-deps -r scripts/growth-chart-requirements.txt
  64. # The star ledger lives in the previously published snapshot, and the metrics
  65. # branch is mutable, so nothing from it is trusted. The script carries one file
  66. # out (size read without writing it, regular file only, fail-closed on any read
  67. # error; only "no such branch" starts a ledger from nothing) and prints the commit
  68. # it read. That commit becomes a job output: the publish job may only push a child
  69. # of exactly that tip. gen_repo_metrics.py then checks the file against the
  70. # committed baseline and keeps every earlier ledger point.
  71. - name: Carry the star ledger from the metrics branch
  72. id: carry
  73. run: |
  74. bash scripts/carry_metrics_snapshot.sh "https://github.com/$GITHUB_REPOSITORY.git" carried >> "$GITHUB_OUTPUT"
  75. # --no-star-times: the default token cannot read stargazer time stamps (REST and
  76. # GraphQL are both refused), so the run reads only the current star count. The star
  77. # history comes from the committed baseline snapshot; the ledger of daily counts
  78. # comes from the carried snapshot. Forks, pull requests and issues are read fresh.
  79. - name: Fetch counts and draw the chart
  80. env:
  81. GH_TOKEN: ${{ github.token }}
  82. run: |
  83. mkdir bundle
  84. "$RUNNER_TEMP/venv/bin/python" -I scripts/gen_repo_metrics.py --refresh --no-page --no-star-times \
  85. --repo "$GITHUB_REPOSITORY" \
  86. --baseline docs/wiki/.metrics-snapshot.json --ledger-from carried/metrics-snapshot.json \
  87. --snapshot bundle/metrics-snapshot.json --out bundle/repo-growth.png
  88. - name: Validate the bundle
  89. run: |
  90. python3 -I scripts/gen_repo_metrics.py --verify-bundle bundle \
  91. --expect-repo "$GITHUB_REPOSITORY" --max-age-hours 1
  92. - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
  93. with:
  94. name: growth-chart-bundle
  95. path: bundle
  96. if-no-files-found: error
  97. retention-days: 1
  98. publish:
  99. needs: render
  100. if: github.repository == 'ChiefGyk3D/Clouds-Against-Humanity' && github.ref == 'refs/heads/main'
  101. runs-on: ubuntu-24.04
  102. timeout-minutes: 10
  103. permissions:
  104. contents: write # the metrics branch only; the script never writes main
  105. steps:
  106. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
  107. with:
  108. persist-credentials: false
  109. - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
  110. with:
  111. name: growth-chart-bundle
  112. path: bundle
  113. - name: Validate again and push to the metrics branch
  114. env:
  115. GH_TOKEN: ${{ github.token }}
  116. METRICS_MAX_AGE_HOURS: "3"
  117. METRICS_EXPECTED_TIP: ${{ needs.render.outputs.tip }}
  118. run: bash scripts/publish_metrics_branch.sh bundle