azure-devopsazure-pipelinesazure-pipelines-yamlazure-pipelines-release-task

Azure Devops Yaml Python Script task: PythonScriptV0 , how to pass arrays as the field supports only a strings


I am trying to pass to a python scripts 3 parameters one of them is an array, this works when i run the script locally, i am using sys.argv to achieve this.

However the argument field support only strings as far i can see. How can i go around this any ideas? Thanks

the array is ${{ parameters.packageVersion }}

Code:

  - task: PythonScript@0
    displayName: 'Modify ansible inventory files (wms_common.yml) with deployed versions'
    inputs:
      scriptSource: filePath
      scriptPath: deployment/s/azure-devops/scripts/script.py
      arguments: |
          ../../inventories/${{ variables.inventory }}/group_vars/all/wms_common.yml
          ../../inventories/central/${{ variables.inventory }}/group_vars/all/wms_common.yml
          ${{ parameters.packageVersion }}

Error:

/azure-devops/wms.full.pipeline.yml (Line: 95, Col: 18): Unable to convert from Array to String. Value: Array

Edit: Reframed question


Solution

  • I think the below YAML will help you convert the array to String objects and use them.

    variables:
      myVariable1: 'value1'
      myVariable2: 'value2'
      system.debug: true
    
    parameters:
    - name: InstanceArgs 
      type: object
      default: [1,2] 
    
    steps:
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
            import argparse 
    
            parse = argparse.ArgumentParser(description="Test")
            parse.add_argument("test1")
            parse.add_argument("test2")
            args = parse.parse_args()
            print(args.test1)
            print(args.test2)
            print('this is a test.')
        # arguments: $(myVariable1) $(myVariable2)'
        arguments: ${{join(' ',parameters.InstanceArgs)}}
    

    You need to use the join expression in YAML to get the elements:

    https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#join

    enter image description here