简体   繁体   English

如何在Jenkins环境中运行不同的tox命令?

[英]How to run different tox command inside Jenkins environment?

I would like to use different command line arguments for py.test, depending on environment: running locally there should be only default ones, but on Jenkins I would like to add --junitxml=junit-{envname}.xml , so the test results could be published in nice format. 我想为py.test使用不同的命令行参数,具体取决于环境:在本地运行应该只有默认值,但在Jenkins上我想添加--junitxml=junit-{envname}.xml ,所以测试结果可以以漂亮的格式发布。 I know from documentation , that there is special [tox:jenkins] section, which should be used in case there is defined 'JENKINS_URL' or 'HUDSON_URL' . 我从文档中知道,有一些特殊的[tox:jenkins]部分,如果有定义的'JENKINS_URL''HUDSON_URL'应该使用它。 So now I created simple tox.ini file: 所以现在我创建了简单的tox.ini文件:

[tox]
envlist = py27, py35

[tox:jenkins]
commands = echo "We are in JENKINS!"

[testenv]
setenv =
    PYTHONPATH = {toxinidir}:{toxinidir}/my_module
commands = python setup.py test

deps =
    -r{toxinidir}/requirements.txt
    -r{toxinidir}/requirements_test.txt

And have defined JENKINS_URL environment variable: 并定义了JENKINS_URL环境变量:

export JENKINS_URL=true

I expect, that if I run tox, then my original command will be substituted with echo , but it doesn't work, instead I end with original command been executed. 我希望,如果我运行tox,那么我的原始命令将被echo替换,但它不起作用,而是我以原始命令结束执行。 Could someone help me with this problem? 有人可以帮我解决这个问题吗?

Okay, found the solution myself: 好的,我自己找到了解决方案:

[tox]
envlist = py27, py35

[testenv]
setenv =
    PYTHONPATH = {toxinidir}:{toxinidir}/my_module
commands = py.test {env:CUSTOM_ARGS} ; <== use environment variable here

deps =
    -r{toxinidir}/requirements.txt
    -r{toxinidir}/requirements_test.txt

and in Jenkins just define CUSTOM_ARGS environment variable like this: 在Jenkins中只需定义CUSTOM_ARGS环境变量,如下所示:

export CUSTOM_ARGS="--junitxml=junit.xml"

You could have used positional arguments . 你可以使用位置参数

Like this: 像这样:

commands = py.test --mandatory-flag {posargs:--default-flag}

And in Jenkins you can call tox like this: 在詹金斯你可以像这样打电话给:

tox -- --override-flag

Note the separation with two dashes -- . 注意用两个破折号分隔--

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

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