1- name : Test Python Package
2- # Stress Test against different platforms and python versions
1+ name : Continuous Integration / Continuous Delivery
2+ # Stress Test and Build Docker Image
3+
4+ # dev has feature/bug-branch and writes code
5+ # dev wants on each push, 'small' Matrix Tests to trigger
6+ # all feature/bug branches "have" to merge to 'dev' branch
7+ # 'dev' branch automatically triggers 'full' Matrix Tests
8+
9+ # it is time to make a new release
10+ # 'dev' branch merges into 'release' branch
11+ # a couple of commits (ie version bump, changelog update) are pushed to 'release'
12+ # a PR opens to merge 'release' into master/main
13+ # the PR triggers ALL TESTS:
14+ # - Full Matrix Stress Tests
15+ # - Doc tests
16+ # - build/release
17+
18+ # if all OK then PR merges
19+ # then a new tag (release) is pushed to github
20+
21+
22+ # full Matrix Stress Test on
23+ # - push to master/main
24+ # - push to stress-test branch
25+ # we want small Matrix Test on each pushed commit
326
427on :
528 push :
629 branches :
7- - master
8- - dev
9- - ci
10- - windows-ci
11- pull_request :
12- branches :
13- - master
14- - dev
15- - develop
30+ - " *"
1631 tags :
1732 - v*
1833
34+ env :
35+ # ability to define the Job Matrix as an env var !
36+ FULL_MATRIX_STRATEGY : " {\" platform\" : [\" ubuntu-latest\" , \" macos-latest\" , \" windows-latest\" ], \" python-version\" : [\" 3.9\" , \" 3.10\" ]}"
37+ UBUNTU_PY310_STRATEGY : " {\" platform\" :[\" ubuntu-latest\" ], \" python-version\" :[\" 3.10\" ]}"
38+ RUN_UNIT_TESTS : " true"
39+ BUILD_DOCKER : " true"
1940
2041jobs :
42+ # we use the below to read the workflow env vars and be able to use in "- if:" Job conditionals
43+ # now we can do -> if: ${{ needs.set_github_outputs.outputs.TESTS_ENABLED == 'true' }}
44+ # github does not have a way to simply do "- if: ${{ env.RUN_UNIT_TESTS == 'true' }} " !!
45+ set_github_outputs :
46+ name : Read Workflow Env Section Vars and set Github Outputs
47+ runs-on : ubuntu-latest
48+ outputs :
49+ matrix : ${{ steps.pass-env-to-output.outputs.matrix }}
50+ TESTS_ENABLED : ${{ steps.pass-env-to-output.outputs.TESTS_ENABLED }}
51+ DOCKER_ENABLED : ${{ steps.pass-env-to-output.outputs.DOCKER_ENABLED }}
52+ steps :
53+ - name : Pass 'env' section variables to GITHUB_OUTPUT
54+ id : pass-env-to-output
55+ run : |
56+ # set the matrix strategy to Full Matrix Stress Test if on master/main or stress-test branch or any tag
57+ BRANCH_NAME=${GITHUB_REF##*/}
58+ if [[ $BRANCH_NAME == "master" || $BRANCH_NAME == "main" || $BRANCH_NAME == "stress-test" || $GITHUB_REF == refs/tags/* ]]; then
59+ echo "matrix=$FULL_MATRIX_STRATEGY" >> $GITHUB_OUTPUT
60+ else
61+ echo "matrix=$UBUNTU_PY310_STRATEGY" >> $GITHUB_OUTPUT
62+ fi
63+ echo "TESTS_ENABLED=$RUN_UNIT_TESTS" >> $GITHUB_OUTPUT
64+ echo "DOCKER_ENABLED=$BUILD_DOCKER" >> $GITHUB_OUTPUT
65+
2166 test_suite :
2267 runs-on : ${{ matrix.platform }}
68+ needs : set_github_outputs
69+ if : ${{ needs.set_github_outputs.outputs.TESTS_ENABLED == 'true' }}
2370 strategy :
24- matrix :
25- # platform: [ubuntu-latest, macos-latest, windows-latest]
26- platform : [ubuntu-latest]
27- # python-version: ["3.7", "3.8", "3.9", "3.10"]
28- python-version : ["3.10"]
71+ # This needs to match the first job's name and output parameter
72+ matrix : ${{fromJSON(needs.set_github_outputs.outputs.matrix)}}
2973
3074 steps :
3175 - uses : actions/checkout@v3
3276 - name : Set up Python ${{ matrix.python-version }}
33- uses : actions/setup-python@v2
77+ uses : actions/setup-python@v4
3478 with :
3579 python-version : ${{ matrix.python-version }}
3680
81125 run : tox -e coverage --sitepackages -vv -s false
82126
83127 - run : pip install coverage[toml]>=5.1
128+ if : ${{ always() }}
84129
85130 - name : Send Coverage Data to Codecov
86131 uses : codecov/codecov-action@v2
92137 flags : unittests
93138 name : codecov-umbrella
94139 verbose : true
140+ if : ${{ always() }}
95141
96142 - name : Check for compliance with Python Best Practices
97143 shell : bash
@@ -114,7 +160,6 @@ jobs:
114160 - name : Upload Source & Wheel distributions as Artefacts
115161 uses : actions/upload-artifact@v2
116162 with :
117- # name: dist-${{ env.OS }}-${{ env.PYTHON }} # TODO enable this
118163 name : dist-${{ matrix.platform }}-${{ matrix.python-version }}
119164 path : ${{ env.DIST_DIR }}
120165 if-no-files-found : error
@@ -131,9 +176,41 @@ jobs:
131176 # with:
132177 # name: coverage-xml-data
133178
179+ # DRAW PYTHON DEPENDENCY GRAPHS
180+ check_if_source_code_changed :
181+ runs-on : ubuntu-latest
182+ name : Check for Source Code changes; happened inside the 'src' dir
183+ outputs :
184+ src_dir_changed : ${{ steps.check_files.outputs.src_dir_changed }}
185+ steps :
186+ - name : Checkout code
187+ uses : actions/checkout@v3
188+ with :
189+ fetch-depth : 2
190+ - name : check modified files
191+ id : check_files
192+ run : |
193+ echo "=============== list modified files ==============="
194+ git diff --name-only HEAD^ HEAD
195+
196+ echo "========== check paths of modified files =========="
197+ git diff --name-only HEAD^ HEAD > files.txt
198+
199+ echo "src_dir_changed=false" >> $GITHUB_OUTPUT
200+ while read file; do
201+ echo $file
202+ if [[ $file =~ ^src/ ]]; then
203+ echo "This modified file is under the 'src' folder."
204+ echo "src_dir_changed=true" >> $GITHUB_OUTPUT
205+ break
206+ fi
207+ done < files.txt
208+
134209 draw-dependencies :
135210 runs-on : ubuntu-latest
136- needs : test_suite
211+ needs : check_if_source_code_changed
212+ if : needs.check_if_source_code_changed.outputs.src_dir_changed == 'true'
213+ name : Draw Python Dependencies as Graphs, in .svg
137214 steps :
138215 - uses : actions/checkout@v3
139216 - name : Set up Python 3.10
@@ -160,3 +237,31 @@ jobs:
160237 name : dependency-graphs
161238 path : pydeps/
162239 if-no-files-found : warn # 'error' or 'ignore' are also available, defaults to `warn`
240+
241+ docker_build :
242+ runs-on : ubuntu-latest
243+ needs : test_suite
244+ if : needs.set_github_outputs.outputs.DOCKER_ENABLED == 'true'
245+ env :
246+ DOCKER_USER : ${{ secrets.DOCKER_USER }}
247+ steps :
248+ - uses : actions/checkout@v3
249+ - name : Build 'n Push Docker Image to DockerHub
250+ run : |
251+ # workflow enabled for branches and v* tags
252+ IMAGE_TAG="${GITHUB_REF_NAME}" # this is branch name or tag name
253+ echo "IMAGE_REF=generate-python:${IMAGE_TAG}" >> $GITHUB_ENV
254+
255+ docker build -t "${DOCKER_USER}/generate-python:${GITHUB_REF_NAME}" .
256+ docker images
257+
258+ - name : Publish Docker Image to DockerHub
259+ env :
260+ DOCKER_PASSWORD : ${{ secrets.DOCKER_PASSWORD }}
261+ run : |
262+ echo "Sanity check: ${DOCKER_USER}"
263+ echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USER}" --password-stdin
264+ echo "Logged into Docker Hub"
265+ docker push "${DOCKER_USER}/${IMAGE_REF}"
266+ echo "Published in Dockerhub :)"
267+ docker logout
0 commit comments