Skip to content

Commit 36d3396

Browse files
authored
Update sync.yml
1 parent 099405b commit 36d3396

1 file changed

Lines changed: 66 additions & 73 deletions

File tree

.github/workflows/sync.yml

Lines changed: 66 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,86 @@
1-
name: Sync to JupyterBook
2-
1+
name: Sync JupyterBook
32
on:
43
push:
5-
branches:
6-
- main
4+
branches: [ main ]
5+
workflow_dispatch:
76

87
jobs:
9-
sync:
8+
sync-jupyterbook:
109
runs-on: ubuntu-latest
11-
10+
permissions:
11+
contents: write
12+
defaults:
13+
run:
14+
shell: bash -l {0}
1215
steps:
13-
- name: Checkout repository
14-
uses: actions/checkout@v4
16+
- uses: actions/checkout@v4
1517
with:
16-
fetch-depth: 0 # Fetch full history to allow branch switching
17-
18-
- name: Fetch all branches
19-
run: |
20-
git fetch --all
21-
22-
- name: Switch to jupyterbook branch
23-
run: |
24-
if git ls-remote --exit-code --heads origin jupyterbook; then
25-
git checkout jupyterbook
26-
else
27-
echo "Branch 'jupyterbook' does not exist. Creating it..."
28-
git checkout -b jupyterbook
29-
git push -u origin jupyterbook
30-
fi
18+
fetch-depth: 0
3119

32-
- name: Ensure clean branch state
33-
run: |
34-
git reset --hard origin/jupyterbook
35-
git clean -fd
20+
- uses: conda-incubator/setup-miniconda@v2
21+
with:
22+
activate-environment: pylipd
23+
environment-file: environment.yml
24+
auto-activate-base: false
3625

37-
- name: Sync files from main branch
26+
- name: Install dependencies
3827
run: |
39-
git checkout main -- data figures notebooks
40-
git add data figures notebooks
28+
conda activate pylipd
29+
pip install pyyaml
4130
42-
- name: Verify copied files
31+
- name: Configure Git
4332
run: |
44-
echo "Verifying copied directories..."
45-
ls -la data
46-
ls -la figures
47-
ls -la notebooks
33+
git config --global user.name 'github-actions[bot]'
34+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
4835
49-
- name: Generate _toc.yml
36+
- name: Copy Files and Update TOC
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5039
run: |
51-
echo "Generating _toc.yml..."
52-
cat <<EOF > _toc.yml
53-
format: jb-book
54-
root: intro
55-
parts:
56-
- caption: Dataset Representation in PyLiPD
57-
chapters:
58-
- file: graph.md
59-
- file: standards.md
60-
- caption: Getting Started
61-
chapters:
62-
EOF
40+
git checkout jupyterbook
41+
rm -rf notebooks/ data/ figures/
42+
git checkout main -- notebooks/ data/ figures/
43+
44+
cat << 'EOF' > update_toc.py
45+
import yaml
46+
import glob
47+
import os
6348
64-
for file in notebooks/L0_*.ipynb; do
65-
[ -e "$file" ] && echo " - file: $file" >> _toc.yml
66-
done
49+
toc = {
50+
'format': 'jb-book',
51+
'root': 'intro',
52+
'parts': [
53+
{'caption': 'Dataset Representation in PyLiPD', 'chapters': [
54+
{'file': 'graph.md'},
55+
{'file': 'standards.md'}
56+
]},
57+
{'caption': 'Getting Started', 'chapters': []},
58+
{'caption': 'Basic Functionalities', 'chapters': []},
59+
{'caption': 'Advanced Querying using SPARQL', 'chapters': []},
60+
{'caption': 'Editing LiPD files', 'chapters': []}
61+
]
62+
}
6763
68-
echo " - caption: Basic Functionalities" >> _toc.yml
69-
echo " chapters:" >> _toc.yml
70-
for file in notebooks/L1_*.ipynb; do
71-
[ -e "$file" ] && echo " - file: $file" >> _toc.yml
72-
done
64+
level_map = {'L0': 1, 'L1': 2, 'L2': 3, 'L3': 4} # Mapping to correct sections
7365
74-
echo " - caption: Advanced Querying using SPARQL" >> _toc.yml
75-
echo " chapters:" >> _toc.yml
76-
for file in notebooks/L2_*.ipynb; do
77-
[ -e "$file" ] && echo " - file: $file" >> _toc.yml
78-
done
66+
notebooks = sorted(glob.glob('notebooks/L*.ipynb'))
7967
80-
echo " - caption: Editing LiPD files" >> _toc.yml
81-
echo " chapters:" >> _toc.yml
82-
for file in notebooks/L3_*.ipynb; do
83-
[ -e "$file" ] && echo " - file: $file" >> _toc.yml
84-
done
68+
for nb in notebooks:
69+
level = os.path.basename(nb)[:2] # Extract L0, L1, etc.
70+
if level in level_map:
71+
part_index = level_map[level]
72+
toc['parts'][part_index]['chapters'].append({'file': nb})
8573
86-
- name: Show _toc.yml contents
87-
run: cat _toc.yml
74+
with open('_toc.yml', 'w') as f:
75+
yaml.dump(toc, f, sort_keys=False)
76+
EOF
77+
78+
conda activate pylipd
79+
python update_toc.py
8880
89-
- name: Commit and push changes
90-
run: |
91-
git add _toc.yml
92-
git commit -m "Sync files and update _toc.yml" || echo "No changes to commit"
93-
git push origin jupyterbook || git push origin jupyterbook --force
81+
git add notebooks/ data/ figures/ _toc.yml
82+
if ! git diff --staged --quiet; then
83+
git commit -m "Auto-sync notebooks, data, figures, and update TOC"
84+
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}"
85+
git push origin jupyterbook
86+
fi

0 commit comments

Comments
 (0)