|
| 1 | +# Reusable workflow: build, deploy, and PR-preview a Quarto site on GitHub |
| 2 | +# Pages. Scaffolded research projects (see the asta-tools `workspace` skill) |
| 3 | +# call this from a thin `docs.yml` stub, so the deploy machinery is maintained |
| 4 | +# once here rather than copied into every project. |
| 5 | +# |
| 6 | +# Contract with the calling repo: |
| 7 | +# - A `make check` target runs the repo's quality gates and renders the site |
| 8 | +# to `_site/` (the workspace skill's Makefile asset provides the baseline; |
| 9 | +# repos add their own gates to that target, never as workflow steps). |
| 10 | +# - The caller grants `contents: write` + `pull-requests: write`; jobs here |
| 11 | +# downscope to least privilege individually. |
| 12 | +# - Full history and PR head refs are fetched before `make check`, for |
| 13 | +# repos whose checks derive content from git history. |
| 14 | +name: Quarto site |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_call: |
| 18 | + |
| 19 | +jobs: |
| 20 | + # Build + quality gates (via `make check`). This is the required status |
| 21 | + # check. Concurrency is keyed per PR (or per branch for main), so a new |
| 22 | + # commit supersedes its own stale build while separate PRs and main never |
| 23 | + # cancel each other. |
| 24 | + build: |
| 25 | + if: github.event.action != 'closed' |
| 26 | + runs-on: ubuntu-latest |
| 27 | + permissions: |
| 28 | + contents: read |
| 29 | + concurrency: |
| 30 | + group: pages-${{ github.event.pull_request.number || github.ref }} |
| 31 | + cancel-in-progress: true |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + fetch-depth: 0 # full history for checks that read git history |
| 36 | + |
| 37 | + # PR head refs are permanent; fetch them so provenance commits referenced |
| 38 | + # by history-derived checks still resolve. |
| 39 | + - name: Fetch PR refs |
| 40 | + run: git fetch --no-tags origin '+refs/pull/*/head:refs/remotes/origin/pr/*' |
| 41 | + |
| 42 | + - uses: quarto-dev/quarto-actions/setup@v2 |
| 43 | + |
| 44 | + # The shared render/validate logic is vendored into each project as |
| 45 | + # scripts/quarto-check.sh. Surface (never fail on) drift between that |
| 46 | + # copy and the canonical one shipped at this workflow's own commit, so |
| 47 | + # stale or hand-edited copies get noticed on every run. |
| 48 | + - name: Check vendored quarto-check.sh for drift |
| 49 | + run: | |
| 50 | + [ -f scripts/quarto-check.sh ] || exit 0 |
| 51 | + canonical="https://raw.githubusercontent.com/allenai/asta-plugins/${{ github.job_workflow_sha }}/plugins/asta-tools/skills/workspace/assets/quarto-check.sh" |
| 52 | + if curl -fsSL "$canonical" -o /tmp/quarto-check.canonical; then |
| 53 | + if ! diff -u scripts/quarto-check.sh /tmp/quarto-check.canonical > /tmp/quarto-check.diff; then |
| 54 | + echo "::warning::scripts/quarto-check.sh differs from the workspace skill's canonical copy — re-copy assets/quarto-check.sh to update (or ignore if intentionally customized)" |
| 55 | + { echo "## quarto-check.sh drift (vendored → canonical)"; echo '```diff'; cat /tmp/quarto-check.diff; echo '```'; } >> "$GITHUB_STEP_SUMMARY" |
| 56 | + fi |
| 57 | + else |
| 58 | + echo "could not fetch canonical quarto-check.sh; skipping drift check" |
| 59 | + fi |
| 60 | +
|
| 61 | + # All quality gates live in the repo's `check` Makefile target — the |
| 62 | + # single source of truth shared with local runs, so CI and local can't |
| 63 | + # drift. Warnings are surfaced to the step summary from inside the target. |
| 64 | + - name: Run docs checks |
| 65 | + run: make check |
| 66 | + |
| 67 | + - name: Prepare Pages artifact |
| 68 | + run: touch _site/.nojekyll # serve asset dirs verbatim (no Jekyll) |
| 69 | + |
| 70 | + - name: Upload rendered site |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + with: |
| 73 | + name: site |
| 74 | + path: _site |
| 75 | + include-hidden-files: true # keep .nojekyll |
| 76 | + |
| 77 | + # Publish: main -> gh-pages root (keeping PR previews); a PR -> a preview under |
| 78 | + # /pr-preview/pr-<N>/ with a link comment. Concurrency is keyed per PR (and |
| 79 | + # separately for main), so main and any PR are always in different groups — a |
| 80 | + # main deploy can't be queued behind or cancelled by a PR's write (including a |
| 81 | + # PR-close cleanup that fires at merge). Concurrent writes from different |
| 82 | + # groups reconcile via rebase-and-retry on push, not a global queue. |
| 83 | + deploy: |
| 84 | + needs: build |
| 85 | + if: github.event_name == 'push' || github.event.action != 'closed' |
| 86 | + runs-on: ubuntu-latest |
| 87 | + permissions: |
| 88 | + contents: write |
| 89 | + pull-requests: write |
| 90 | + concurrency: |
| 91 | + group: pages-${{ github.event.pull_request.number || github.ref }} |
| 92 | + cancel-in-progress: true |
| 93 | + steps: |
| 94 | + - uses: actions/checkout@v4 |
| 95 | + - uses: actions/download-artifact@v4 |
| 96 | + with: |
| 97 | + name: site |
| 98 | + path: _site |
| 99 | + |
| 100 | + - name: Publish to gh-pages |
| 101 | + env: |
| 102 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 103 | + GH_TOKEN: ${{ github.token }} |
| 104 | + run: | |
| 105 | + git config user.name "github-actions[bot]" |
| 106 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 107 | + # First deploy: create gh-pages from the empty tree without touching |
| 108 | + # the main checkout (an orphan checkout here would make the worktree |
| 109 | + # add below fail — a branch can't be checked out twice). |
| 110 | + git fetch origin gh-pages:gh-pages 2>/dev/null \ |
| 111 | + || git branch gh-pages "$(git commit-tree "$(git hash-object -t tree /dev/null)" -m "Initialize gh-pages")" |
| 112 | + tmp=$(mktemp -d) |
| 113 | + git worktree add "$tmp" gh-pages |
| 114 | +
|
| 115 | + if [ "${{ github.event_name }}" = "push" ]; then |
| 116 | + # Main deploy: replace the site root, keep pr-preview/. |
| 117 | + find "$tmp" -mindepth 1 -maxdepth 1 ! -name pr-preview ! -name .git -exec rm -rf {} + |
| 118 | + cp -r "$GITHUB_WORKSPACE/_site/." "$tmp/" |
| 119 | + else |
| 120 | + # PR preview: publish under pr-preview/pr-<N>/. |
| 121 | + rm -rf "$tmp/pr-preview/pr-$PR_NUMBER" |
| 122 | + mkdir -p "$tmp/pr-preview/pr-$PR_NUMBER" |
| 123 | + cp -r "$GITHUB_WORKSPACE/_site/." "$tmp/pr-preview/pr-$PR_NUMBER/" |
| 124 | + # Keep previews out of search indexes. A project Pages site can't use |
| 125 | + # a path-scoped robots.txt (only the host-root one is honored), so |
| 126 | + # inject a noindex meta tag into the preview's pages (main is left |
| 127 | + # indexable). |
| 128 | + find "$tmp/pr-preview/pr-$PR_NUMBER" -name '*.html' -print0 \ |
| 129 | + | xargs -0 sed -i 's#<head>#<head><meta name="robots" content="noindex, nofollow">#' |
| 130 | + fi |
| 131 | +
|
| 132 | + cd "$tmp" |
| 133 | + git add -A |
| 134 | + if git diff --cached --quiet; then |
| 135 | + echo "gh-pages already up to date" |
| 136 | + else |
| 137 | + git commit -m "Deploy ${{ github.event_name }} ${{ github.sha }}" |
| 138 | + for i in 1 2 3 4 5; do |
| 139 | + git push origin gh-pages && break |
| 140 | + [ "$i" = 5 ] && { echo "::error::could not push gh-pages after 5 attempts"; exit 1; } |
| 141 | + echo "gh-pages moved underneath us — rebasing and retrying ($i/5)" |
| 142 | + git pull --rebase origin gh-pages || { git rebase --abort 2>/dev/null || true; echo "::error::gh-pages rebase conflict"; exit 1; } |
| 143 | + done |
| 144 | + fi |
| 145 | +
|
| 146 | + # For a PR, ensure exactly one preview-link comment (create or update), |
| 147 | + # independent of whether the content changed. Best-effort — a comment |
| 148 | + # hiccup shouldn't fail an otherwise-successful publish. |
| 149 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 150 | + url="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-$PR_NUMBER/" |
| 151 | + body="📄 Preview: $url" |
| 152 | + existing=$(gh api "repos/${{ github.repository }}/issues/$PR_NUMBER/comments" \ |
| 153 | + --jq 'map(select(.body | startswith("📄 Preview:"))) | .[0].id // empty') |
| 154 | + if [ -n "$existing" ]; then |
| 155 | + gh api "repos/${{ github.repository }}/issues/comments/$existing" -X PATCH -f body="$body" >/dev/null \ |
| 156 | + || echo "::warning::could not update preview comment" |
| 157 | + else |
| 158 | + gh api "repos/${{ github.repository }}/issues/$PR_NUMBER/comments" -f body="$body" >/dev/null \ |
| 159 | + || echo "::warning::could not create preview comment" |
| 160 | + fi |
| 161 | + fi |
| 162 | +
|
| 163 | + # Remove a PR's preview when the PR closes. Shares that PR's concurrency group |
| 164 | + # (keyed on the PR number) so it cancels any still-running build/deploy for the |
| 165 | + # PR first — otherwise a late deploy could recreate the preview after removal. |
| 166 | + cleanup-preview: |
| 167 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
| 168 | + runs-on: ubuntu-latest |
| 169 | + permissions: |
| 170 | + contents: write |
| 171 | + concurrency: |
| 172 | + group: pages-${{ github.event.pull_request.number || github.ref }} |
| 173 | + cancel-in-progress: true |
| 174 | + steps: |
| 175 | + - uses: actions/checkout@v4 |
| 176 | + - name: Remove PR preview |
| 177 | + env: |
| 178 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 179 | + run: | |
| 180 | + git config user.name "github-actions[bot]" |
| 181 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 182 | + git fetch origin gh-pages:gh-pages 2>/dev/null || exit 0 |
| 183 | + tmp=$(mktemp -d) |
| 184 | + git worktree add "$tmp" gh-pages |
| 185 | + rm -rf "$tmp/pr-preview/pr-$PR_NUMBER" |
| 186 | + cd "$tmp" |
| 187 | + git add -A |
| 188 | + if git diff --cached --quiet; then echo "no preview to remove"; exit 0; fi |
| 189 | + git commit -m "Remove preview for PR #$PR_NUMBER" |
| 190 | + for i in 1 2 3 4 5; do |
| 191 | + git push origin gh-pages && break |
| 192 | + [ "$i" = 5 ] && { echo "::error::could not push gh-pages after 5 attempts"; exit 1; } |
| 193 | + echo "gh-pages moved underneath us — rebasing and retrying ($i/5)" |
| 194 | + git pull --rebase origin gh-pages || { git rebase --abort 2>/dev/null || true; echo "::error::gh-pages rebase conflict"; exit 1; } |
| 195 | + done |
0 commit comments