从 Azure DevOps Yaml 管道中的 Python 脚本任务中的变量组访问变量

问题描述

我在 Azure DevOps Yaml 管道中使用了“文件”类型的 Python 脚本任务。我需要使用我在 Python 文件的变量组中定义的变量。 以下是我在 Azure devops yaml 管道上的任务。

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

关于我如何实现这一目标的任何建议?

谢谢!

解决方法

您需要使用参数将变量传递给脚本,当然您还需要引用变量组:

variables:
- group: variableGroup

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

然后在脚本中使用'argparse'。

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

如果您使用的是内联脚本,您可以这样做:

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