|
| 1 | +# GitHub Actions workflow for executing Jupyter notebooks |
| 2 | +# Runs on pull requests and nightly to ensure notebooks work correctly |
| 3 | + |
| 4 | +name: Notebooks |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [main, master] |
| 9 | + paths: |
| 10 | + - 'notebooks/**' |
| 11 | + - 'boxcrete/**' |
| 12 | + - 'pyproject.toml' |
| 13 | + - '.github/workflows/notebooks.yml' |
| 14 | + pull_request: |
| 15 | + branches: [main, master] |
| 16 | + paths: |
| 17 | + - 'notebooks/**' |
| 18 | + - 'boxcrete/**' |
| 19 | + - 'pyproject.toml' |
| 20 | + - '.github/workflows/notebooks.yml' |
| 21 | + schedule: |
| 22 | + # Run nightly at 3:00 AM UTC |
| 23 | + - cron: '0 3 * * *' |
| 24 | + workflow_dispatch: |
| 25 | + |
| 26 | +jobs: |
| 27 | + execute-notebooks: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + strategy: |
| 30 | + fail-fast: false |
| 31 | + matrix: |
| 32 | + python-version: ['3.10'] |
| 33 | + |
| 34 | + steps: |
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@v4 |
| 37 | + |
| 38 | + - name: Set up Python ${{ matrix.python-version }} |
| 39 | + uses: actions/setup-python@v5 |
| 40 | + with: |
| 41 | + python-version: ${{ matrix.python-version }} |
| 42 | + |
| 43 | + - name: Cache pip dependencies |
| 44 | + uses: actions/cache@v4 |
| 45 | + with: |
| 46 | + path: ~/.cache/pip |
| 47 | + key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }} |
| 48 | + restore-keys: | |
| 49 | + ${{ runner.os }}-pip-${{ matrix.python-version }}- |
| 50 | + ${{ runner.os }}-pip- |
| 51 | +
|
| 52 | + - name: Install dependencies |
| 53 | + run: | |
| 54 | + python -m pip install --upgrade pip |
| 55 | + pip install -e ".[notebooks]" |
| 56 | +
|
| 57 | + - name: Install Jupyter kernel |
| 58 | + run: | |
| 59 | + python -m ipykernel install --user --name python3 |
| 60 | +
|
| 61 | + - name: Execute notebooks |
| 62 | + run: | |
| 63 | + python - << 'PYEOF' |
| 64 | + import subprocess, sys |
| 65 | + from pathlib import Path |
| 66 | +
|
| 67 | + notebooks = sorted(Path("notebooks").glob("*.ipynb")) |
| 68 | + failed = [] |
| 69 | +
|
| 70 | + for nb in notebooks: |
| 71 | + print(f"{'=' * 40}") |
| 72 | + print(f"Executing: {nb}") |
| 73 | + print(f"{'=' * 40}") |
| 74 | +
|
| 75 | + result = subprocess.run( |
| 76 | + [ |
| 77 | + sys.executable, "-m", "jupyter", "nbconvert", |
| 78 | + "--to", "notebook", |
| 79 | + "--execute", |
| 80 | + "--inplace", |
| 81 | + "--ExecutePreprocessor.timeout=600", |
| 82 | + "--ExecutePreprocessor.kernel_name=python3", |
| 83 | + str(nb), |
| 84 | + ], |
| 85 | + capture_output=False, |
| 86 | + ) |
| 87 | +
|
| 88 | + if result.returncode != 0: |
| 89 | + failed.append(str(nb)) |
| 90 | + print(f"❌ Failed: {nb}") |
| 91 | + else: |
| 92 | + print(f"✅ Successfully executed: {nb}") |
| 93 | + print() |
| 94 | +
|
| 95 | + if failed: |
| 96 | + print(f"\n{len(failed)} notebook(s) failed:") |
| 97 | + for f in failed: |
| 98 | + print(f" - {f}") |
| 99 | + sys.exit(1) |
| 100 | + else: |
| 101 | + print(f"\nAll {len(notebooks)} notebook(s) executed successfully.") |
| 102 | + PYEOF |
| 103 | +
|
| 104 | + - name: Upload executed notebooks as artifacts |
| 105 | + uses: actions/upload-artifact@v4 |
| 106 | + if: always() |
| 107 | + with: |
| 108 | + name: executed-notebooks |
| 109 | + path: notebooks/*.ipynb |
| 110 | + retention-days: 7 |
| 111 | + |
| 112 | + notebook-lint: |
| 113 | + runs-on: ubuntu-latest |
| 114 | + steps: |
| 115 | + - name: Checkout repository |
| 116 | + uses: actions/checkout@v4 |
| 117 | + |
| 118 | + - name: Set up Python |
| 119 | + uses: actions/setup-python@v5 |
| 120 | + with: |
| 121 | + python-version: '3.10' |
| 122 | + |
| 123 | + - name: Install dependencies |
| 124 | + run: | |
| 125 | + python -m pip install --upgrade pip |
| 126 | + pip install nbformat |
| 127 | +
|
| 128 | + - name: Validate notebook format |
| 129 | + run: | |
| 130 | + python - << 'EOF' |
| 131 | + import nbformat |
| 132 | + import sys |
| 133 | + from pathlib import Path |
| 134 | +
|
| 135 | + notebooks = list(Path('notebooks').glob('*.ipynb')) |
| 136 | + errors = [] |
| 137 | +
|
| 138 | + for nb_path in notebooks: |
| 139 | + try: |
| 140 | + with open(nb_path, 'r', encoding='utf-8') as f: |
| 141 | + nb = nbformat.read(f, as_version=4) |
| 142 | + print(f"✅ Valid notebook: {nb_path}") |
| 143 | + except Exception as e: |
| 144 | + errors.append(f"❌ Invalid notebook {nb_path}: {e}") |
| 145 | + print(errors[-1]) |
| 146 | +
|
| 147 | + if errors: |
| 148 | + print(f"\n{len(errors)} notebook(s) failed validation.") |
| 149 | + sys.exit(1) |
| 150 | + else: |
| 151 | + print(f"\nAll {len(notebooks)} notebook(s) are valid.") |
| 152 | + EOF |
0 commit comments