How to run jobs/stages/builds(?) for different versions of Python?

Hey guys! I recently started using Travis and I have a problem.

I have the following .travis.yml:

language: python

python:
  - "3.9.6"
  - "3.10.1"
  - "3.11.0"

before_install:
  - curl -sSL https://install.python-poetry.org | python - --version 1.4.0

install:
  - poetry install --no-ansi

script:
  - echo "Linter"
  - echo "SonarQube"
  - echo "pytest"
  - echo "mkdocs"

stages:
  - name: Staging
    if: branch = staging

  - name: Production
    if: branch = production

jobs:
  include:
    - stage: Staging
      python: "3.9.6" 
      before_install:
        - curl -sSL https://install.python-poetry.org | python - --version 1.4.0
      install:
        - poetry install --no-ansi
      script:
        - echo "Staging - script"

    - stage: Staging
      python: "3.10.1" 
      before_install:
        - curl -sSL https://install.python-poetry.org | python - --version 1.4.0
      install:
        - poetry install --no-ansi
      script:
        - echo "Staging - script"

    - stage: Production
      install:
        - echo "Production - installation"
      script:
        - echo "Production - script"

I want to test my application in the three versions of Python, in addition to running the stages according to the condition (also in the three versions of Python).

With this format, the stage runs in the two versions I mentioned, but it seems kind of redundant to have to create a stage for each version of python. However, it runs the stage first and then the normal test.

How do I fix this?

Hi @x-steam,

Look at this example and see if this gives you a good template:

which-steps: &which-steps
  before_install:
    - which -a python2.7 ; python2.7 --version || true
    - pyenv install --list | grep 2.7 || true
    - which -a python3.5 ; python3.5 --version || true
    - pyenv install --list | grep 3.5 || true
    - which -a python3.6 ; python3.6 --version || true
    - pyenv install --list | grep 3.6 || true
    - which -a python3.7 ; python3.7 --version || true
    - pyenv install --list | grep 3.7 || true

trusty-steps: &trusty-steps
  os: linux
  <<: *which-steps

xenial-steps: &xenial-steps
  <<: *trusty-steps
  dist: xenial
  
trusty-cpp-steps: &trusty-cpp-steps
  <<: *trusty-steps
  language: cpp

xenial-cpp-steps: &xenial-cpp-steps
  <<: *xenial-steps
  language: cpp

osx-steps: &osx-steps
  os: osx
  language: shell  # 'language: python' is not yet supported on Travis CI macOS
  # addons:
  #  homebrew:
  #    update: true
  #    packages:
  #      - pyenv
  <<: *which-steps

windows-steps: &windows-steps
  os: windows
  language: shell  # 'language: python' is not yet supported on Travis CI Windows
  <<: *which-steps

language: python
matrix:
  allow_failures:
    - name: "Python 3.7 on Trusty Linux"
    - name: "Python 3.7-dev on Trusty Linux cpp"
    - name: "Python 3.7-dev on Xenial Linux cpp"
  include:
    # Trusty Linux =====================
    - name: "Python 2.7 on Trusty Linux"
      <<: *trusty-steps
      python: 2.7
    - name: "Python 3.5 on Trusty Linux"
      <<: *trusty-steps
      python: 3.5
    - name: "Python 3.6 on Trusty Linux"
      <<: *trusty-steps
      python: 3.6
    - name: "Python 3.7 on Trusty Linux"
      <<: *trusty-steps
      python: 3.7