-
Notifications
You must be signed in to change notification settings - Fork 186
68 lines (60 loc) · 2.29 KB
/
add-notebook-examples-to-docs.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
name: Add Notebook Examples to Docs
on:
push:
branches:
- main
paths:
- 'examples/**'
- 'docs/v1/examples/**'
- '.github/workflows/add-notebook-examples-to-docs.yml'
workflow_dispatch:
permissions:
contents: write
jobs:
add-notebook-examples-to-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install jupyter nbconvert
- name: Convert notebooks to markdown and add to docs
run: |
set -x # Enable debug mode
for file in docs/v1/examples/*.mdx; do
echo "Processing file: $file"
source_file=$(grep -oP '(?<=\{/\* SOURCE_FILE: ).*(?= \*/\})' "$file" || true)
if [[ -z "$source_file" ]]; then
continue
fi
echo "Source file: $source_file"
if [[ -f "$source_file" ]]; then
echo "Converting notebook to markdown"
jupyter nbconvert --to markdown "$source_file" || { echo "Error: Failed to convert $source_file" >&2; continue; }
markdown_file="${source_file%.ipynb}.md"
echo "Removing existing content after {/* SOURCE_FILE: ... */}"
sed -i '\#{/\* SOURCE_FILE:#,$d' "$file"
echo "Appending markdown to $file"
echo -e "\n{/* SOURCE_FILE: $source_file */}\n" >> "$file"
cat "$markdown_file" >> "$file" || { echo "Error: Failed to append markdown to $file" >&2; continue; }
rm "$markdown_file" || { echo "Error: Failed to remove $markdown_file" >&2; continue; }
else
echo "Error: Source file not found: $source_file" >&2
fi
done
- name: Add, commit, and push changes
run: |
git config --global user.name 'Howard Gil'
git config --global user.email 'HowieG@users.noreply.github.com'
git add docs/v1/examples/*.mdx
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "GitHub Action: Update examples in docs from notebooks"
git push
fi