Skip to content

Add files via upload #224

Add files via upload

Add files via upload #224

name: Notebook Automation
on:
push:
paths:
- "**/*.ipynb"
jobs:
process-notebooks:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
ssh-key: ${{ secrets.ACTIONS_BOT_KEY }}
- name: Identify Modified and Deleted Notebooks
run: |
echo "Current SHA: ${{ github.sha }}"
echo "Before SHA: ${{ github.event.before }}"
if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ]; then
echo "First push to this branch. Checking for parents."
parent_sha=$(git log --pretty=%P -n 1 ${{ github.sha }} | cut -d' ' -f1)
echo "Parent SHA: $parent_sha"
if [ -n "$parent_sha" ]; then
echo "Commit has parent: $parent_sha. Diffing against it."
if ! git cat-file -e "$parent_sha^{commit}" 2>/dev/null; then
echo "Parent not in shallow clone. Fetching."
git fetch origin "$parent_sha" --depth=1
fi
git diff --name-status "$parent_sha" "${{ github.sha }}" | grep '\.ipynb$' > notebook_changes.txt
else
echo "No parent (orphan branch). Diffing against empty tree."
git diff --name-status 4b825dc642cb6eb9a060e54bf8d69288fbee4904 "${{ github.sha }}" | grep '\.ipynb$' > notebook_changes.txt
fi
else
if git cat-file -e "${{ github.event.before }}^{commit}" 2>/dev/null; then
echo "Diffing against previous commit: ${{ github.event.before }}"
git diff --name-status "${{ github.event.before }}" "${{ github.sha }}" | grep '\.ipynb$' > notebook_changes.txt
else
echo "Fetching previous commit: ${{ github.event.before }}"
git fetch origin "${{ github.event.before }}" --depth=1
git diff --name-status "${{ github.event.before }}" "${{ github.sha }}" | grep '\.ipynb$' > notebook_changes.txt
fi
fi
if [ ! -s notebook_changes.txt ]; then
echo "No notebook changes detected."
echo "A .dummy.ipynb" > notebook_changes.txt
fi
echo "Contents of notebook_changes.txt:"
cat notebook_changes.txt
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install nbformat jupytext
- name: Add Links to Notebooks
run: python .github/scripts/add_links_to_notebooks.py notebook_changes.txt
env:
REPO_OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
BRANCH_NAME: ${{ github.ref_name }}
- name: Commit Notebook Changes
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "github-actions@github.com"
git add "*.ipynb"
git commit -m "Added Colab and nbviewer links to notebooks" || echo "No changes to commit"
- name: Convert Notebooks to Jupytext
run: |
while IFS=$'\t' read -r status file; do
relative_path="${file#./}"
dir_part="$(dirname "$relative_path")"
if [[ "$dir_part" == "." ]]; then
target_dir="src"
else
target_dir="src/$dir_part"
fi
target_file="$target_dir/$(basename "$file" .ipynb).md"
if [[ "$status" == "D" ]]; then
if [[ -f "$target_file" ]]; then
echo "Deleting source file: $target_file"
rm -f "$target_file"
fi
elif [[ "$status" == "A" || "$status" == "M" ]]; then
echo "Processing modified/added notebook: $file"
mkdir -p "$target_dir"
jupytext --to md --output "$target_file" "$file"
fi
done < notebook_changes.txt
- name: Commit Jupytext Changes
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "github-actions@github.com"
git add "*.md"
git commit -m "Updated source files for modified notebooks" || echo "No changes to commit"
- name: Push All Changes
run: |
git push