如何让jenkins运行" pip install"?

时间:2013-02-09 15:43:07

标签: jenkins continuous-integration pip

我有一个git repo,想让jenkins克隆它然后运行

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

任何想法如何解决这个问题?此外,当我在终端中运行pip install -r requirement.txt作为jenkins用户时,它不需要sudo权限。我可以让jenkins(流程)作为jenkins用户运行吗?

4 个答案:

答案 0 :(得分:23)

您必须运行sudo才能运行pip,这是一个很大的警告,表明您的虚拟环境无法运行。构建输出显示pip正在系统site-packages目录中安装需求,这不是virtualenv的工作方式。

您的构建脚本实际上并未保留已激活的虚拟环境。 activate脚本设置的环境变量在子bash进程中设置,不会传播到构建脚本。您应该使用activate脚本而不是运行单独的shell:

virtualenv venv --distribute
. venv/bin/activate 
pip install -r requirements.txt
python tests.py

如果你将它作为一个构建步骤运行,那应该可以工作(并在 venv 中安装你的软件包)。如果要添加更多步骤,则需要在其他步骤中设置PATH环境变量。您可能最好提供pippython的完整路径,以确保您不依赖于系统包安装。

答案 1 :(得分:3)

尝试使用

stage('test') {
     agent {
          docker {
               image 'qnib/pytest'
          }
     }
     steps {
          sh 'virtualenv venv && . venv/bin/activate && pip install -r requirements.txt && python tests.py'
     }
}

答案 2 :(得分:1)

我完全同意所说的内容......但是为了使这更像“詹金斯”

创建一个基本项目 - 并在自定义步骤中执行类似这样的操作

PROJECT="Tree"


rm -Rf ~/Builds/$PROJECT
CODE_HOME=~/Builds/$PROJECT/code
PYENV_HOME=~/Builds/$PROJECT/python
export PYENV_HOME
export PYTHONPATH=""
echo "Creating new Python env"
/usr/local/bin/python3 -m venv  --clear $PYENV_HOME
source $PYENV_HOME/bin/activate

echo "Get Project"
mkdir -p $CODE_HOME
cd $CODE_HOME
git clone https://github.com/MyUsername/MyTree.git .

pip install --upgrade pip
pip install nose
pip install coverage
pip install -r requirements.txt
python setup.py build
python setup.py install

在此之后,你可以进行鼻子测试等......

答案 3 :(得分:0)

这是我让Jenkins在Windows计算机上执行pip安装要求的步骤:

在“执行Windows批处理命令”管道中

REM activate venv, update pip and install package
cmd /k "cd <path to your directory like C:\WebAPI> & .\venv\Scripts\activate.bat & python -m pip install -U pip & pip install -r .\requirements.txt"

cmd /k执行Windows命令提示符,您可以在其中添加任何命令。 &是管道运算符。所以你可以有多个命令