I have a workflow on GitHub to run unit tests on a Python application. I need the test to run for various Python versions and various OS versions. Right now the workflow looks like this:
name: Python tests
on:
push:
branches:
- dev
pull_request:
release:
types: [published]
jobs:
release-strategy:
push-pull-strategy:
pytest:
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
...
This is a bit too heavy to be run for every pull request on dev, but on the other hand we really want to run all these tests before a release in made, so we would like to run the full matrix strategy only upon release, and run a smaller subset for all other cases, eg ["3.7", "3.11"]
and [ubuntu-20.04, ubuntu-22.04]
.
Is there a way to implement this behavior without having two separate workflows?
CodePudding user response:
I realized I could describe this logic in a previous job using standard bash instructions (instead of trying to use the workflow logic). My solution looks like this:
name: Python tests
on:
push:
branches:
- dev
pull_request:
release:
types: [published]
jobs:
select-strategy:
runs-on: ubuntu-latest
outputs:
python-versions: ${{ steps.set-matrix.outputs.python-versions }}
steps:
- id: set-matrix
run: |
if [ ${{ github.event_name }} == "release" ]; then
echo "python-versions=[\"3.7\",\"3.8\",\"3.9\",\"3.10\",\"3.11\"]" >> $GITHUB_OUTPUT
else
echo "python-versions=[\"3.7\",\"3.11\"]" >> $GITHUB_OUTPUT
fi
pytest:
needs: select-strategy
strategy:
matrix:
python-version: ${{ fromJson(needs.select-strategy.outputs.python-versions) }}
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
...