Build LaTeX and Deploy PDFs #24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build LaTeX and Deploy PDFs | |
| on: | |
| push: | |
| branches: main | |
| paths: [docs/**] | |
| pull_request: | |
| branches: main | |
| paths: [docs/**] | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pages: write | |
| id-token: write | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Restore PDF cache | |
| uses: actions/cache@v4 | |
| with: | |
| path: build-pdf | |
| key: pdf-cache-${{ github.ref }}-${{ hashFiles('docs/**/*.sty', 'Makefile') }} | |
| restore-keys: | | |
| pdf-cache- | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get changed LaTeX files | |
| id: latex-files | |
| run: | | |
| CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} -- docs/**/*.tex | xargs) | |
| echo "Changed files: $CHANGED_FILES" | |
| echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV | |
| CHANGED_FILENAMES=$(for file in $CHANGED_FILES; do basename "$file"; done | xargs) | |
| echo "CHANGED_FILENAMES=$CHANGED_FILENAMES" >> $GITHUB_ENV | |
| FILE_COUNT=$(echo $CHANGED_FILENAMES | wc -w) | |
| if [ $FILE_COUNT -gt 1 ]; then | |
| PLURAL_S='s' | |
| else | |
| PLURAL_S='' | |
| fi | |
| echo "PLURAL_S=$PLURAL_S" >> $GITHUB_ENV | |
| - name: Compile LaTeX | |
| uses: xu-cheng/texlive-action@v2 | |
| with: | |
| scheme: full | |
| texlive_version: 2024 | |
| run: | | |
| apk add make | |
| PDF_CACHE_DIR="$GITHUB_WORKSPACE/build-pdf" | |
| mkdir -p "$PDF_CACHE_DIR" | |
| echo "PWD: $(pwd)" | |
| echo "Cache dir contents:" | |
| ls -la "$PDF_CACHE_DIR" || true | |
| # Bootstrap: full build if cache is empty | |
| if [ -z "${CHANGED_FILES}" ] && [ -z "$(ls -A "$PDF_CACHE_DIR" 2>/dev/null)" ]; then | |
| echo "No cached PDFs found; building all documents" | |
| CHANGED_FILES=$(find docs -name "*.tex") | |
| fi | |
| for file in $CHANGED_FILES; do | |
| # Build using existing Makefile rules | |
| make -B "${file%.tex}.pdf" | |
| # Copy result into build-pdf, preserving structure | |
| rel=$(dirname "$file" | cut -d'/' -f2-) | |
| mkdir -p "build-pdf/$rel" | |
| cp "${file%.tex}.pdf" "build-pdf/$rel/" | |
| done | |
| continue-on-error: true # Job continues even if this step fails | |
| - name: Prepare site | |
| run: | | |
| set -euo pipefail | |
| # Always start from repo root | |
| pwd | |
| ls | |
| # Recreate public directory | |
| rm -rf public | |
| mkdir -p public | |
| # Copy PDFs preserving structure | |
| rsync -av build-pdf/ public/ | |
| # Sanity check | |
| echo "PDFs found:" | |
| find public -name "*.pdf" || true | |
| # Generate PDF list (paths relative to site root) | |
| PDF_LIST=$(find public -name "*.pdf" | sort | sed 's|^public/||' | awk -F/ ' | |
| { | |
| dir = $1 | |
| file = $NF | |
| groups[dir] = groups[dir] "<li><a href=\"" $0 "\">" file "</a></li>\n" | |
| } | |
| END { | |
| for (dir in groups) { | |
| printf "<h2>%s</h2>\n<ul>\n%s</ul>\n", dir, groups[dir] | |
| } | |
| }') | |
| # Ensure site template exists | |
| test -f site/index.html | |
| # Build index.html safely | |
| awk -v list="$PDF_LIST" ' | |
| /<!-- PDF_LIST -->/ { print list; next } | |
| { print } | |
| ' site/index.html > public/index.html | |
| # Copy static assets | |
| cp site/style.css public/ | |
| - name: Upload Pages artifact | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: public | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: github-pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| steps: | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 |