|
| 1 | +on: |
| 2 | + push: |
| 3 | + branches: |
| 4 | + - main |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + publish: |
| 8 | + description: "Deploy to Pages (manual runs only)" |
| 9 | + type: boolean |
| 10 | + default: false |
| 11 | + |
| 12 | +name: Render and Publish |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +jobs: |
| 18 | + build: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Check out repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 # need history for diffing against main |
| 26 | + |
| 27 | + - name: Normalize QMD mtimes to last commit |
| 28 | + run: | |
| 29 | + set -euo pipefail |
| 30 | + # Set each tracked .qmd file's mtime to its last git commit time to avoid false "newer than HTML" renders. |
| 31 | + git ls-files -z -- '*.qmd' | xargs -0 -I{} sh -c 'ts=$(git log -1 --format=%ct -- "{}" || true); if [ -n "$ts" ]; then touch -d "@$ts" "{}"; fi' |
| 32 | +
|
| 33 | + - name: Download previous artifact |
| 34 | + if: github.event_name == 'push' |
| 35 | + id: artifact_status |
| 36 | + env: |
| 37 | + GH_TOKEN: ${{ github.token }} |
| 38 | + REPO: ${{ github.repository }} |
| 39 | + run: | |
| 40 | + set -euo pipefail |
| 41 | + latest_run=$(gh api "/repos/$REPO/actions/workflows/render-and-publish.yml/runs?branch=main&status=success&per_page=1" --jq '.workflow_runs[0].id' || true) |
| 42 | + if [ -z "${latest_run:-}" ]; then |
| 43 | + echo "No successful main run found; skipping artifact download." |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | + artifact_id=$(gh api "/repos/$REPO/actions/runs/$latest_run/artifacts" --jq '.artifacts[] | select(.name=="site-build") | .id' | head -n1 || true) |
| 47 | + if [ -z "${artifact_id:-}" ]; then |
| 48 | + echo "No site-build artifact found; will do full rebuild." |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | + echo "Downloading site-build artifact from run $latest_run (artifact $artifact_id)." |
| 52 | + gh api "/repos/$REPO/actions/artifacts/$artifact_id/zip" > /tmp/site.zip |
| 53 | + # -n keeps any checked-in sources from being overwritten by restored artifact content. |
| 54 | + unzip -qn /tmp/site.zip -d site |
| 55 | + echo "Contents of site/ after artifact extraction:" |
| 56 | + ls -la site/ | head -50 |
| 57 | + if [ -d site/_site ]; then |
| 58 | + echo "site/_site present after restore." |
| 59 | + echo "site_ok=true" >> "$GITHUB_OUTPUT" |
| 60 | + else |
| 61 | + echo "site/_site missing after restore; will rely on full rebuild." |
| 62 | + fi |
| 63 | +
|
| 64 | + - name: Set up Python |
| 65 | + uses: actions/setup-python@v5 |
| 66 | + with: |
| 67 | + python-version: '3.10' |
| 68 | + - name: Install dependencies |
| 69 | + run: | |
| 70 | + pip install plotly kaleido |
| 71 | +
|
| 72 | + - name: Set up Java |
| 73 | + uses: actions/setup-java@v4 |
| 74 | + with: |
| 75 | + distribution: 'temurin' |
| 76 | + java-version: '21' |
| 77 | + |
| 78 | + - name: Set up Clojure |
| 79 | + uses: DeLaGuardo/setup-clojure@main |
| 80 | + with: |
| 81 | + cli: 'latest' |
| 82 | + |
| 83 | + - name: Cache clojure dependencies |
| 84 | + uses: actions/cache@v3 |
| 85 | + with: |
| 86 | + path: | |
| 87 | + ~/.m2/repository |
| 88 | + ~/.gitlibs |
| 89 | + ~/.deps.clj |
| 90 | + key: cljdeps-${{ runner.os }} |
| 91 | + |
| 92 | + - name: Detect changed Clojure sources |
| 93 | + if: github.event_name == 'push' && steps.artifact_status.outputs.site_ok == 'true' |
| 94 | + id: changed_clj |
| 95 | + env: |
| 96 | + GH_TOKEN: ${{ github.token }} |
| 97 | + REPO: ${{ github.repository }} |
| 98 | + run: | |
| 99 | + set -euo pipefail |
| 100 | + git fetch origin main:refs/remotes/origin/main |
| 101 | + # Get the commit of the previous successful run (where the artifact came from) |
| 102 | + latest_commit=$(gh api "/repos/$REPO/actions/workflows/render-and-publish.yml/runs?branch=main&status=success&per_page=1" --jq '.workflow_runs[0].head_commit.id' || true) |
| 103 | + if [ -z "${latest_commit:-}" ] || [ "${latest_commit}" = "null" ]; then |
| 104 | + echo "No previous commit found; doing full rebuild." |
| 105 | + exit 0 |
| 106 | + fi |
| 107 | + echo "Comparing against previous artifact commit: $latest_commit" |
| 108 | + changed=$(git diff --name-only --diff-filter=ACMR "$latest_commit" HEAD -- 'src/**/*.clj' | tr '\n' ' ') |
| 109 | + if [ -z "$changed" ]; then |
| 110 | + echo "No changed Clojure sources detected." |
| 111 | + exit 0 |
| 112 | + fi |
| 113 | + echo "Changed Clojure source files:" $changed |
| 114 | + echo "files=$changed" >> "$GITHUB_OUTPUT" |
| 115 | +
|
| 116 | + - name: Build notebooks (changed set or full when none) |
| 117 | + run: clojure -M:clay -A:markdown ${{ steps.changed_clj.outputs.files }} |
| 118 | + |
| 119 | + - name: Set up Quarto |
| 120 | + uses: quarto-dev/quarto-actions/setup@v2 |
| 121 | + with: |
| 122 | + tinytex: true |
| 123 | + |
| 124 | + - name: Render Quarto |
| 125 | + uses: quarto-dev/quarto-actions/render@v2 |
| 126 | + with: |
| 127 | + path: site |
| 128 | + |
| 129 | + - name: Upload artifact |
| 130 | + uses: actions/upload-pages-artifact@v3 |
| 131 | + with: |
| 132 | + path: site/_site |
| 133 | + |
| 134 | + - name: Upload site artifact |
| 135 | + uses: actions/upload-artifact@v4 |
| 136 | + with: |
| 137 | + name: site-build |
| 138 | + path: site/ |
| 139 | + retention-days: 90 |
| 140 | + |
| 141 | + deploy: |
| 142 | + needs: build |
| 143 | + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true' && github.ref == 'refs/heads/main') |
| 144 | + permissions: |
| 145 | + pages: write |
| 146 | + id-token: write |
| 147 | + environment: |
| 148 | + name: github-pages |
| 149 | + url: ${{ steps.deployment.outputs.page_url }} |
| 150 | + runs-on: ubuntu-latest |
| 151 | + steps: |
| 152 | + - name: Deploy to GitHub Pages |
| 153 | + id: deployment |
| 154 | + uses: actions/deploy-pages@v4 |
0 commit comments