将参数传递给 Azure Devops 中的 python 脚本

Nat*_*sha 4 yaml azure python-3.x azure-devops

我正在尝试将系统变量从 azure devops 传递给 python 脚本。这是我目前在 Yaml 文件中的内容:

- script: pytest test/test_pipeline.py 
          --$(global_variable) 
          --junitxml=$(Build.StagingDirectory)/test_pipeline-results.xml
          displayName: 'Testing Pipeline'
          condition: always()
Run Code Online (Sandbox Code Playgroud)

我的脚本中需要的变量是$(global_variable). 该变量包含值$(Build.SourcesDirectory)。它是全局变量。"unrecognised arguments"当我运行作业时,我收到一条错误消息。

任何解决此问题的帮助都会有所帮助。

谢谢!

编辑:

完整日志:

`##[section]Starting: Testing Pipeline
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.151.2
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
pytest test/test_pipeline.py --my_param="/home/vsts/work/1/s" --junitxml=/home/vsts/work/1/a/test_pipeline-results.xml
========================== Starting Command Output ===========================
[command]/bin/bash --noprofile --norc /home/vsts/work/_temp/64fb4a65-90de-42d5-bfb3-58cc8aa174e3.sh
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --my_param=/home/vsts/work/1/s
  inifile: None
  rootdir: /home/vsts/work/1/s

##[error]Bash exited with code '4'.
##[section]Finishing: Testing Pipeline`
Run Code Online (Sandbox Code Playgroud)

Mer*_*SFT 7

我尝试写一个简单的示例供您参考。

\n

在您的.py文件中,请使用add_argument读取命令行参数。在本期中,该命令行参数来自您指定的任务。

\n

A.py文件:

\n
import argparse \n\ndef parse_argument():\n       parse = argparse.ArgumentParser(description="ForTest!")\n       parse.add_argument("-test_data")\n       parse.add_argument("-build_dir")\n       \n
Run Code Online (Sandbox Code Playgroud)\n

然后,在PythonScript@0任务中:

\n
scriptPath: \xe2\x80\x98A.py\xe2\x80\x99\nargument: -test_data $(myVariable1) -build_dir $(myVariable2)\n
Run Code Online (Sandbox Code Playgroud)\n

myVariable1都是myVariable2我定制的变量。您都可以使用环境变量。

\n

由于在Python脚本中,add_argument用于从命令行读取参数。所以它可以接收指定的值Argument

\n

另外,对于您问题中的问题,我认为您\xe2\x80\x99d最好从脚本中删除内容:--my_param="/home/vsts/work/1/s"然后重试。因为--my_param无法为 提供有效的论证pytest

\n

希望能帮到你。

\n