lundi 22 mars 2021

How to generate a github actions build matrix that dynamically includes a list of nox sessions?

I recently started to migrate some of my open source python libraries from Travis to Github Actions. To be more independent from the CI/CD platform, I decided to first describe all test environments and configurations using nox.

Let's consider the following prototypical noxfile.py :

import nox

@nox.session(python=["3.7", "3.8"])
def tests(session):
    print("Interesting stuff going on here")

@nox.session(python=["3.7", "3.8"])
@nox.parametrize("a", [-1, 1])
@nox.parametrize("b", [False, True])
def tests_with_params(session, a, b):
    print("Interesting stuff going on here")

@nox.session(python="3.7")
def other(session):
    print("another session")

Leading to the following nox --list output:

* tests-3.7
* tests-3.8
* tests_with_params-3.7(b=False, a=-1)
* tests_with_params-3.7(b=True, a=-1)
* tests_with_params-3.7(b=False, a=1)
* tests_with_params-3.7(b=True, a=1)
* tests_with_params-3.8(b=False, a=-1)
* tests_with_params-3.8(b=True, a=-1)
* tests_with_params-3.8(b=False, a=1)
* tests_with_params-3.8(b=True, a=1)
* other

I would like the github actions workflow to run all sessions for tests, or for tests_with_params. Using a hardcoded build matrix in my workflow yaml definition file it works, for example here I list the two sessions for tests:

# (in .github/workflows/base.yml)
jobs:
  run_all_tests:
    strategy:
      fail-fast: false
      matrix:
        # Here we manually list two nox sessions
        nox_session: ["tests-3.7", "tests-3.8"]
    name: Run nox session $
    runs-on: ubuntu-latest
    steps:
      # (other steps before this: checkout code, install python and nox...)
      - run: nox -s "$"

however as can be seen above, the list of sessions has to be copied manually in the matrix. So if I decide to change the parametrization of my nox sessions, this will have to be updated.

It would therefore be far easier to have the github actions workflow "ask nox" to dynamically get this list. How can we do this ?

Aucun commentaire:

Enregistrer un commentaire