jenkins: install python3 environment into jenkins in docker container

Use docker to install the jenkins environment. The workspace directory built by jenkins is built in the container by default. If we want to execute python3 code, we need to install the python3 environment inside the container.

Encounter problems

Enter pip3 when building, prompt: pip3: not found

The console output after the job is built

Started by user admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/yoyoketang
[yoyoketang] $ /bin/sh -xe /tmp/jenkins2225854290036644814.sh
+ pwd
/var/jenkins_home/workspace/yoyoketang
+ pip3
/tmp/jenkins2225854290036644814.sh: 3: /tmp/jenkins2225854290036644814.sh: pip3: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

pip3: not found, indicating that there is no python3 environment inside the jenkins container

Into the jenkins container

Enter the docker container, add -u root to enter the container with root user privileges

docker exec -it -u root 容器id /bin/bash

View current operating systemcat /etc/issue

root@cb8e397d5308:/# uname -a
Linux cb8e397d5308 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 GNU/Linux
root@cb8e397d5308:/# cat /etc/issue
Debian GNU/Linux 9 \n \l

What I found here is the Debian system (not ubuntu, nor centos), here yum is not available, so you can’t install it with yum, here use apt-get instead of yum, perform the update apt-get first

apt-get update

Install python3 environment inside the container

Download the python3 installation package

Enter the inside of the container, linux install python3 environment, refer to this https://www.cnblogs.com/yoyoketang/p/10195102.html

root@cb8e397d5308:/var/jenkins_home# cd /var/jenkins_home/
root@cb8e397d5308:/var/jenkins_home# mkdir python3
root@cb8e397d5308:/var/jenkins_home# cd python3/
root@cb8e397d5308:/var/jenkins_home/python3# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
root@cb8e397d5308:/var/jenkins_home/python3# tar -xvf Python-3.6.8.tgz
root@cb8e397d5308:/var/jenkins_home/python3# ls
Python-3.6.8  Python-3.6.8.tgz
root@cb8e397d5308:/var/jenkins_home/python3# cd Python-3.6.8
root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# ./configure --prefix=/var/jenkins_home/python3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/var/jenkins_home/python3/Python-3.6.8':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

The ./configure --prefix=/var/jenkins_home/python3above error was reported during execution  : configure: error: no acceptable C compiler found in $PATH

apt-get install dependencies

This is because of the lack of gcc related dependencies, use apt-get instead of yum to install related dependencies

apt-get -y install gcc automake autoconf libtool make
apt-get -y install make*
apt-get -y install zlib*
apt-get -y install openssl libssl-dev
apt-get install sudo

make compile and install

Execute make and make install in the /var/jenkins_home/python3/Python-3.6.8 directory to install

./configure --prefix=/var/jenkins_home/python3 --with-ssl
make
make install

Add soft link

Add python3 soft link

ln -s /var/jenkins_home/python3/bin/python3.6 /usr/bin/python3

Add pip3 soft link

ln -s /var/jenkins_home/python3/bin/pip3 /usr/bin/pip3

Check the environment

Enter pip3 and python3 to check the environment

root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# pip3

root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# python3
Python 3.6.8 (default, Jan  1 2020, 10:15:14) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Use pip3 to install a requests package

pip3 install requests

ssl problem

If you encounter ssl related issues when installing pip3: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

This is because of the lack of ssl dependency packages. The online solution is yum install openssl-devel , because the Debian system does not have yum, install it with apt-get

apt-get -y install openssl libssl-dev

After the installation is complete, only the pip installation problem corresponding to the python2 that comes with the system can be solved, but the pip3 installation problem of python3 cannot be solved.

Solution: add parameter --with-ssl when compiling above

./configure --prefix=/var/jenkins_home/python3 --with-ssl

Just re-execute make and make install

You can also check whether you can import ssl in the python environment

root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# python3
Python 3.6.8 (default, Jan  1 2020, 10:15:14) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> 

Guess you like

Origin blog.csdn.net/chuancheng_zeng/article/details/109758172