-
Notifications
You must be signed in to change notification settings - Fork 1
170 lines (148 loc) · 5.5 KB
/
ci_html_build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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