Test code on multiple Python versions with gitlab-ci.yml

Hey everyone,
I found out that GitLab recently introduced parallel: matrix to support parallel testing of multiple jobs. I wanted to use this to test my repository on Python 3.9, 3.10 and 3.11 in parallel. However, I can’t figure out or find an example of how to use the syntax.
My .gitlab-ci.yml file is as follows:
Before:

image: python:3.9
# Rest of CI

After:

image:
  parallel:
    matrix: # What now?

I would appreciate any help.
Thank you! :smiley:
Tobias

Hi Tobias,

I myself never used this parallel:matrix, but I believe what you want is not supported. parallel: matrix supports providing different variables to a job, but not a different image to run it:

Use parallel:matrix to run a job multiple times in parallel in a single pipeline, but with different variable values for each instance of the job.

From CI/CD YAML syntax reference | GitLab

So, what you need to do is to define 3 different test jobs, each running on different image:

test python39:
  stage: test
  image: python:3.9-slim
  script:
    # testing stuff

test python310:
  stage: test
  image: python:3.10-slim
  script:
    # testing stuff

test python311:
  stage: test
  image: python:3.11-slim
  script:
    # testing stuff

Repetitive, yes. To optimize this (=following DRY concepts), you can define your “test job template” (using YAML anchors) and then use it in other jobs just with different image (use this as reference, didn’t test anything :wink: ):

.test_job_template: &test_job_template
  stage: test
  script:
    # testing stuff here and everything else that jobs share (variables, rules, etc)

test python39:
  <<: *test_job_template
  image: python:3.9-slim

test python310:
  <<: *test_job_template
  image: python:3.10-slim

test python311:
  <<: *test_job_template
  image: python:3.11-slim

More on GitLab CI optimizations: Optimize GitLab CI/CD configuration files | GitLab

Hope this helps! :slight_smile:

The parallel matrix syntax allows to generate variables – if their value matches exactly a container image tag, you can take advantage for pulling Python version specific images from Docker Hub and run the code in these containers.

test:
  image: "python:$VERSION"
  script:
    - python -V
  parallel:
    matrix:
      - VERSION: ['3.9', '3.10']

if you want to use different tag names, you need to modify the VERSION variable, or amend the image string. If the container image is hosted somewhere else, e.g. the GitLab container registry, the image string needs to use the full URL prefix for the python image.

My team mate Itzik created a short video tutorial in

2 Likes