|
1 | | -name: Sync to JupyterBook |
2 | | - |
| 1 | +name: Sync JupyterBook |
3 | 2 | on: |
4 | 3 | push: |
5 | | - branches: |
6 | | - - main |
| 4 | + branches: [ main ] |
| 5 | + workflow_dispatch: |
7 | 6 |
|
8 | 7 | jobs: |
9 | | - sync: |
| 8 | + sync-jupyterbook: |
10 | 9 | runs-on: ubuntu-latest |
11 | | - |
| 10 | + permissions: |
| 11 | + contents: write |
| 12 | + defaults: |
| 13 | + run: |
| 14 | + shell: bash -l {0} |
12 | 15 | steps: |
13 | | - - name: Checkout repository |
14 | | - uses: actions/checkout@v4 |
| 16 | + - uses: actions/checkout@v4 |
15 | 17 | 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 |
31 | 19 |
|
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 |
36 | 25 |
|
37 | | - - name: Sync files from main branch |
| 26 | + - name: Install dependencies |
38 | 27 | run: | |
39 | | - git checkout main -- data figures notebooks |
40 | | - git add data figures notebooks |
| 28 | + conda activate pylipd |
| 29 | + pip install pyyaml |
41 | 30 |
|
42 | | - - name: Verify copied files |
| 31 | + - name: Configure Git |
43 | 32 | 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' |
48 | 35 |
|
49 | | - - name: Generate _toc.yml |
| 36 | + - name: Copy Files and Update TOC |
| 37 | + env: |
| 38 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
50 | 39 | 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 |
63 | 48 |
|
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 | + } |
67 | 63 |
|
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 |
73 | 65 |
|
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')) |
79 | 67 |
|
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}) |
85 | 73 |
|
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 |
88 | 80 |
|
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