travis 安全环境变量未在 tox 中使用 [英] travis secure env variables not used in tox

查看:27
本文介绍了travis 安全环境变量未在 tox 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的 travis 构建日志中看到 env 变量已正确导出:

I can see in my travis build log that env variables are exported correctly :

Setting environment variables from .travis.yml
$ export K_API_KEY=[secure]
$ export K_PRIVATE_KEY=[secure]
$ export TOXENV=py27

但是,在我的测试中没有选择它们,这些测试使用基本的 config.py 文件,该文件应该以这种方式获取 env 变量(API_KEY = os.environ['K_API_KEY']),请参阅相关的 travis 日志:

However they aren't picked in my tests which use a basic config.py file that just should get the env variables this way (API_KEY = os.environ['K_API_KEY']), see relevant travis log:

$ source ~/virtualenv/python2.7/bin/activate
$ python --version
Python 2.7.9
$ pip --version
pip 6.0.7 from /home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages (python 2.7)
install
1.34s$ pip install -U tox
Collecting tox
  Downloading tox-2.3.1-py2.py3-none-any.whl (40kB)
    100% |################################| 40kB 1.2MB/s  
Collecting virtualenv>=1.11.2 (from tox)
  Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
    100% |################################| 1.8MB 262kB/s 
Collecting py>=1.4.17 from https://pypi.python.org/packages/19/f2/4b71181a49a4673a12c8f5075b8744c5feb0ed9eba352dd22512d2c04d47/py-1.4.31-py2.py3-none-any.whl#md5=aa18874c9b4d1e5ab53e025008e43387 (from tox)
  Downloading py-1.4.31-py2.py3-none-any.whl (81kB)
    100% |################################| 86kB 3.5MB/s 
Collecting pluggy<0.4.0,>=0.3.0 (from tox)
  Downloading pluggy-0.3.1-py2.py3-none-any.whl
Installing collected packages: pluggy, py, virtualenv, tox
  Found existing installation: py 1.4.26
    Uninstalling py-1.4.26:
      Successfully uninstalled py-1.4.26
Successfully installed pluggy-0.3.1 py-1.4.31 tox-2.3.1 virtualenv-15.0.2
$ tox
GLOB sdist-make: /home/travis/build/euri10/pykraken/setup.py
py27 create: /home/travis/build/euri10/pykraken/.tox/py27
py27 installdeps: -r/home/travis/build/euri10/pykraken/requirements_dev.txt
py27 inst: /home/travis/build/euri10/pykraken/.tox/dist/pykraken-0.1.0.zip
py27 installed: alabaster==0.7.8,argh==0.26.2,Babel==2.3.4,bumpversion==0.5.3,cffi==1.6.0,coverage==4.0,cryptography==1.3.2,docutils==0.12,enum34==1.1.6,flake8==2.4.1,idna==2.1,ipaddress==1.0.16,Jinja2==2.8,MarkupSafe==0.23,mccabe==0.3.1,pathtools==0.1.2,pep8==1.7.0,pluggy==0.3.1,py==1.4.31,pyasn1==0.1.9,pycparser==2.14,pyflakes==0.8.1,Pygments==2.1.3,pykraken==0.1.0,pytest==2.8.3,pytz==2016.4,PyYAML==3.11,requests==2.10.0,six==1.10.0,snowballstemmer==1.2.1,Sphinx==1.3.1,sphinx-rtd-theme==0.1.9,tox==2.1.1,virtualenv==15.0.2,watchdog==0.8.3
py27 runtests: PYTHONHASHSEED='2032885705'
py27 runtests: commands[0] | py.test --basetemp=/home/travis/build/euri10/pykraken/.tox/py27/tmp
============================= test session starts ==============================
platform linux2 -- Python 2.7.9, pytest-2.8.3, py-1.4.31, pluggy-0.3.1
rootdir: /home/travis/build/euri10/pykraken, inifile: 
collected 0 items / 2 errors 
==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_private.py ____________________

tests/test_private.py:5: in <module>
    from pykraken.config import PROXY, API_KEY, PRIVATE_KEY
pykraken/config.py:4: in <module>
    API_KEY = os.environ['K_API_KEY']
.tox/py27/lib/python2.7/UserDict.py:23: in __getitem__
    raise KeyError(key)
E   KeyError: 'K_API_KEY'

我怀疑是我的 tox.ini(见下文)没有选择这些变量,但不确定,知道吗?

I suspect it's my tox.ini (see below) that doesn't pick those variables, but not sure, any idea ?

[tox]
envlist = py27, flake8
; envlist = py26, py27, py33, py34, py35

[flake8]
max-line-length= 100
exclude= tests/*

[testenv]
setenv =
    PYTHONPATH = {toxinidir}:{toxinidir}/pykraken
deps =
    -r{toxinidir}/requirements_dev.txt
commands =
    py.test --basetemp={envtmpdir}

推荐答案

正如@jonrsharpe 所指出的,解决方案是使用 passenv 在 tox.ini 中的选项,如 文档

As righlty pointed by @jonrsharpe the solution is to use the passenv option in tox.ini as described in the documentation

[testenv]
setenv =
    PYTHONPATH = {toxinidir}:{toxinidir}/pykraken
passenv =
    K_API_KEY
    K_PRIVATE_KEY

这篇关于travis 安全环境变量未在 tox 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆