-
Notifications
You must be signed in to change notification settings - Fork 3.6k
382 lines (331 loc) Β· 16.3 KB
/
Copy pathbook-preview-dev.yml
File metadata and controls
382 lines (331 loc) Β· 16.3 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
name: 'π Book Β· ποΈ Preview (Dev)'
# =============================================================================
# ποΈ Preview (Dev) β Deploy dev build artifacts to staging site
# =============================================================================
#
# Downloads HTML/PDF/EPUB artifacts from a successful Validate run and deploys
# them to the dev preview site via SSH push to the target repository.
#
# Flow:
# 1. RESOLVE_RUN β Find the correct Validate workflow run (auto or manual)
# 2. DOWNLOAD β Pull Vol1 + Vol2 artifacts (HTML, PDF, EPUB)
# 3. ASSEMBLE_SITE β Build preview site with volume chooser and downloads
# 4. DEPLOY β Push to dev preview repo via SSH deploy key
#
# Triggers:
# - workflow_run: on successful completion of Validate (Dev)
# - workflow_dispatch: manual deploy of a specific commit or run ID
#
# Deploys to: Dev preview site (via SSH to DEV_REPO)
# Secrets: GITHUB_TOKEN, SSH_DEPLOY_KEY
# Vars: BOOK_TOOLS, DEV_REPO, DEV_REPO_URL,
# VOL1_DEPLOY_PATH (top-level dir for Vol I, mirrors live),
# VOL2_DEPLOY_PATH (top-level dir for Vol II, mirrors live)
#
# Related:
# - book-validate-dev.yml β Produces the build artifacts this workflow deploys
# - book-publish-live.yml β Production equivalent (deploys to gh-pages)
#
# =============================================================================
on:
workflow_dispatch:
inputs:
commit_sha:
description: 'Optional: Specific commit SHA to deploy. Deploys the latest successful `dev` build if empty. IMPORTANT: Select the `dev` branch before running.'
required: false
type: string
run_id:
description: 'Optional: Specific validate workflow run ID to pull artifacts from (bypasses success check).'
required: false
type: string
workflow_run:
workflows: ["π Book Β· β
Validate (Dev)"]
types:
- completed
concurrency:
group: deploy-preview-${{ github.ref }}
cancel-in-progress: true
jobs:
deploy:
name: 'πΊ Deploy to GitHub Pages (Dev Preview)'
if: (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'dev') || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
# Deploys dev branch to: https://harvard-edge.github.io/cs249r_book_dev/
permissions:
contents: write # Allow write access to repository
pages: write
actions: read # Allow reading of workflow runs
steps:
- name: β¬οΈ Get workflow run ID
id: run_info
run: |
if [[ "${{ github.event_name }}" == "workflow_run" ]]; then
echo "run_id=${{ github.event.workflow_run.id }}" >> $GITHUB_OUTPUT
echo "head_sha=${{ github.event.workflow_run.head_sha }}" >> $GITHUB_OUTPUT
exit 0
fi
# If a specific run_id is provided, use it directly (bypasses success check)
RUN_ID_INPUT=$(echo "${{ github.event.inputs.run_id }}" | xargs)
if [[ -n "$RUN_ID_INPUT" ]]; then
echo "π Using provided run ID: $RUN_ID_INPUT"
echo "run_id=$RUN_ID_INPUT" >> $GITHUB_OUTPUT
RUN_SHA=$(gh api repos/${{ github.repository }}/actions/runs/${RUN_ID_INPUT} --jq '.head_sha')
echo "head_sha=$RUN_SHA" >> $GITHUB_OUTPUT
exit 0
fi
COMMIT_SHA=$(echo "${{ github.event.inputs.commit_sha }}" | xargs)
API_URL="repos/${{ github.repository }}/actions/workflows/book-validate-dev.yml/runs"
if [[ -n "$COMMIT_SHA" ]]; then
echo "π Searching for successful 'Validate Dev' run for commit starting with: $COMMIT_SHA"
JQ_QUERY=".workflow_runs[] | select((.head_sha | startswith(\"$COMMIT_SHA\")) and .status == \"completed\" and .conclusion == \"success\") | .id"
else
echo "π No commit SHA provided. Searching for the latest successful 'Validate Dev' run on 'dev' branch."
JQ_QUERY=".workflow_runs[] | select(.head_branch == \"dev\" and .status == \"completed\" and .conclusion == \"success\") | .id"
fi
latest_run_id=$(gh api "$API_URL" --jq "$JQ_QUERY" | head -n 1)
if [ -z "$latest_run_id" ]; then
if [[ -n "$COMMIT_SHA" ]]; then
echo "::error::Could not find a successful 'Validate Dev' run for commit ${COMMIT_SHA}."
else
echo "::error::Could not find any successful 'Validate Dev' run on the 'dev' branch."
fi
exit 1
fi
echo "Found run ID: ${latest_run_id}"
echo "run_id=${latest_run_id}" >> $GITHUB_OUTPUT
latest_run_sha=$(gh api repos/${{ github.repository }}/actions/runs/${latest_run_id} --jq '.head_sha')
echo "head_sha=${latest_run_sha}" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: π₯ Download Vol1 HTML Artifact
uses: actions/download-artifact@v8
with:
name: dev-html-vol1-linux
run-id: ${{ steps.run_info.outputs.run_id }}
path: ./html-vol1-artifact
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: π₯ Download Vol2 HTML Artifact
uses: actions/download-artifact@v8
with:
name: dev-html-vol2-linux
run-id: ${{ steps.run_info.outputs.run_id }}
path: ./html-vol2-artifact
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: π₯ Download Vol1 PDF Artifact
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: dev-pdf-vol1-linux
run-id: ${{ steps.run_info.outputs.run_id }}
path: ./pdf-vol1-artifact
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: π₯ Download Vol2 PDF Artifact
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: dev-pdf-vol2-linux
run-id: ${{ steps.run_info.outputs.run_id }}
path: ./pdf-vol2-artifact
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: π₯ Download Vol1 EPUB Artifact
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: dev-epub-vol1-linux
run-id: ${{ steps.run_info.outputs.run_id }}
path: ./epub-vol1-artifact
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: π₯ Download Vol2 EPUB Artifact
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: dev-epub-vol2-linux
run-id: ${{ steps.run_info.outputs.run_id }}
path: ./epub-vol2-artifact
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: π¦ Build volume preview site
run: |
echo "π¦ Preparing volume preview site..."
mkdir -p ./preview-site/vol1 ./preview-site/vol2
echo "π Vol1 artifact contents:"
ls -la ./html-vol1-artifact/ 2>/dev/null || echo " (empty or missing)"
echo "π Vol2 artifact contents:"
ls -la ./html-vol2-artifact/ 2>/dev/null || echo " (empty or missing)"
# Use rsync-style copy that handles both flat and nested artifact layouts
cp -r ./html-vol1-artifact/. ./preview-site/vol1/
cp -r ./html-vol2-artifact/. ./preview-site/vol2/
mkdir -p ./preview-site/vol1/assets/downloads ./preview-site/vol2/assets/downloads
# Copy PDF/EPUB downloads by exact name (artifact dirs contain many files;
# a wildcard find can pick up a tiny figure PDF instead of the book)
VOL1_PDF=$(find ./pdf-vol1-artifact -name "Machine-Learning-Systems-Vol1.pdf" -type f 2>/dev/null | head -1)
[ -n "$VOL1_PDF" ] && cp "$VOL1_PDF" "./preview-site/vol1/assets/downloads/Machine-Learning-Systems-Vol1.pdf" || true
VOL1_EPUB=$(find ./epub-vol1-artifact -name "Machine-Learning-Systems-Vol1.epub" -type f 2>/dev/null | head -1)
[ -n "$VOL1_EPUB" ] && cp "$VOL1_EPUB" "./preview-site/vol1/assets/downloads/Machine-Learning-Systems-Vol1.epub" || true
VOL2_PDF=$(find ./pdf-vol2-artifact -name "Machine-Learning-Systems-Vol2.pdf" -type f 2>/dev/null | head -1)
[ -n "$VOL2_PDF" ] && cp "$VOL2_PDF" "./preview-site/vol2/assets/downloads/Machine-Learning-Systems-Vol2.pdf" || true
VOL2_EPUB=$(find ./epub-vol2-artifact -name "Machine-Learning-Systems-Vol2.epub" -type f 2>/dev/null | head -1)
[ -n "$VOL2_EPUB" ] && cp "$VOL2_EPUB" "./preview-site/vol2/assets/downloads/Machine-Learning-Systems-Vol2.epub" || true
- name: π Create .nojekyll file
run: touch ./preview-site/.nojekyll
- name: π¦ Inspect final site contents
run: |
echo "::group::Site Contents"
ls -la ./preview-site
echo "::endgroup::"
echo "::group::Vol1 Downloads"
if [ -d "./preview-site/vol1/assets/downloads" ]; then
ls -lh ./preview-site/vol1/assets/downloads
else
echo "β οΈ No vol1/assets/downloads directory"
fi
echo "::endgroup::"
echo "::group::Vol2 Downloads"
if [ -d "./preview-site/vol2/assets/downloads" ]; then
ls -lh ./preview-site/vol2/assets/downloads
else
echo "β οΈ No vol2/assets/downloads directory"
fi
echo "::endgroup::"
# Sanity check: PDFs should be >1MB (a figure PDF would be <100KB)
for vol in vol1 vol2; do
PDF="./preview-site/$vol/assets/downloads/Machine-Learning-Systems-$(echo $vol | sed 's/vol/Vol/').pdf"
if [ -f "$PDF" ]; then
SIZE=$(stat -c%s "$PDF" 2>/dev/null || stat -f%z "$PDF" 2>/dev/null)
if [ "$SIZE" -lt 1000000 ]; then
echo "::error::$vol PDF is only ${SIZE} bytes β likely a figure, not the book!"
else
echo "β
$vol PDF looks correct: $(du -h "$PDF" | cut -f1)"
fi
fi
done
- name: β¬οΈ Checkout repository (for scripts)
uses: actions/checkout@v6
with:
ref: dev
path: ./repo-scripts
sparse-checkout: |
${{ vars.BOOK_TOOLS }}/scripts/publish/modify_dev_announcement.py
.github/scripts/rewrite-dev-urls.sh
.github/scripts/flatten-vol-urls.sh
sparse-checkout-cone-mode: false
- name: π§ Flatten volume URL structure
# Moves contents/vol{N}/* to the site root so preview URLs are
# devsite/vol{N}/{chapter}/{chapter}.html (not double-nested).
# Must run before rewrite-dev-urls so depth calculations are correct.
run: |
if [ -d "./preview-site/vol1" ]; then
bash ./repo-scripts/.github/scripts/flatten-vol-urls.sh ./preview-site/vol1 vol1
fi
if [ -d "./preview-site/vol2" ]; then
bash ./repo-scripts/.github/scripts/flatten-vol-urls.sh ./preview-site/vol2 vol2
fi
- name: π Rewrite URLs for dev site
run: |
bash ./repo-scripts/.github/scripts/rewrite-dev-urls.sh vol1 ./preview-site/vol1 --live-external
bash ./repo-scripts/.github/scripts/rewrite-dev-urls.sh vol2 ./preview-site/vol2 --live-external
- name: π§ Modify announcement for dev preview
run: |
echo "π§ Modifying announcement banner for development preview..."
# Get commit info for display
COMMIT_HASH="${{ steps.run_info.outputs.head_sha }}"
COMMIT_SHORT="${COMMIT_HASH:0:8}"
echo "π Adding dev preview banner with commit info: $COMMIT_SHORT"
python3 ./repo-scripts/${{ vars.BOOK_TOOLS }}/scripts/publish/modify_dev_announcement.py \
./preview-site \
--verbose \
--commit-hash "$COMMIT_HASH" \
--commit-short "$COMMIT_SHORT"
- name: π Deploy to Dev Site via SSH
env:
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
run: |
echo "π Starting ssh-agent..."
eval "$(ssh-agent -s)"
echo "$SSH_DEPLOY_KEY" | tr -d '\r' | ssh-add - > /dev/null
# Add github.com to known hosts to avoid prompt. Port 22 intermittently
# refuses connections from runners, so trust the 443 endpoint too and
# keep it available as a fallback rather than losing a whole build.
mkdir -p ~/.ssh
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null || true
ssh-keyscan -p 443 ssh.github.com >> ~/.ssh/known_hosts
echo "π§ Configuring git..."
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
echo "π Cloning target repository (${{ vars.DEV_REPO }})..."
clone_target() {
git clone --depth=1 "${{ vars.DEV_REPO_URL }}" target-repo
}
cloned=false
for attempt in 1 2 3; do
if clone_target; then
cloned=true
break
fi
rm -rf target-repo
echo "β οΈ Clone attempt ${attempt} failed."
if [ "$attempt" -eq 2 ]; then
echo "π Falling back to ssh.github.com:443 for the final attempt."
printf 'Host github.com\n Hostname ssh.github.com\n Port 443\n User git\n' >> ~/.ssh/config
fi
sleep $((attempt * 10))
done
if [ "$cloned" != true ]; then
echo "β Could not reach the dev site repository over SSH after 3 attempts."
exit 1
fi
echo "π Entering target repository..."
cd target-repo
# Per-volume top-level deploy paths. Mirrors book-publish-live.yml so
# /vol1/ and /vol2/ resolve identically on dev preview and live.
VOL1_PATH="${{ vars.VOL1_DEPLOY_PATH }}"
VOL2_PATH="${{ vars.VOL2_DEPLOY_PATH }}"
if [ -z "$VOL1_PATH" ] || [ -z "$VOL2_PATH" ]; then
echo "β vars.VOL1_DEPLOY_PATH or vars.VOL2_DEPLOY_PATH is unset β refusing to run destructive rm with empty path."
exit 1
fi
echo "π§Ή Cleaning /$VOL1_PATH/ and /$VOL2_PATH/ subdirectories (preserving other subsites)..."
rm -rf "$VOL1_PATH" "$VOL2_PATH"
# Also clean up the legacy /book/ wrapper from the previous nested layout.
# Keeping it would leave zombie content at harvard-edge.github.io/cs249r_book_dev/book/.
rm -rf book
mkdir -p "$VOL1_PATH" "$VOL2_PATH"
echo "π Copying Vol1 content to /$VOL1_PATH/..."
cp -r "${{ github.workspace }}/preview-site/vol1/." "$VOL1_PATH/"
echo "π Copying Vol2 content to /$VOL2_PATH/..."
cp -r "${{ github.workspace }}/preview-site/vol2/." "$VOL2_PATH/"
touch .nojekyll
echo "π Validating deployment content..."
if [ ! -f "$VOL1_PATH/index.html" ]; then
echo "β CRITICAL: $VOL1_PATH/index.html is missing after copy. Aborting deployment."
exit 1
fi
if [ ! -f "$VOL2_PATH/index.html" ]; then
echo "β CRITICAL: $VOL2_PATH/index.html is missing after copy. Aborting deployment."
exit 1
fi
echo "π¦ Repository structure:"
ls -la
echo ""
echo "π¦ /$VOL1_PATH/ contents:"
ls -la "$VOL1_PATH/" | head -10
echo ""
echo "π¦ /$VOL2_PATH/ contents:"
ls -la "$VOL2_PATH/" | head -10
echo "π¦ Committing and pushing changes..."
git add .
git commit -m "π Deploy book dev preview to /$VOL1_PATH/ + /$VOL2_PATH/ from ${{ steps.run_info.outputs.head_sha }}" --allow-empty || echo "π‘ Nothing to commit"
# Retry push with rebase to handle concurrent deploys
for i in 1 2 3; do
if git push origin main 2>/dev/null; then
echo "β
Push succeeded on attempt $i"
break
fi
echo "β οΈ Push failed (attempt $i/3), pulling with rebase..."
git pull --rebase origin main || true
done
# DEV_REPO contains org/repo format, extract just repo name for GitHub Pages URL
REPO_NAME="${{ vars.DEV_REPO }}"
REPO_NAME="${REPO_NAME##*/}"
echo "β
Volumes deployed to:"
echo " https://harvard-edge.github.io/${REPO_NAME}/$VOL1_PATH/"
echo " https://harvard-edge.github.io/${REPO_NAME}/$VOL2_PATH/"