pythongitlab-citox

Tox InterpreterNotFound Gitlab-CI Pipeline


I need some help with testing my python package using tox in a gitlab-ci pipeline:

I want to test my package on multiple versions. For this, I can write the following in my tox.ini:

[tox]
envlist = py{310, 311}

[testenv]
deps =
    -rrequirements.txt
commands =
    python -m pytest tests -s

Running the command tox works locally, as I have multiple python versions installed via conda (I believe this is the reason).

Until now, I have always also tested my package in my gitlab pipeline: (.gitlab-ci.yml)

image: python:3.11

unit-test:
    stage: test
    script:
        - pip install tox
        - tox -r

This causes the pipeline to fail with the following message:

ERROR:  py310: InterpreterNotFound: python3.10
  py311: commands succeeded

Is there a gitlab ci container image already out there, that includes multiple python versions?


Solution

  • This is what I came up with for some of my projects:

    '.review':
      before_script:
        - 'python -m pip install tox'
      script:
        - 'export TOX_ENV="${CI_JOB_NAME##review}"'
        - 'tox'
    
    'review py3.9':
      extends: '.review'
      image: 'python:3.9'
    
    'review py3.12':
      extends: '.review'
      image: 'python:3.12'
    

    I have not really looked into this in a while, so there might be better solutions nowadays. Anyway, the advantage of this solution is that it avoids repeating the script part for each Python version. The "trick" is to set the TOX_ENV environment variable to something like py3.9 or py3.11, which is done be extracting this value from the name of the job which is contained in the CI_JOB_NAME environment variable set by the GitLab CI pipeline runner.