|
| 1 | +name: Publish Pages |
| 2 | + |
| 3 | +# Reusable workflow (RFC 0001). Assembles docs/ plus the redirect mapping and |
| 4 | +# deploys GitHub Pages; when handed a mapping artifact it also records the newly |
| 5 | +# minted redirects on the gh-pages branch. |
| 6 | +# |
| 7 | +# Lives in its own workflow rather than a composite action for two reasons: |
| 8 | +# * a composite action runs inside the CALLING job and inherits its token, so |
| 9 | +# it could not hold narrower permissions than the job that posts pets; |
| 10 | +# * both callers share this workflow's concurrency group, which is what stops |
| 11 | +# two Pages deployments from overlapping. |
| 12 | +# |
| 13 | +# The CALLING job must grant: |
| 14 | +# contents: write -- only when passing mapping_artifact (push to gh-pages) |
| 15 | +# pages: write -- deploy Pages |
| 16 | +# id-token: write -- OIDC for actions/deploy-pages |
| 17 | + |
| 18 | +on: |
| 19 | + workflow_call: |
| 20 | + inputs: |
| 21 | + mapping_artifact: |
| 22 | + description: 'Artifact holding a newly minted redirects.json. Empty = redeploy only.' |
| 23 | + type: string |
| 24 | + required: false |
| 25 | + default: '' |
| 26 | + |
| 27 | +jobs: |
| 28 | + publish: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + |
| 31 | + # Repo-scoped group shared by every caller, so the scheduled posting run and |
| 32 | + # a docs push can never deploy Pages at the same time. |
| 33 | + concurrency: |
| 34 | + group: pages-publish |
| 35 | + cancel-in-progress: false |
| 36 | + |
| 37 | + # actions/deploy-pages requires the job to declare this environment. |
| 38 | + environment: |
| 39 | + name: github-pages |
| 40 | + url: ${{ steps.deployment.outputs.page_url }} |
| 41 | + |
| 42 | + steps: |
| 43 | + - name: Checkout repo |
| 44 | + uses: actions/checkout@v6 |
| 45 | + |
| 46 | + - name: Download newly minted redirects |
| 47 | + if: ${{ inputs.mapping_artifact != '' }} |
| 48 | + continue-on-error: true # no artifact (nothing minted, or the run failed early) is fine |
| 49 | + uses: actions/download-artifact@v8 |
| 50 | + with: |
| 51 | + name: ${{ inputs.mapping_artifact }} |
| 52 | + path: minted |
| 53 | + |
| 54 | + - name: Checkout gh-pages (redirect mapping) |
| 55 | + continue-on-error: true # the branch does not exist on the very first run |
| 56 | + uses: actions/checkout@v6 |
| 57 | + with: |
| 58 | + ref: gh-pages |
| 59 | + path: gh-pages |
| 60 | + |
| 61 | + - name: Merge redirect mapping |
| 62 | + run: | |
| 63 | + # RFC 0001 constraint 4: the mapping is append-only and permanent -- |
| 64 | + # losing an entry breaks a link already posted to social media. |
| 65 | + mkdir -p minted gh-pages |
| 66 | + [ -f minted/redirects.json ] || echo '{}' > minted/redirects.json |
| 67 | + [ -f gh-pages/redirects.json ] || echo '{}' > gh-pages/redirects.json |
| 68 | + cp gh-pages/redirects.json previous.json |
| 69 | +
|
| 70 | + # Argument order is load-bearing: the second operand wins every key |
| 71 | + # collision, so gh-pages (the authoritative copy) always beats the |
| 72 | + # freshly minted file. A missing or empty minted file is therefore a |
| 73 | + # no-op and can never delete an existing redirect. Do NOT swap these. |
| 74 | + # jq failing here (a corrupt authoritative mapping) fails the step by |
| 75 | + # design -- pushing a merge derived from an unreadable file is exactly |
| 76 | + # the data loss this ordering exists to prevent. |
| 77 | + jq -s '.[0] * .[1]' minted/redirects.json previous.json > merged.json |
| 78 | +
|
| 79 | + # Reporting only -- never let a logging quirk fail the publish. |
| 80 | + jq -r -n --slurpfile new merged.json --slurpfile old previous.json \ |
| 81 | + '(($new[0] | keys) - ($old[0] | keys)) as $added |
| 82 | + | if ($added | length) == 0 then "No new redirects." |
| 83 | + else "New redirects: " + ($added | join(", ")) end' || true |
| 84 | + echo "Mapping now holds $(jq 'length' merged.json || echo '?') redirect(s)." |
| 85 | +
|
| 86 | + - name: Record mapping on gh-pages |
| 87 | + if: ${{ inputs.mapping_artifact != '' }} |
| 88 | + env: |
| 89 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + run: | |
| 91 | + if [ ! -d gh-pages/.git ]; then |
| 92 | + echo "gh-pages branch does not exist yet; initializing it." |
| 93 | + git -C gh-pages init -q -b gh-pages |
| 94 | + git -C gh-pages remote add origin \ |
| 95 | + "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 96 | + fi |
| 97 | + cp merged.json gh-pages/redirects.json |
| 98 | + cd gh-pages |
| 99 | + git config user.name "github-actions[bot]" |
| 100 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 101 | +
|
| 102 | + # -f: master's .gitignore ignores the generated redirects.json, and |
| 103 | + # gh-pages may carry a copy of that file. Without -f, git add exits 1 |
| 104 | + # and stages nothing, silently ending the redirect system. |
| 105 | + git add -f redirects.json |
| 106 | + if git diff --cached --quiet; then |
| 107 | + echo "No new redirects to record." |
| 108 | + exit 0 |
| 109 | + fi |
| 110 | +
|
| 111 | + # Commit BEFORE any pull. "git pull --rebase" refuses to run with a |
| 112 | + # dirty index (exit 128) even when the remote has not moved, so |
| 113 | + # pulling first would fail on every run that mints a redirect. |
| 114 | + git commit -q -m "Record redirect mapping (automated)" |
| 115 | + git push -q origin HEAD:gh-pages || { |
| 116 | + git pull --rebase -q origin gh-pages |
| 117 | + git push -q origin HEAD:gh-pages |
| 118 | + } |
| 119 | + echo "Redirect mapping updated on gh-pages." |
| 120 | +
|
| 121 | + - name: Assemble Pages site |
| 122 | + run: | |
| 123 | + mkdir -p _site |
| 124 | + cp -r docs/. _site/ |
| 125 | + cp merged.json _site/redirects.json |
| 126 | +
|
| 127 | + - name: Upload Pages artifact |
| 128 | + uses: actions/upload-pages-artifact@v5 |
| 129 | + with: |
| 130 | + path: _site |
| 131 | + |
| 132 | + - name: Deploy to GitHub Pages |
| 133 | + id: deployment |
| 134 | + uses: actions/deploy-pages@v5 |
0 commit comments