-
Notifications
You must be signed in to change notification settings - Fork 298
418 lines (352 loc) · 14.9 KB
/
Copy pathnotebook-pr.yaml
File metadata and controls
418 lines (352 loc) · 14.9 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
name: notebook-pr
on:
pull_request:
branches: main
paths: "**/*.ipynb"
env:
NB_KERNEL: python
ORG: NeuromatchAcademy
NMA_REPO: course-content-dl
NMA_MAIN_BRANCH: main
jobs:
setup:
runs-on: ubuntu-latest
outputs:
notebooks: ${{ steps.set-matrix.outputs.notebooks }}
has_notebooks: ${{ steps.set-matrix.outputs.has_notebooks }}
commit_message: ${{ steps.get-commit.outputs.message }}
materials_changed: ${{ steps.materials-check.outputs.materials_changed }}
skip_ci: ${{ steps.check-skip.outputs.skip }}
steps:
- name: Checkout
uses: actions/checkout@v5
with:
persist-credentials: false
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Get commit message
id: get-commit
run: |
msg=$(git log -1 --pretty=format:"%s")
echo "message=$msg" >> $GITHUB_OUTPUT
- name: Check skip ci
id: check-skip
env:
COMMIT_MESSAGE: ${{ steps.get-commit.outputs.message }}
run: |
if [[ "$COMMIT_MESSAGE" == *"skip ci"* ]]; then
echo "skip=true" >> $GITHUB_OUTPUT
else
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Setup Python environment
if: steps.check-skip.outputs.skip != 'true'
uses: ./.github/actions/setup-python-env
- name: Setup CI tools
if: steps.check-skip.outputs.skip != 'true'
uses: ./.github/actions/setup-ci-tools
with:
commit-message: ${{ steps.get-commit.outputs.message }}
- name: Setup rendering dependencies
if: steps.check-skip.outputs.skip != 'true'
uses: ./.github/actions/setup-rendering-deps
- name: Get changed files
if: steps.check-skip.outputs.skip != 'true'
id: changed-files
uses: tj-actions/changed-files@v47.0.6
- name: Check if materials.yml changed
id: materials-check
if: steps.check-skip.outputs.skip != 'true'
run: |
if [[ "${{ steps.changed-files.outputs.all_changed_files }}" =~ materials\.yml ]]; then
echo "materials_changed=true" >> $GITHUB_OUTPUT
else
echo "materials_changed=false" >> $GITHUB_OUTPUT
fi
- name: Set notebook matrix
id: set-matrix
if: steps.check-skip.outputs.skip != 'true'
run: |
nbs=$(nmaci select-notebooks ${{ steps.changed-files.outputs.all_changed_files }})
if [ -z "$nbs" ]; then
echo "No notebooks to process"
echo "has_notebooks=false" >> $GITHUB_OUTPUT
echo "notebooks=[]" >> $GITHUB_OUTPUT
else
echo "Notebooks to process: $nbs"
echo "has_notebooks=true" >> $GITHUB_OUTPUT
json_array=$(echo "$nbs" | tr ' ' '\n' | jq -R . | jq -s -c .)
echo "notebooks=$json_array" >> $GITHUB_OUTPUT
fi
process:
needs: setup
if: needs.setup.outputs.skip_ci != 'true' && needs.setup.outputs.has_notebooks == 'true'
runs-on: ubuntu-latest
timeout-minutes: 180
strategy:
fail-fast: false
matrix:
notebook: ${{ fromJson(needs.setup.outputs.notebooks) }}
steps:
- name: Checkout
uses: actions/checkout@v5
with:
persist-credentials: false
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Setup Python environment
uses: ./.github/actions/setup-python-env
- name: Setup CI tools
uses: ./.github/actions/setup-ci-tools
with:
commit-message: ${{ needs.setup.outputs.commit_message }}
- name: Setup rendering dependencies
uses: ./.github/actions/setup-rendering-deps
- name: Process notebook
env:
COMMIT_MESSAGE: ${{ needs.setup.outputs.commit_message }}
PYTHONUNBUFFERED: "1"
run: |
echo "::group::Processing ${{ matrix.notebook }}"
echo "Started at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo "Job timeout: 120 minutes"
if [[ "$COMMIT_MESSAGE" == *"ci:check"* ]]; then
execflag="--check-execution"
echo "Mode: Check execution (no changes)"
else
execflag="--execute"
echo "Mode: Execute and process"
fi
echo "::endgroup::"
echo "::group::Notebook execution output"
nmaci process-notebooks "${{ matrix.notebook }}" $execflag
exit_code=$?
echo "::endgroup::"
echo "Completed at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
exit $exit_code
- name: Get tutorial directory
id: get-dir
run: |
nb="${{ matrix.notebook }}"
dir=$(dirname "$nb")
nb_name=$(basename "$nb" .ipynb)
echo "dir=$dir" >> $GITHUB_OUTPUT
echo "nb_name=$nb_name" >> $GITHUB_OUTPUT
safe_dir=$(echo "$dir" | sed 's|/|___|g')
echo "artifact_name=${safe_dir}___${nb_name}" >> $GITHUB_OUTPUT
- name: Upload processed tutorial directory
uses: actions/upload-artifact@v5
with:
name: tutorial-${{ steps.get-dir.outputs.artifact_name }}
path: |
${{ matrix.notebook }}
${{ steps.get-dir.outputs.dir }}/static/${{ steps.get-dir.outputs.nb_name }}*
${{ steps.get-dir.outputs.dir }}/solutions/${{ steps.get-dir.outputs.nb_name }}*
${{ steps.get-dir.outputs.dir }}/student/${{ steps.get-dir.outputs.nb_name }}*
${{ steps.get-dir.outputs.dir }}/instructor/${{ steps.get-dir.outputs.nb_name }}*
if-no-files-found: ignore
retention-days: 1
finalize:
needs: [setup, process]
if: always() && needs.setup.outputs.skip_ci != 'true' && needs.setup.outputs.has_notebooks == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
with:
persist-credentials: false
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Setup Python environment
uses: ./.github/actions/setup-python-env
- name: Setup CI tools
uses: ./.github/actions/setup-ci-tools
with:
commit-message: ${{ needs.setup.outputs.commit_message }}
- name: Download all artifacts
uses: actions/download-artifact@v5
with:
path: artifacts/
- name: Detect failed notebooks
id: detect-failures
run: |
expected='${{ needs.setup.outputs.notebooks }}'
failed_nbs=""
success_nbs=""
echo "Checking which notebooks were successfully processed..."
for nb in $(echo "$expected" | jq -r '.[]'); do
nb_name=$(basename "$nb" .ipynb)
if ls artifacts/tutorial-*"___${nb_name}" 1>/dev/null 2>&1; then
echo " ✓ $nb - SUCCESS"
success_nbs="$success_nbs $nb"
else
echo " ✗ $nb - FAILED (no artifact)"
failed_nbs="$failed_nbs $nb"
fi
done
success_nbs="${success_nbs# }"
failed_nbs="${failed_nbs# }"
echo "success_notebooks=$success_nbs" >> $GITHUB_OUTPUT
echo "failed_notebooks=$failed_nbs" >> $GITHUB_OUTPUT
if [ -n "$failed_nbs" ]; then
echo "has_failures=true" >> $GITHUB_OUTPUT
echo ""
echo "⚠️ Some notebooks failed to process: $failed_nbs"
else
echo "has_failures=false" >> $GITHUB_OUTPUT
echo ""
echo "✅ All notebooks processed successfully"
fi
- name: Collect failure context
if: steps.detect-failures.outputs.has_failures == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
run_id=${{ github.run_id }}
echo "## Notebook Processing Failure Context" > failure_context.md
echo "" >> failure_context.md
jobs_json=$(gh api repos/${{ github.repository }}/actions/runs/${run_id}/jobs)
for nb in ${{ steps.detect-failures.outputs.failed_notebooks }}; do
nb_name=$(basename "$nb" .ipynb)
echo "### $nb" >> failure_context.md
echo '```' >> failure_context.md
job_id=$(echo "$jobs_json" | jq -r --arg nb "$nb" '.jobs[] | select(.name | contains($nb)) | .id')
if [ -n "$job_id" ] && [ "$job_id" != "null" ]; then
gh api repos/${{ github.repository }}/actions/jobs/${job_id}/logs 2>/dev/null | \
awk '
/##\[group\]Notebook execution output/{capturing=1; next}
capturing && /##\[endgroup\]/{exit}
capturing && /##\[error\]/{print; exit}
capturing && /##\[group\]/{exit}
capturing && /Post job cleanup/{exit}
capturing{print}
' | \
tail -100 >> failure_context.md || echo "Could not retrieve logs" >> failure_context.md
else
echo "Job not found for $nb" >> failure_context.md
fi
echo '```' >> failure_context.md
echo "" >> failure_context.md
done
- name: Post failure context comment
if: steps.detect-failures.outputs.has_failures == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if [ -f failure_context.md ]; then
gh pr comment ${{ github.event.pull_request.number }} --body-file failure_context.md
fi
- name: Restore processed files
run: |
echo "Restoring processed files from artifacts..."
for dir in artifacts/tutorial-*; do
if [ -d "$dir" ]; then
artifact_name=$(basename "$dir" | sed 's/^tutorial-//')
full_path=$(echo "$artifact_name" | sed 's|___|/|g')
tutorial_dir=$(dirname "$full_path")
echo "Restoring from artifact $(basename "$dir") to: $tutorial_dir"
mkdir -p "$tutorial_dir"
cp -rv "$dir"/* "$tutorial_dir/" 2>/dev/null || true
fi
done
echo "Restore complete."
- name: Verify exercises
env:
COMMIT_MESSAGE: ${{ needs.setup.outputs.commit_message }}
run: |
nbs='${{ needs.setup.outputs.notebooks }}'
nb_list=$(echo "$nbs" | jq -r '.[]' | tr '\n' ' ')
nmaci verify-exercises $nb_list --c "$COMMIT_MESSAGE"
- name: Make PR comment
run: |
nbs='${{ needs.setup.outputs.notebooks }}'
nb_list=$(echo "$nbs" | jq -r '.[]' | tr '\n' ' ')
branch=${{ github.event.pull_request.head.ref }}
nmaci make-pr-comment $nb_list --branch $branch --o comment.txt
- name: Update READMEs
if: needs.setup.outputs.materials_changed == 'true'
run: nmaci generate-readmes
- name: Remove unreferenced derivatives
run: |
nmaci find-unreferenced > to_remove.txt
if [ -s to_remove.txt ]; then git rm --pathspec-from-file=to_remove.txt; fi
- name: Clean up artifacts directory
run: rm -rf artifacts/
- name: Revert failed notebooks to main
if: steps.detect-failures.outputs.has_failures == 'true' && steps.detect-failures.outputs.success_notebooks != ''
run: |
failed_nbs="${{ steps.detect-failures.outputs.failed_notebooks }}"
echo "Reverting failed notebooks to main branch version: $failed_nbs"
for nb in $failed_nbs; do
if git show origin/main:"$nb" > /dev/null 2>&1; then
git checkout origin/main -- "$nb"
echo " Reverted: $nb"
else
git rm -f "$nb" 2>/dev/null || rm -f "$nb"
echo " Removed (new file): $nb"
fi
done
- name: Create follow-up PR for failed notebooks
if: steps.detect-failures.outputs.has_failures == 'true' && steps.detect-failures.outputs.success_notebooks != ''
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
failed_nbs="${{ steps.detect-failures.outputs.failed_notebooks }}"
pr_number=${{ github.event.pull_request.number }}
original_branch=${{ github.head_ref }}
new_branch="reprocess-pr${pr_number}-$(date +%Y%m%d%H%M%S)"
git stash --include-untracked
git fetch origin main
git checkout -b "$new_branch" origin/main
for nb in $failed_nbs; do
git checkout "origin/$original_branch" -- "$nb" 2>/dev/null || echo "Note: $nb may be new in this PR"
done
git add $failed_nbs
git commit -m "Reprocess notebooks from PR #${pr_number}
These notebooks failed to process in the original PR and need reprocessing:
$(echo "$failed_nbs" | tr ' ' '\n' | sed 's/^/- /')"
gh auth setup-git
git push origin "$new_branch"
new_pr_url=$(gh pr create \
--title "Reprocess: Failed notebooks from PR #${pr_number}" \
--body "## Auto-generated PR
The following notebooks failed to process in PR #${pr_number} and need reprocessing:
$(echo "$failed_nbs" | tr ' ' '\n' | sed 's/^/- /')
This PR was automatically created to allow these notebooks to be processed independently.
Original PR: #${pr_number}" \
--base "$original_branch" \
--head "$new_branch")
echo "Created follow-up PR: $new_pr_url"
gh pr comment "$pr_number" --body "## Follow-up PR Created
A follow-up PR has been created for the notebooks that failed to process:
**Follow-up PR:** $new_pr_url
**Branch:** \`$new_branch\`
The failed notebooks have been reverted to their \`main\` branch versions in this PR.
Once this PR is merged, the follow-up PR can be used to reprocess the failed notebooks independently."
git checkout "$original_branch"
git stash pop || true
- name: Commit post-processed files
if: steps.detect-failures.outputs.success_notebooks != ''
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add '**/*.ipynb'
git add '**/static/*.png'
git add '**/solutions/*.py'
git add '**/student/*.ipynb'
git add '**/instructor/*.ipynb'
git add '**/README.md'
if [ "${{ steps.detect-failures.outputs.has_failures }}" == "true" ]; then
msg="Process tutorial notebooks (some notebooks moved to follow-up PR)"
else
msg="Process tutorial notebooks"
fi
git diff-index --quiet HEAD || git commit -m "$msg"
- name: Push post-processed files
if: steps.detect-failures.outputs.success_notebooks != ''
uses: ad-m/github-push-action@v1.3.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.head_ref }}