skip to Main Content

I want to start matrix testing for my code. I try running unit test using the yml below on Azure Devops pipelines.

When I run it, it shortens "3.10" to "3.1". How do I avoid that? enter image description here

parameters:
  - name: imageList
    type: object
    default: ["windows-latest", "ubuntu-latest", "windows-2019", "ubuntu-20.04"]
  - name: pythonList
    type: object
    default:
      - "3.11"
      - "3.10" # WHAT TO DO HERE
      - "3.9"

stages:
  - stage: test_on_microsoft_hosted_agents
    jobs:
      - ${{ each image in parameters.imageList }}:
        - job: ${{ replace(replace(image, '-', '_'), '.', '_') }}
          pool:
            vmImage: ${{ image }}
          strategy:
            maxParallel: 1
            matrix:
              ${{ each python in parameters.pythonList }}:
                Python${{ replace(python, '.', '') }}:
                  python.version: "${{ python }}"
                  platform.name: "${{ image }}"
          steps:
            - task: UsePythonVersion@0
              inputs:
                versionSpec: "$(python.version)"
              displayName: "Use Python $(python.version)"

3

Answers


  1. Try specifying using the format function to ensure that the version string remains intact

    parameters:
      - name: imageList
        type: object
        default: ["windows-latest", "ubuntu-latest", "windows-2019", "ubuntu-20.04"]
      - name: pythonList
        type: object
        default:
          - "3.11"
          - "3.10"
          - "3.9"
    
    stages:
      - stage: test_on_microsoft_hosted_agents
        jobs:
          - ${{ each image in parameters.imageList }}:
            - job: ${{ replace(replace(image, '-', '_'), '.', '_') }}
              pool:
                vmImage: ${{ image }}
              strategy:
                maxParallel: 1
                matrix:
                  ${{ each python in parameters.pythonList }}:
                    Python${{ format('{0:.2f}', python) | replace('.', '') }}:
                      python.version: "${{ python }}"
                      platform.name: "${{ image }}"
              steps:
                - task: UsePythonVersion@0
                  inputs:
                    versionSpec: "$(python.version)"
                  displayName: "Use Python $(python.version)"
    
    Login or Signup to reply.
  2. I try avoiding as much formatting etc work in azure pipelines as possible as it can get cursed, its typically easier (from my experience) to scan the parameter and then toggle a classic matrix based on those values.

    The way I would implement this python versioning strategy is as follows:

    parameters:
      # as is
    ...
    strategy:
      matrix:
        ${{ if contains(parameters.pythonList, '3.8') }}: 
          Python38:
            python.version: '3.8'
        ${{ if contains(parameters.pythonList, '3.9') }}: 
          Python39:
            python.version: '3.9'
        ${{ if contains(parameters.pythonList, '3.10') }}: 
          Python310:
            python.version: '3.10'
        ${{ if contains(parameters.pythonList, '3.11') }}: 
          Python311:
            python.version: '3.11'
        ${{ if contains(parameters.pythonList, '3.12') }}: 
          Python312:
            python.version: '3.12'
    ...
    - task: UsePythonVersion@0
      displayName: 'Use Python Version: $(python.version)'
      inputs:
        versionSpec: '$(python.version)'
    

    This also removes all the formatting and splitting malarky, and is the way azure recommends handling this versioning stuff

    Login or Signup to reply.
  3. This is a dirty workaround, but consider adding a prefix to the version number to force it to be treated as a string, instead of a number.

    Instead of:

    parameters:
      - name: pythonList
        type: object
        default:
          - "3.11"
          - "3.10"
          - "3.9"
    

    Do:

    parameters:
      - name: pythonList
        type: object
        default:
          - "v3.11"
          - "v3.10"
          - "v3.9"
    

    Prefix v can be easily removed when setting the version in UsePythonVersion@0 task.

    Example

    trigger: none
    
    parameters:
      - name: imageList
        type: object
        default: 
          - windows-latest
          - ubuntu-latest
          - windows-2019
          - ubuntu-20.04
      - name: pythonList
        type: object
        default:
          - v3.11
          - v3.10
          - v3.9
    
    stages:
      - stage: test_on_microsoft_hosted_agents
        jobs:
          - ${{ each image in parameters.imageList }}:
            - job: ${{ replace(replace(image, '-', '_'), '.', '_') }}
              pool:
                vmImage: ${{ image }}
              strategy:
                maxParallel: 1
                matrix:
                  ${{ each python in parameters.pythonList }}:
                    Python${{ replace(python, '.', '') }}:
                      python.version: "${{ replace(python, 'v', '') }}" # <--------------- remove 'v' from version
                      platform.name: "${{ image }}"
              steps:
                - task: UsePythonVersion@0
                  inputs:
                    versionSpec: "$(python.version)"
                  displayName: "Use Python $(python.version)"
    

    Running the pipeline:

    Pipeline jobs and tasks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search