Repository navigation
Pr tutorial update #2
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: Convert FODT to PDF | |
| # Convert added/changed .fodt tutorials to PDF with LibreOffice. | |
| # - pull_request: build + upload PDF artifacts (no commit) | |
| # - push: build, upload artifacts, and commit PDFs back to the branch | |
| # - workflow_dispatch: manual rebuild | |
| on: | |
| pull_request: | |
| paths: | |
| - "**.fodt" | |
| - "build_tools/**" | |
| - ".github/workflows/fodt-to-pdf.yml" | |
| push: | |
| paths: | |
| - "**.fodt" | |
| workflow_dispatch: | |
| inputs: | |
| all_fodt: | |
| description: "Convert every tracked .fodt file (not only files from the latest commit)" | |
| type: boolean | |
| default: true | |
| concurrency: | |
| group: fodt-to-pdf-${{ github.event.pull_request.number || github.ref }} | |
| cancel-in-progress: true | |
| # Static permissions only (expressions are not allowed here). | |
| # Commit/push is still gated to non-PR push events in the job below. | |
| permissions: | |
| contents: write | |
| jobs: | |
| convert: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 60 | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| # Full history so we can diff against the PR base or previous push. | |
| fetch-depth: 0 | |
| - name: Install LibreOffice | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y --no-install-recommends \ | |
| libreoffice-writer \ | |
| default-jre-headless | |
| soffice --version | |
| - name: Collect FODT files to convert | |
| id: fodt | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if [[ "${{ github.event_name }}" == "pull_request" ]]; then | |
| BASE_SHA="${{ github.event.pull_request.base.sha }}" | |
| HEAD_SHA="${{ github.event.pull_request.head.sha }}" | |
| # Triple-dot: changes on the PR branch since it diverged from base. | |
| mapfile -t FILES < <(git diff --name-only --diff-filter=AM "${BASE_SHA}...${HEAD_SHA}" -- '*.fodt' || true) | |
| elif [[ "${{ github.event_name }}" == "push" ]]; then | |
| BEFORE="${{ github.event.before }}" | |
| if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then | |
| mapfile -t FILES < <(git ls-files '*.fodt') | |
| else | |
| mapfile -t FILES < <(git diff --name-only --diff-filter=AM "$BEFORE" "${{ github.sha }}" -- '*.fodt' || true) | |
| fi | |
| elif [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ inputs.all_fodt }}" != "true" ]]; then | |
| mapfile -t FILES < <(git diff --name-only --diff-filter=AM HEAD~1 HEAD -- '*.fodt' || true) | |
| else | |
| # workflow_dispatch with all_fodt=true | |
| mapfile -t FILES < <(git ls-files '*.fodt') | |
| fi | |
| COUNT=0 | |
| : > /tmp/fodt_list.txt | |
| for f in "${FILES[@]+"${FILES[@]}"}"; do | |
| [[ -z "${f:-}" ]] && continue | |
| [[ -f "$f" ]] || continue | |
| echo "$f" >> /tmp/fodt_list.txt | |
| COUNT=$((COUNT + 1)) | |
| done | |
| echo "count=$COUNT" >> "$GITHUB_OUTPUT" | |
| { | |
| echo "files<<EOF" | |
| cat /tmp/fodt_list.txt | |
| echo "EOF" | |
| } >> "$GITHUB_OUTPUT" | |
| echo "FODT files to convert ($COUNT):" | |
| cat /tmp/fodt_list.txt || true | |
| - name: Skip notice | |
| if: steps.fodt.outputs.count == '0' | |
| run: | | |
| echo "No matching .fodt files to convert for this event." | |
| echo "On pull requests this is OK when only build tooling/workflow files changed." | |
| - name: Convert FODT to PDF | |
| if: steps.fodt.outputs.count != '0' | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| chmod +x build_tools/convert_fodt_to_pdf.sh build_tools/build_pdfs.sh | |
| mapfile -t FILES < /tmp/fodt_list.txt | |
| ./build_tools/build_pdfs.sh "${FILES[@]}" | |
| echo "Generated PDFs:" | |
| while IFS= read -r fodt; do | |
| pdf="${fodt%.fodt}.pdf" | |
| ls -lh "$pdf" | |
| done < /tmp/fodt_list.txt | |
| - name: Stage PDF artifacts | |
| if: steps.fodt.outputs.count != '0' | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| mkdir -p /tmp/pdf-artifacts | |
| while IFS= read -r fodt; do | |
| pdf="${fodt%.fodt}.pdf" | |
| if [[ -f "$pdf" ]]; then | |
| mkdir -p "/tmp/pdf-artifacts/$(dirname "$pdf")" | |
| cp "$pdf" "/tmp/pdf-artifacts/$pdf" | |
| fi | |
| done < /tmp/fodt_list.txt | |
| find /tmp/pdf-artifacts -type f -name '*.pdf' -ls | |
| - name: Upload PDF artifacts | |
| if: steps.fodt.outputs.count != '0' | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: tutorial-pdfs-${{ github.event.pull_request.number || github.run_id }} | |
| path: /tmp/pdf-artifacts | |
| if-no-files-found: warn | |
| retention-days: 14 | |
| - name: Commit and push PDFs | |
| if: >- | |
| steps.fodt.outputs.count != '0' | |
| && github.event_name == 'push' | |
| && github.ref_type == 'branch' | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| while IFS= read -r fodt; do | |
| pdf="${fodt%.fodt}.pdf" | |
| if [[ -f "$pdf" ]]; then | |
| git add -- "$pdf" | |
| fi | |
| done < /tmp/fodt_list.txt | |
| if git diff --cached --quiet; then | |
| echo "No PDF changes to commit" | |
| exit 0 | |
| fi | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git commit -m "Auto-convert tutorial FODT to PDF" | |
| # Checkout is detached at the pushed SHA; push back to the branch name. | |
| git push origin "HEAD:${{ github.ref_name }}" |