简体   繁体   English

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

[英]Passing arguments to python script in Azure Devops

I am trying to pass a system variable to the python script from azure devops.我正在尝试将系统变量从 azure devops 传递给 python 脚本。 This is what I currently have in Yaml file:这是我目前在 Yaml 文件中的内容:

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

The variable I need in my script is $(global_variable) .我在脚本中需要的变量是$(global_variable) The variable contains a value of $(Build.SourcesDirectory) .该变量包含$(Build.SourcesDirectory)的值。 It is the global variable.它是全局变量。 I am getting an error message as "unrecognised arguments" when I run the job.当我运行作业时,我收到一条错误消息"unrecognised arguments"

Any help to tackle this will be helpful.解决此问题的任何帮助都会有所帮助。

Thanks!谢谢!

EDIT:编辑:

Complete log:完整日志:

`##[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`

I tried to write a simple sample for you refer.我试着写了一个简单的例子供你参考。

In your .py file, please use add_argument to read in the command line parameter.在您的.py文件中,请使用add_argument读取命令行参数。 In this issue, this command line parameter comes from your task specified.在本期中,此命令行参数来自您指定的任务。

A.py file: A.py文件:

import argparse 

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

And then, in PythonScript@0 task:然后,在PythonScript@0任务中:

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

myVariable1 and myVariable2 are all my customized variable. myVariable1myVariable2都是我自定义的变量。 You can all use environment variable.您都可以使用环境变量。

Since in python script, add_argument is used to read in the parameter which from command line.由于在 python 脚本中, add_argument用于add_argument读取参数。 So it can receive the value from Argument specified.因此它可以从指定的Argument接收值。

In addition, for the issue which in your question, I think you'd better delete the content from your script: --my_param="/home/vsts/work/1/s" and try again.另外,对于您问题中的问题,我认为您最好从脚本中删除内容:-- --my_param="/home/vsts/work/1/s"然后再试一次。 Because --my_param could not the valid argument for pytest .因为--my_param不能作为pytest的有效参数。

Hope can help you.希望能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM