Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make jenkins run "pip install"?

I have a git repo and would like to get jenkins to clone it then run

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

The console output from jenkins is:

+ 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

I've tried adding sudo before the command, but it's doesn't work either:

+ 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

Any ideas how to get around this? Also when I run pip install -r requirement.txt in a terminal as the jenkins user it doesn't need sudo permission. Can I get jenkins (the process) to run as the jenkins user?

like image 291
nickponline Avatar asked Feb 09 '13 15:02

nickponline


People also ask

Can Jenkins run Python?

If you chose our Python Git Project, you will have to specify a build step that runs the python test runner. In the job configuration screen, scroll down to Build. Click on Add build step and choose Execute Batch Command.

Can we write Jenkins file in Python?

In this tutorial, we will implement the Jenkins CI/CD Pipeline for Python applications. We will be building a pipeline as code, aka Declarative pipeline. What is the declarative pipeline? The pipeline is the new feature of Jenkins, where we can write the Jenkins job as a sequence of steps.


1 Answers

The fact that you have to run use sudo to run pip is a big warning that your virtual environment isn't working. The build output shows that pip is installing the requirements in your system site-packages directory, which is not the way virtualenv works.

Your build script doesn't actually preserve the activated virtual environment. The environment variables set by the activate script are set in a child bash process and are not propagated up to the build script. You should source the activate script instead of running a separate shell:

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

If you're running this as one build step, that should work (and install your packages in venv). If you want to add more steps, you'll need to set the PATH environment variable in your other steps. You're probably better off providing full paths to pip and python to ensure you're not dependent on system package installations.

like image 79
Dave Bacher Avatar answered Sep 19 '22 12:09

Dave Bacher