-
Notifications
You must be signed in to change notification settings - Fork 3
147 lines (134 loc) · 4.99 KB
/
ci_runner.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
on:
workflow_call:
inputs:
python-version:
required: true
type: string
secrets:
CASJOBS_USERID:
description: 'CASJOBS user ID'
required: false
CASJOBS_PW:
description: 'CASJOBS password'
required: false
#permissions:
# id-token: write
jobs:
gather-notebooks:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v3
## routine to gather only the changed notebook files and supply them to the matrix
- name: changed-files
id: get-changed-files
uses: tj-actions/changed-files@v36
with:
separator: ","
files: |
**/*.ipynb
## convert the list of files to an array and push them into the matrix as a json object
- name: set-matrix
id: set-matrix
run: |
IFS=',' read -r -a array <<< "${{steps.get-changed-files.outputs.all_changed_files}}"
echo "{$array}"
echo "matrix=$(jq --compact-output --null-input '$ARGS.positional' --args -- "${array[@]}")" >> $GITHUB_OUTPUT
notebook-execution:
needs: gather-notebooks
environment: ci_env
runs-on: ubuntu-latest
permissions:
contents: write
strategy:
fail-fast: false
matrix:
notebooks: ${{ fromJson(needs.gather-notebooks.outputs.matrix) }}
steps:
- uses: actions/checkout@v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v4 ## needed for caching
with:
python-version: ${{ inputs.python-version }}
cache: 'pip'
- name: Add conda to system path
run: |
# $CONDA is an environment variable pointing to the root of the miniconda directory
echo $CONDA/bin >> $GITHUB_PATH
- name: Install dependencies
run: |
## Install the local requirements file
echo DEBUG ---
pwd ## print working directory
echo "Path to req's: $(dirname ${{ matrix.notebooks }})/requirements.txt"
ls $(dirname ${{ matrix.notebooks }})
echo ---
if [ -f $(dirname "${{ matrix.notebooks }}")/pre-requirements.sh ]; then
chmod +x $(dirname "${{ matrix.notebooks }}")/pre-requirements.sh
./$(dirname "${{ matrix.notebooks }}")/pre-requirements.sh
fi
if [ -f pre-requirements.txt ]; then
pip install -r pre-requirements.txt
fi
if [ -f $(dirname "${{ matrix.notebooks }}")/pre-installl.sh ]; then
chmod +x $(dirname "${{ matrix.notebooks }}")/pre-install.sh
./$(dirname "${{ matrix.notebooks }}")/pre-install.sh
fi
#pip install -r $(dirname "${{ matrix.notebooks }}")/requirements.txt
pip install -r $(dirname ${{ matrix.notebooks }})/requirements.txt
pip install pytest
pip install nbval
pip install nbconvert
pip install bandit
- name: Security testing with Bandit
run: |
bandit "${{ matrix.notebooks }}"
- name: Execute notebooks
id: execute
run: |
#export CASJOBS_PW="$CI_CASJOBS_PW"
#export CASJOBS_USERID="$CI_CASJOBS_USERID"
jupyter nbconvert --to notebook --execute --inplace ${{ matrix.notebooks }}
- name: Validate notebooks
run: |
jupyter nbconvert --clear-output --inplace "${{ matrix.notebooks }}"
pytest --nbval "${{ matrix.notebooks }}"
- name: Archive executed notebooks
run: |
git config pull.rebase false
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
# Check if branch exists and checkout or create
git show-ref --verify --quiet refs/heads/gh-storage
if [ $? -ne 0 ]; then
git checkout -b gh-storage
else
git checkout gh-storage
fi
git add ${{ matrix.notebooks }}
if [[ $(git status --porcelain) ]]; then
git commit -m 'Added executed notebook ${{ matrix.notebooks }}'
else
echo "No changes to commit"
exit 0
fi
ATTEMPTS_LEFT=3
SLEEP_TIME=5
until [ $ATTEMPTS_LEFT -lt 1 ]
do
# Fetch and reset instead of pulling
git fetch origin gh-storage
git reset --hard origin/gh-storage
git push origin gh-storage 2>&1 | tee output.log
if grep -q "Updates were rejected because the remote contains work" output.log; then
echo "Push failed due to conflicting remote changes. Remaining attempts: $ATTEMPTS_LEFT"
let "ATTEMPTS_LEFT--"
sleep $SLEEP_TIME
SLEEP_TIME=$(($SLEEP_TIME * 2))
else
break
fi
done