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

How to run different tox command inside Jenkins environment?

我想为 py.test 使用不同的命令行参数,具体取决于环境:运行在本地应该只有默认参数,但在 Jenkins 上我想添加 --junitxml=junit-{envname}.xml,因此测试结果可以以良好的格式发布。 我从 documentation, that there is special [tox:jenkins] section, which should be used 知道如果定义了 'JENKINS_URL''HUDSON_URL'。所以现在我创建了简单的 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

并定义了JENKINS_URL环境变量:

export JENKINS_URL=true

我希望,如果我 运行 tox,那么我原来的命令将被替换为 echo,但它不起作用,相反我以执行原始命令结束。 有人可以帮我解决这个问题吗?

好的,自己找到了解决方案:

[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

在 Jenkins 中,只需像这样定义 CUSTOM_ARGS 环境变量:

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

您本可以使用 positional arguments

像这样:

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

在 Jenkins 中你可以这样调用 tox:

tox -- --override-flag

注意两个破折号的分隔 --