|
| 1 | +# Notebook Testing Workflow |
| 2 | +# |
| 3 | +# This workflow tests Jupyter notebooks and runs by default on: |
| 4 | +# - Pull requests targeting the 'main' branch |
| 5 | +# |
| 6 | +# To manually trigger this workflow on any branch: |
| 7 | +# 1. Go to Actions tab in GitHub |
| 8 | +# 2. Select "Test Notebooks" workflow |
| 9 | +# 3. Click "Run workflow" dropdown |
| 10 | +# 4. Select your branch and click "Run workflow" |
| 11 | + |
| 12 | +name: Test Notebooks |
| 13 | + |
| 14 | +on: |
| 15 | + pull_request: |
| 16 | + branches: |
| 17 | + - main |
| 18 | + # Allow manual triggering from the Actions tab |
| 19 | + workflow_dispatch: |
| 20 | + |
| 21 | +jobs: |
| 22 | + test-notebooks: |
| 23 | + name: Notebooks (${{ matrix.python-version }}, ${{ matrix.os }}) |
| 24 | + runs-on: ${{ matrix.os }} |
| 25 | + defaults: |
| 26 | + run: |
| 27 | + shell: bash |
| 28 | + strategy: |
| 29 | + fail-fast: true |
| 30 | + matrix: |
| 31 | + os: ["ubuntu-latest"] |
| 32 | + python-version: ["3.10", "3.11", "3.12"] |
| 33 | + |
| 34 | + steps: |
| 35 | + - uses: actions/checkout@v4 |
| 36 | + |
| 37 | + - name: Install uv |
| 38 | + uses: astral-sh/setup-uv@v3 |
| 39 | + with: |
| 40 | + version: "latest" |
| 41 | + |
| 42 | + - name: Set up Python ${{ matrix.python-version }} |
| 43 | + run: uv python install ${{ matrix.python-version }} |
| 44 | + |
| 45 | + - name: Cache MTH5 test files |
| 46 | + uses: actions/cache@v4 |
| 47 | + with: |
| 48 | + path: ~/.cache/aurora |
| 49 | + key: mth5-test-files-${{ runner.os }}-${{ hashFiles('tests/conftest.py') }} |
| 50 | + restore-keys: | |
| 51 | + mth5-test-files-${{ runner.os }}- |
| 52 | +
|
| 53 | + - name: Create virtual environment and install dependencies |
| 54 | + run: | |
| 55 | + uv venv --python ${{ matrix.python-version }} |
| 56 | + source .venv/bin/activate |
| 57 | + uv pip install --upgrade pip |
| 58 | + uv pip install 'setuptools<68' |
| 59 | + uv pip install -e ".[dev,test]" |
| 60 | + uv pip install mt_metadata[obspy] |
| 61 | + uv pip install "mt_metadata[obspy]" |
| 62 | + uv pip install mth5 |
| 63 | + uv pip install git+https://github.com/kujaku11/mth5_test_data.git |
| 64 | + # Explicitly include nbconvert & ipykernel |
| 65 | + uv pip install jupyter nbconvert nbformat ipykernel |
| 66 | + python -m ipykernel install --user --name "python3" |
| 67 | +
|
| 68 | + - name: Install system dependencies |
| 69 | + run: | |
| 70 | + sudo apt-get update |
| 71 | + sudo apt-get install -y pandoc |
| 72 | +
|
| 73 | + - name: Execute Jupyter Notebooks |
| 74 | + run: | |
| 75 | + source .venv/bin/activate |
| 76 | + # debugging: print locations of key packages |
| 77 | + python -c "import pkg_resources; print('pkg_resources location:', pkg_resources.__file__)" |
| 78 | + python -c "import setuptools; print('setuptools location:', setuptools.__file__)" |
| 79 | + |
| 80 | + python << 'EOF' |
| 81 | + import nbformat |
| 82 | + import subprocess |
| 83 | + import sys |
| 84 | + |
| 85 | + notebooks = [ |
| 86 | + "docs/examples/dataset_definition.ipynb", |
| 87 | + "docs/examples/operate_aurora.ipynb", |
| 88 | + "docs/tutorials/pkd_units_check.ipynb", |
| 89 | + "docs/tutorials/pole_zero_fitting/lemi_pole_zero_fitting_example.ipynb", |
| 90 | + "docs/tutorials/processing_configuration.ipynb", |
| 91 | + "docs/tutorials/process_cas04_multiple_station.ipynb", |
| 92 | + "docs/tutorials/synthetic_data_processing.ipynb" |
| 93 | + ] |
| 94 | + |
| 95 | + failures = [] |
| 96 | + |
| 97 | + for nb_path in notebooks: |
| 98 | + # Update kernel spec |
| 99 | + print(f"Updating kernel in {nb_path}") |
| 100 | + try: |
| 101 | + with open(nb_path, "r", encoding="utf-8") as f: |
| 102 | + nb = nbformat.read(f, as_version=4) |
| 103 | + |
| 104 | + nb["metadata"]["kernelspec"]["name"] = "python3" |
| 105 | + nb["metadata"]["kernelspec"]["display_name"] = "Python (python3)" |
| 106 | + |
| 107 | + with open(nb_path, "w", encoding="utf-8") as f: |
| 108 | + nbformat.write(nb, f) |
| 109 | + print(f"✓ Updated kernel in {nb_path}") |
| 110 | + except Exception as e: |
| 111 | + print(f"✗ Failed to update kernel in {nb_path}: {e}") |
| 112 | + failures.append(nb_path) |
| 113 | + continue |
| 114 | + |
| 115 | + # Execute notebook |
| 116 | + print(f"Executing {nb_path}") |
| 117 | + result = subprocess.run( |
| 118 | + ["jupyter", "nbconvert", "--to", "notebook", "--execute", nb_path], |
| 119 | + capture_output=True, |
| 120 | + text=True |
| 121 | + ) |
| 122 | + |
| 123 | + if result.returncode != 0: |
| 124 | + print(f"✗ Failed to execute {nb_path}") |
| 125 | + print(result.stderr) |
| 126 | + failures.append(nb_path) |
| 127 | + else: |
| 128 | + print(f"✓ Successfully executed {nb_path}") |
| 129 | + |
| 130 | + if failures: |
| 131 | + print("\n======= Summary =======") |
| 132 | + print(f"Failed notebooks: {failures}") |
| 133 | + sys.exit(1) |
| 134 | + else: |
| 135 | + print("\n✓ All notebooks executed successfully!") |
| 136 | + EOF |
0 commit comments