|
1 |
| -# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples |
2 |
| -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help |
3 |
| - |
4 |
| -# Set up renv following instructions at https://www.emilhvitfeldt.com/post/bookdown-netlify-github-actions/ |
5 | 1 | on:
|
6 | 2 | push:
|
7 |
| - branches: [main, master] |
| 3 | + branches: [main, master, v2] |
8 | 4 | pull_request:
|
9 |
| - branches: [main, master] |
| 5 | + branches: [main, master, v2] |
10 | 6 | workflow_dispatch:
|
11 | 7 |
|
12 | 8 | name: bookdown
|
13 | 9 |
|
14 | 10 | jobs:
|
15 | 11 | bookdown:
|
16 | 12 | runs-on: ubuntu-latest
|
17 |
| - # Only restrict concurrency for non-PR jobs |
18 | 13 | concurrency:
|
19 | 14 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
|
20 | 15 | env:
|
21 |
| - GITHUB_PAT: ${{ secrets.GH_PAT }} |
| 16 | + GITHUB_PAT: ${{ secrets.GITHUB_PAT }} |
22 | 17 | EMAIL: ${{ secrets.EMAIL }}
|
23 | 18 | steps:
|
24 |
| - - uses: actions/checkout@v3 |
| 19 | + # Checkout repository |
| 20 | + - uses: actions/checkout@v4 |
25 | 21 |
|
| 22 | + # Setup Pandoc |
26 | 23 | - uses: r-lib/actions/setup-pandoc@v2
|
27 | 24 |
|
| 25 | + # Setup R environment with RSPM enabled |
28 | 26 | - uses: r-lib/actions/setup-r@v2
|
29 | 27 | with:
|
30 | 28 | use-public-rspm: true
|
31 | 29 |
|
| 30 | + # Restore cached package and TinyTeX state files |
| 31 | + - name: Cache package state |
| 32 | + uses: actions/cache@v4 |
| 33 | + with: |
| 34 | + path: | |
| 35 | + installed_packages.rds |
| 36 | + tinytex_packages.rds |
| 37 | + key: package-state-${{ runner.os }}-${{ hashFiles('**/renv.lock') }} |
| 38 | + restore-keys: package-state-${{ runner.os }}- |
| 39 | + |
| 40 | + # Check and install R packages if needed |
| 41 | + - name: Check and Install R Packages |
| 42 | + run: | |
| 43 | + R -e ' |
| 44 | + if (file.exists("installed_packages.rds")) { |
| 45 | + saved_pkgs <- readRDS("installed_packages.rds"); |
| 46 | + current_pkgs <- installed.packages(); |
| 47 | + if (!identical(saved_pkgs[,1], current_pkgs[,1])) { |
| 48 | + missing_pkgs <- setdiff(rownames(saved_pkgs), rownames(current_pkgs)); |
| 49 | + if (length(missing_pkgs) > 0) install.packages(missing_pkgs); |
| 50 | + } else { |
| 51 | + cat("No package changes detected.\n"); |
| 52 | + } |
| 53 | + } else { |
| 54 | + install.packages(c("tidyverse", "bookdown")); |
| 55 | + }' |
| 56 | + env: |
| 57 | + GITHUB_PAT: ${{ secrets.GITHUB_PAT }} |
| 58 | + |
| 59 | + # Install and cache TinyTeX only if needed |
| 60 | + - name: Install TinyTeX if Needed |
| 61 | + run: | |
| 62 | + if ! command -v tlmgr &> /dev/null; then |
| 63 | + R -e 'install.packages("tinytex"); tinytex::install_tinytex()'; |
| 64 | + R -e 'tinytex::tlmgr_install(c("xetex", "fontspec", "xunicode", "geometry", "fancyhdr", "titlesec", "titling", "tocloft", "sectsty", "environ", "trimspaces", "wrapfig", "multirow", "colortbl", "tabularx", "varwidth", "footnote", "threeparttable", "enumitem", "footmisc", "fncychap", "hyphenat", "biblatex", "biber"))'; |
| 65 | + else |
| 66 | + echo "TinyTeX is already installed"; |
| 67 | + fi |
| 68 | + shell: bash |
| 69 | + |
| 70 | + # Cache TinyTeX installation |
| 71 | + - name: Cache TinyTeX installation |
| 72 | + uses: actions/cache@v4 |
| 73 | + with: |
| 74 | + path: ~/.TinyTeX |
| 75 | + key: tinytex-${{ runner.os }}-${{ hashFiles('**/*.tex') }} |
| 76 | + restore-keys: tinytex- |
| 77 | + |
| 78 | + # Install fonts only if needed |
| 79 | + - name: Check and Install Fonts |
| 80 | + run: | |
| 81 | + if ! fc-list | grep -q "Inconsolata"; then |
| 82 | + sudo apt-get update; |
| 83 | + sudo apt-get install -y fonts-inconsolata fonts-nanum; |
| 84 | + sudo fc-cache -fv; |
| 85 | + else |
| 86 | + echo "Fonts are already installed"; |
| 87 | + fi |
| 88 | + shell: bash |
| 89 | + |
| 90 | + # Setup renv if needed |
32 | 91 | - uses: r-lib/actions/setup-renv@v2
|
33 | 92 |
|
| 93 | + # Ensure _bookdown_files directory exists |
| 94 | + - name: Ensure _bookdown_files directory exists |
| 95 | + run: mkdir -p _bookdown_files |
| 96 | + |
| 97 | + # Cache bookdown results |
34 | 98 | - name: Cache bookdown results
|
35 |
| - uses: actions/cache@v3 |
| 99 | + uses: actions/cache@v4 |
36 | 100 | with:
|
37 | 101 | path: _bookdown_files
|
38 | 102 | key: bookdown-${{ hashFiles('**/*Rmd') }}
|
39 | 103 | restore-keys: bookdown-
|
40 | 104 |
|
41 |
| - - name: Build site |
42 |
| - run: bookdown::render_book("index.Rmd", quiet = TRUE) |
43 |
| - shell: Rscript {0} |
| 105 | + # Check if bookdown::pdf_book is enabled (not commented out) in _output.yml |
| 106 | + - name: Check for uncommented pdf_book in _output.yml |
| 107 | + id: check_pdf |
| 108 | + run: | |
| 109 | + if grep -qE '^[^#]*bookdown::pdf_book' _output.yml; then |
| 110 | + echo "PDF book is enabled."; |
| 111 | + echo "build_pdf=true" >> $GITHUB_ENV; |
| 112 | + else |
| 113 | + echo "PDF book is not enabled."; |
| 114 | + echo "build_pdf=false" >> $GITHUB_ENV; |
| 115 | + fi |
| 116 | +
|
| 117 | + # Build PDF book if enabled |
| 118 | + - name: Build PDF book |
| 119 | + if: env.build_pdf == 'true' |
| 120 | + run: | |
| 121 | + R -e 'bookdown::render_book("index.Rmd", "bookdown::pdf_book")' |
| 122 | +
|
| 123 | + # Build HTML books |
| 124 | + - name: Build HTML gitbook book |
| 125 | + if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') || github.event_name == 'pull_request' |
| 126 | + run: | |
| 127 | + R -e 'bookdown::render_book("index.Rmd", "bookdown::gitbook")' |
| 128 | +
|
| 129 | + - name: Build HTML bs4_book book |
| 130 | + if: github.ref == 'refs/heads/v2' || github.event_name == 'pull_request' |
| 131 | + run: | |
| 132 | + R -e 'bookdown::render_book("index.Rmd", "bookdown::bs4_book")' |
| 133 | +
|
| 134 | + # Save R package state and TinyTeX installation to RDS files |
| 135 | + - name: Save R Package and TinyTeX State |
| 136 | + run: | |
| 137 | + R -e 'saveRDS(installed.packages(), file = "installed_packages.rds")'; |
| 138 | + R -e 'tinytex_packages <- tinytex::tl_pkgs(); saveRDS(tinytex_packages, file = "tinytex_packages.rds")' |
| 139 | +
|
| 140 | + # Deploy HTML to GitHub Pages if on main or master branch |
| 141 | + - name: Deploy to GitHub pages from main/master 🚀 |
| 142 | + if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') |
| 143 | + |
| 144 | + with: |
| 145 | + branch: gh-pages |
| 146 | + folder: docs |
| 147 | + clean: false |
| 148 | + |
| 149 | + # Deploy to v2-publish if on v2 branch |
| 150 | + - name: Deploy to v2-publish branch from v2 🚀 |
| 151 | + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/v2' |
| 152 | + |
| 153 | + with: |
| 154 | + branch: v2-publish |
| 155 | + folder: docs |
| 156 | + clean: false |
44 | 157 |
|
45 |
| - - name: Deploy to GitHub pages 🚀 |
46 |
| - if: github.event_name != 'pull_request' |
47 |
| - |
| 158 | + # Deploy to /v2 subdirectory of gh-pages from v2 branch |
| 159 | + - name: Deploy to v2 subdirectory of gh-pages 🚀 |
| 160 | + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/v2' |
| 161 | + |
48 | 162 | with:
|
49 | 163 | branch: gh-pages
|
50 | 164 | folder: docs
|
| 165 | + target-folder: v2 # Specify the target subdirectory |
| 166 | + clean: false |
0 commit comments