1+ name : Generate versions.json
2+
3+ on :
4+ workflow_run :
5+ workflows : ["Deploy tag → gh-pages/tag"] # This workflow will run after "Deploy Tags" completes
6+ types :
7+ - completed
8+
9+ permissions :
10+ contents : write
11+
12+ jobs :
13+ generate :
14+ if : github.event.workflow_run.conclusion == 'success'
15+ runs-on : ubuntu-latest
16+ steps :
17+ - name : Checkout gh-pages branch
18+ uses : actions/checkout@v4
19+ with :
20+ ref : gh-pages
21+
22+ - name : Generate versions.json
23+ id : generate
24+ run : |
25+ set -euo pipefail
26+
27+ # List top-level directories, excluding dotfiles.
28+ DIRS=$(find . -maxdepth 1 -mindepth 1 -type d ! -name '.*' -printf '%f\n' || true)
29+
30+ # Extract semver-style directories (e.g., v1.2.3) and detect "latest"
31+ TAGS=$(echo "$DIRS" | grep -E '^v[0-9]+(\.[0-9]+)*$' || true)
32+ HAS_LATEST=$(echo "$DIRS" | grep -x 'latest' || true)
33+
34+ # Sort semantic versions descending
35+ VERSIONS_ARRAY='[]'
36+ if [ -n "$TAGS" ]; then
37+ SORTED=$(echo "$TAGS" | sort -V -r)
38+ VERSIONS_ARRAY=$(printf '%s\n' "$SORTED" | jq -R -s -c 'split("\n")[:-1]')
39+ fi
40+
41+ # Prepend 'latest' to the array if it exists
42+ if [ -n "$HAS_LATEST" ]; then
43+ VERSIONS_JSON=$(echo "$VERSIONS_ARRAY" | jq -c '["latest"] + .')
44+ else
45+ VERSIONS_JSON=$VERSIONS_ARRAY
46+ fi
47+
48+ # Create the versions.json file
49+ echo "$VERSIONS_JSON" > versions.json
50+
51+ echo "Generated versions.json:"
52+ cat versions.json
53+
54+ - name : Commit & push if changed
55+ run : |
56+ git config user.name "github-actions[bot]"
57+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
58+
59+ git add versions.json
60+
61+ # Only commit if there are changes
62+ if git diff --staged --quiet; then
63+ echo "No changes in versions.json, skipping commit."
64+ exit 0
65+ fi
66+
67+ git commit -m "Update versions.json"
68+ git push origin gh-pages
0 commit comments