我有一个git repo,想让詹金斯克隆它,然后运行
virtualenv venv --distribute
/bin/bash venv/source/activate
pip install -r requirements.txt
python tests.py
jenkins的控制台输出为:
+ virtualenv venv --distribute
New python executable in venv/bin/python
Installing distribute..........................done.
Installing pip...............done.
+ /bin/bash venv/bin/activate
+ pip install -r requirements.txt
Downloading/unpacking flask (from -r requirements.txt (line 1))
Running setup.py egg_info for package flask
SNIP
creating /usr/local/lib/python2.7/dist-packages/flask
error: could not create '/usr/local/lib/python2.7/dist-packages/flask': Permission denied
----------------------------------------
Command /usr/bin/python -c "import setuptools;__file__='/var/lib/jenkins/workspace/infatics-website/build/flask/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-hkdBAi-record/install-record.txt failed with error code 1
Storing complete log in /home/jenkins/.pip/pip.log
Build step 'Execute shell' marked build as failure
Finished: FAILURE
我试过在命令前添加sudo,但这也不起作用:
+ sudo pip install -r requirements.txt
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
Build step 'Execute shell' marked build as failure
Finished: FAILURE
任何想法如何解决这个问题?另外,当我以jenkins用户身份在终端中运行pip install -r requirements.txt时,它不需要sudo权限。我可以让jenkins(该进程)以jenkins用户身份运行吗?
最佳答案
必须运行使用sudo
来运行pip
的事实是对您的虚拟环境无法正常工作的重大警告。构建输出显示pip
在系统site-packages目录中安装需求,这不是virtualenv的工作方式。
您的构建脚本实际上并未保留激活的虚拟环境。由激活脚本设置的环境变量是在子bash进程中设置的,不会传播到构建脚本。您应该提供activate
脚本而不是运行单独的shell:
virtualenv venv --distribute
. venv/bin/activate
pip install -r requirements.txt
python tests.py
如果您将此操作作为一个构建步骤运行,则应该可以正常运行(并在venv中安装软件包)。如果要添加更多步骤,则需要在其他步骤中设置PATH环境变量。您最好提供
pip
和python
的完整路径,以确保您不依赖于系统软件包的安装。关于jenkins - 如何让jenkins运行“pip install”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14789730/