environment-variablesazure-pipelines-yamlazure-devops-pipelinesazure-pipeline-python-script-task

Access variables from Variable Groups inside Python script task in Azure DevOps Yaml pipeline


I'm using a Python script task of type 'file' in my Azure DevOps Yaml pipeline. I need to use the variables that I defined in my Variable Group in the Python file. The following is my task on Azure devops yaml pipeline.

  - task: PythonScript@0
    displayName: 'Run a Python script'
    inputs:
      scriptPath: 'pythonTest.py'

Any advise on how I can achieve this?

Thanks!


Solution

  • You need to pass the variables to the script, using arguments, and you of course need to reference the variable group:

    variables:
    - group: variableGroup
    
    steps:
      - task: PythonScript@0
        displayName: 'Run a Python script'
        inputs:
          scriptPath: 'pythonTest.py'
          arguments: --variableInScript $(variableInVariableGroup)
    

    And then use 'argparse' in the script.

    https://learn.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python?view=azure-devops#run-python-scripts

    If you were using an inline script you could have done it like this:

    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          print('variableInVariableGroup: $(variableInVariableGroup)')