【问题标题】:Passing arguments to python script in Azure Devops在 Azure Devops 中将参数传递给 python 脚本
【发布时间】:2019-12-13 21:37:53
【问题描述】:

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

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

我在脚本中需要的变量是$(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://docs.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`

【问题讨论】:

  • 您可以尝试使用 Python 脚本任务。在Arguments 中,您可以在其中传递变量。
  • 我也尝试过。类似这样的东西:-任务:PublishTestResults@2 条件:always() displayName:“测试用例的测试管道”输入:参数:$(global_variable)testResultsFiles:$ (Build.StagingDirectory)/test_pipeline-results.xml testRunTitle: 测试管道
  • 你在哪里定义global_variable
  • @ShaykiAbramczyk 一开始。为了确定,我也附和了这个值。它确实存储了我想要的东西。
  • 您介意更新问题中的完整日志吗?

标签: python-3.x azure azure-devops yaml


【解决方案1】:

我试着写了一个简单的示例供你参考。

在您的.py 文件中,请使用add_argument 读取命令行参数。在这个问题中,这个命令行参数来自你指定的任务。

A.py文件:

import argparse 

def parse_argument():
       parse = argparse.ArgumentParser(description="ForTest!")
       parse.add_argument("-test_data")
       parse.add_argument("-build_dir")
       

然后,在PythonScript@0 任务中:

scriptPath: ‘A.py’
argument: -test_data $(myVariable1) -build_dir $(myVariable2)

myVariable1myVariable2 都是我自定义的变量。你们都可以使用环境变量。

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

另外,对于你问题中的问题,我认为你最好将脚本中的内容删除:--my_param="/home/vsts/work/1/s",然后再试一次。因为--my_param 不能成为pytest 的有效参数。

希望能帮到你。

【讨论】:

  • 谢谢!你不知道参数中有空格吗?
  • 天哪,这个解决方案很好,但是使用的语言很糟糕。我几乎无法理解它的作用。
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多