-
Notifications
You must be signed in to change notification settings - Fork 0
212 lines (187 loc) · 8.21 KB
/
Copy pathpreview.yml
File metadata and controls
212 lines (187 loc) · 8.21 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
name: Quarto Preview
on:
pull_request:
types:
- opened
- reopened
- synchronize
- closed
- labeled
- unlabeled
# Cancel an in-progress preview build when a newer event supersedes it on
# the same ref — otherwise a fresh push queues behind the stale build and
# the up-to-date preview is delayed until the obsolete one finishes (see
# d-morrison/rme#812). `github.ref` is stable across a PR's events, so all
# of a PR's preview runs share one group.
concurrency:
group: preview-${{ github.ref }}
cancel-in-progress: true
jobs:
build-deploy:
if: |
(github.event.action != 'labeled' && github.event.action != 'unlabeled') ||
contains(fromJson('["revealjs","pdf","docx","no-preview-highlights","clear-freezer"]'), github.event.label.name)
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
TZ: America/Los_Angeles
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history for git diff
- name: Set up Quarto
uses: quarto-dev/quarto-actions/setup@v2
with:
tinytex: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
- uses: r-lib/actions/setup-r-dependencies@v2
with:
packages: |
any::knitr
any::rmarkdown
any::tibble
- name: Restore Quarto freezer
if: ${{ !contains(github.event.pull_request.labels.*.name, 'clear-freezer') }}
id: quarto-freezer-cache
uses: actions/cache/restore@v4
with:
path: _freeze
key: ${{ runner.os }}-quarto-freezer-${{ github.event.pull_request.head.sha }}-${{ github.run_attempt }}
restore-keys: |
${{ runner.os }}-quarto-freezer-${{ github.event.pull_request.head.sha }}-
${{ runner.os }}-quarto-freezer-main-
${{ runner.os }}-quarto-freezer-
- name: Render HTML
uses: quarto-dev/quarto-actions/render@v2
with:
to: html
# Detect which chapters have changed and optionally disable highlighting
# based on the 'no-preview-highlights' PR label
- name: Detect changed chapters (based on rendered files)
run: python3 .github/scripts/detect-changed-chapters.py
env:
HTML_DIR: ./_site
# Set to 'true' if PR has 'no-preview-highlights' label
DISABLE_PREVIEW_HIGHLIGHTS: ${{ contains(github.event.pull_request.labels.*.name, 'no-preview-highlights') }}
- name: Inject preview metadata
if: env.PREVIEW_SHOW_HIGHLIGHTS == 'true'
run: python3 .github/scripts/inject-preview-metadata.py
- name: Re-render with metadata
if: env.PREVIEW_SHOW_HIGHLIGHTS == 'true'
uses: quarto-dev/quarto-actions/render@v2
with:
to: html
- name: Collect QMD files for optional renders
id: optional-render-files
run: |
files=()
if [ -f index.qmd ]; then
files+=(index.qmd)
fi
if [ -d chapters ]; then
while IFS= read -r chapter_file; do
files+=("$chapter_file")
done < <(find chapters -maxdepth 1 -type f -name '*.qmd' | sort)
fi
if [ ${#files[@]} -eq 0 ]; then
echo "has_files=false" >> "$GITHUB_OUTPUT"
exit 0
fi
{
echo "files<<EOF"
printf '%s\n' "${files[@]}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "has_files=true" >> "$GITHUB_OUTPUT"
- name: Render Reveal.js slides
if: contains(github.event.pull_request.labels.*.name, 'revealjs') && steps.optional-render-files.outputs.has_files == 'true'
run: |
while IFS= read -r file; do
[ -n "$file" ] || continue
echo "Rendering Reveal.js for $file"
quarto render "$file" --to revealjs
done <<< "${{ steps.optional-render-files.outputs.files }}"
- name: Render PDF
if: contains(github.event.pull_request.labels.*.name, 'pdf') && steps.optional-render-files.outputs.has_files == 'true'
run: |
while IFS= read -r file; do
[ -n "$file" ] || continue
echo "Rendering PDF for $file"
quarto render "$file" --to pdf
done <<< "${{ steps.optional-render-files.outputs.files }}"
- name: Render DOCX
if: contains(github.event.pull_request.labels.*.name, 'docx') && steps.optional-render-files.outputs.has_files == 'true'
run: |
while IFS= read -r file; do
[ -n "$file" ] || continue
echo "Rendering DOCX for $file"
quarto render "$file" --to docx
done <<< "${{ steps.optional-render-files.outputs.files }}"
- name: Install python-docx for DOCX comparison
if: contains(github.event.pull_request.labels.*.name, 'docx')
run: pip install python-docx
- name: Highlight HTML content changes
if: env.PREVIEW_SHOW_HIGHLIGHTS == 'true'
run: |
echo "Changed chapters:"
echo "${{ env.PREVIEW_CHANGED_CHAPTERS }}"
python3 .github/scripts/highlight-html-changes.py
env:
HTML_DIR: ./_site
PREVIEW_CHANGED_CHAPTERS: ${{ env.PREVIEW_CHANGED_CHAPTERS }}
- name: Create DOCX with tracked changes
if: contains(github.event.pull_request.labels.*.name, 'docx')
run: python3 .github/scripts/create-docx-tracked-changes.py
env:
DOCX_DIR: ./_site
- name: Add home page banner
run: python3 .github/scripts/add-home-banner.py
env:
HTML_DIR: ./_site
PREVIEW_CHANGED_CHAPTERS: ${{ env.PREVIEW_CHANGED_CHAPTERS }}
- name: Save Quarto freezer
uses: actions/cache/save@v4
with:
path: _freeze
key: ${{ runner.os }}-quarto-freezer-${{ github.event.pull_request.head.sha }}-${{ github.run_attempt }}
- name: Deploy PR Preview
id: preview-step
if: github.event_name == 'pull_request'
uses: rossjrw/pr-preview-action@v1
with:
source-dir: ./_site/
# Deploy on label changes too, not just open/reopen/synchronize
# The 'auto' action ignores label events, so we explicitly set 'deploy' for non-closed PRs
action: ${{ github.event.action == 'closed' && 'remove' || 'deploy' }}
preview-branch: gh-pages
comment: "false"
wait-for-pages-deployment: false
# Create sticky comment with preview link
# Following https://github.com/rossjrw/pr-preview-action?tab=readme-ov-file#customise-the-sticky-comment
- uses: marocchino/sticky-pull-request-comment@v2
if: steps.preview-step.outputs.deployment-action == 'deploy' && env.deployment_status == 'success'
with:
header: pr-preview
recreate: true
message: |
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
:---:
| <p><img src="https://qr.rossjrw.com/?url=${{ steps.preview-step.outputs.preview-url }}" height="100" align="right" alt="QR code for preview link"></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }} <br><br>
| <h6>Built to branch [`gh-pages`](${{ github.server_url }}/${{ github.repository }}/tree/gh-pages) at ${{ steps.preview-step.outputs.action-start-time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ github.repository }}/deployments) is complete. <br><br> </h6>
- uses: marocchino/sticky-pull-request-comment@v2
if: steps.preview-step.outputs.deployment-action == 'remove' && env.deployment_status == 'success'
with:
header: pr-preview
recreate: true
message: |
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
:---:
Preview removed because the pull request was closed.
${{ steps.preview-step.outputs.action-start-time }}