Update requirements.txt #189
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build HTML on merge | |
on: | |
pull_request_target: | |
types: [closed] | |
paths: | |
- '**/.ipynb' | |
- '**/requirements.txt' | |
env: | |
CASJOBS_PW: ${{ secrets.CASJOBS_PW }} | |
CASJOBS_USERID: ${{ secrets.CASJOBS_USERID }} | |
jobs: | |
prepare_matrix: | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
ref: ${{ github.event.pull_request.head.sha }} | |
- id: set-matrix | |
shell: bash | |
run: | | |
# Convert the comma-separated list to an array. | |
IFS=',' read -r -a files <<< "${{ steps.get-changed-files.outputs.all_changed_files }}" | |
echo "Changed files: ${files[*]}" | |
# Array to hold the notebooks that will be executed. | |
notebooks=() | |
notebook_changed=false | |
# First, if any changed file is a notebook, add it. | |
for file in "${files[@]}"; do | |
if [[ "$file" == *.ipynb ]]; then | |
notebook_changed=true | |
notebooks+=("$file") | |
fi | |
done | |
# If no notebook file changed, look for requirements.txt changes. | |
if [ "$notebook_changed" = false ]; then | |
for file in "${files[@]}"; do | |
if [[ "$file" == *requirements.txt ]]; then | |
dir=$(dirname "$file") | |
echo "Found requirements file in folder '$dir'; gathering notebooks from that folder." | |
# Find all notebooks in the folder (only that folder, not recursing). | |
while IFS= read -r nb; do | |
notebooks+=("$nb") | |
done < <(find "$dir" -maxdepth 1 -type f -name '*.ipynb') | |
fi | |
done | |
fi | |
# Remove duplicate entries. | |
unique_notebooks=($(printf "%s\n" "${notebooks[@]}" | sort -u)) | |
if [ ${#unique_notebooks[@]} -eq 0 ]; then | |
echo "No relevant changes found; skipping notebook execution." | |
echo "has_notebooks=false" >> "$GITHUB_OUTPUT" | |
echo "matrix=[]" >> "$GITHUB_OUTPUT" | |
else | |
echo "Notebooks selected for testing: ${unique_notebooks[*]}" | |
echo "has_notebooks=true" >> "$GITHUB_OUTPUT" | |
# Build a JSON array for the matrix. | |
matrix_json=$(printf '%s\n' "${unique_notebooks[@]}" | jq -R . | jq -s .) | |
# Write the JSON to GITHUB_OUTPUT using the heredoc syntax. | |
printf "matrix<<EOF\n%s\nEOF\n" "$matrix_json" >> "$GITHUB_OUTPUT" | |
fi | |
execute-notebooks: | |
if: github.event.pull_request.merged == true | |
needs: prepare_matrix | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
notebook: ${{fromJson(needs.prepare_matrix.outputs.matrix)}} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Setup Python | |
uses: actions/setup-python@v3 | |
with: | |
python-version: ${{ vars.PYTHON_VERSION }} | |
- name: Install Dependencies (if requirements.txt exists) and Execute Notebook | |
run: | | |
notebook="${{ matrix.notebook }}" | |
dir=$(dirname "$notebook") | |
if [ -f "$dir/requirements.txt" ]; then | |
pip install -r "$dir/requirements.txt" | |
fi | |
pip install notebook | |
jupyter nbconvert --to notebook --execute --inplace "$notebook" | |
- name: Commit modified file on current branch | |
run: | | |
git config user.name 'CI Bot' | |
git config user.email '[email protected]' | |
git add "${{ matrix.notebook }}" | |
git commit -m "Storing executed notebook ${{ matrix.notebook }}" | |
- name: Checkout only the file to the target branch | |
run: | | |
git fetch | |
git pull | |
git checkout -f gh-storage | |
git checkout @{-1} "${{ matrix.notebook }}" | |
- name: Commit and push modifications to target branch | |
run: | | |
git commit -m "Storing executed notebook ${{ matrix.notebook }}" | |
MAX_RETRIES=5 | |
RETRY_DELAY=10s | |
for i in $(seq 1 $MAX_RETRIES); do | |
git push origin gh-storage --force && break || { | |
echo "Push $i failed... waiting $RETRY_DELAY" | |
sleep $RETRY_DELAY | |
} | |
done | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
generate_html: | |
if: github.event.pull_request.merged == true | |
needs: execute-notebooks | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Python ${{ vars.PYTHON_VERSION }} | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ vars.PYTHON_VERSION }} | |
- name: Install dependencies | |
run: | | |
pip install ghp-import | |
pip install jupyter-book==v0.15.1 | |
pip install myst-nb | |
pip install astroid | |
pip install nbval | |
#pip install bs4 | |
#pip install lxml | |
## test to bypass the jupyter-book lower version | |
pip install jsonschema==4.6.0 | |
PATH="${PATH}:${HOME}/.local/bin" | |
- name: Build HTML | |
run: | | |
git fetch | |
git checkout origin/gh-storage -- notebooks/ | |
jupyter-book build . | |
# Push the book's HTML to github-pages | |
- name: GitHub Pages action | |
uses: peaceiris/actions-gh-pages@v3 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
publish_dir: ./_build/html |