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