Build a super detailed tutorial using docker + Jenkins + Python 3 environment

Time:2021-9-23

preface:

After the automation is written, it needs to run regularly on the server every day. For such a problem, the Jenkins container is installed in docker, and then pull the code from GIT. It is found that the code is in the directory of the Jenkins container. When running, it will prompt that the python environment is not installed and there is no third-party library.

There are three solutions:

The first method: mount the directory of the Jenkins container to the host directory when starting the Jenkins container and execute pass (the method cannot be implemented)

The second is to create a local node on Jenkins, pull the code locally, and then run the local project (it is more convenient to use on the local machine, but has less limitations)

The third method: re encapsulate the Jenkins image, install the environment of Python 3 in the Jenkins image, and execute the project in the container (the steps are troublesome, but the subsequent permanent use)

Both the first method and the second method require the installation of docker and Jenkins images in the server

1、 Install docker

Install dockerhttps://www.runoob.com/docker/docker-tutorial.html

2、 Install jenkisn image

1. Find the image on docker
    docker search jenkins
  
2. Download the image you need
    docker pull jenkinsci/blueocean
  
3. Check whether the download is successful
    docker images
  
4. Start the downloaded image
  docker run -d -p 10240:8080 -p 10241:50000 -v /jenkins_autotest/jenkins_home:/var/jenkins_home -v /etc/localtime:/etc/localtime  -e JAVA_OPTS=-Duser.timezone=Asia/Shanghai --name jenkint_test jenkinsci/blueocean
 
#Description: docker run runs
  -d: Run container in the background - P: specify the port mapping of the container - P 10240:8080 means that the 8080 port of the container is mapped to the 10240 port of the host. Map multiple ports using - P 8082:8080 - P 8083:8081
   -v: Mount the host directory and the directory / users / songpeilun / Jenkins in the docker container_ Home absolute directory of local host/ var/jenkins_ Home container directory (mount the container directory locally)
   -v: / etc / Localtime: / etc / Localtime local time and container time synchronization
   -e JAVA_ Opts = - duser. Timezone = Asia / Shanghai set the container time to Shanghai time (otherwise the container time is 8 hours different from the local time)
   --name jenkint_ Test sets the name of the container
   Jenkinsci / BlueOcean select the image name to start the container (latest will be added after the default image. If you pull the image created by yourself, add your version number jenkinsci / BlueOcean: version tag)

   ⚠️  When attaching a local directory to a container directory, you must give permission to the local folder Chmod 777 Jenkins_ home

Enter docker images to check whether the Jenkins image is successfully installed


docker images

See if the container is running


docker ps

It can be seen that there are already running containers Jenkins in docker

This means that Jenkins has been installed and you can start Jenkins now

3、 Start Jenkins

The browser enters the IP + port number of the server http://47.99.98.250:10240/ If not, turn off the system firewall

There are two ways to view passwords: the first is to view them in the server container, and the second is to view them locally (provided you map the local directory)

Well, Jenkins has been installed here

View password in container: cat / var / Jenkins_ home/secrets/initialAdminPassword
Map local directory: cat / users / songpeilun / Jenkins_ home/secrets/initialAdminPassword
#/Users/songpeilun/jenkins_ Home local mapping directory address

3、 Method 2: create a new local node

Let’s talk about the specific logic of method 2, which is mainly to create a new Jenkins local node and use the new node to pull the code on git to the local (host) to execute the code

This method is mainly applicable to a set of environments where there is already a set of Python 3 in the local area, which can directly run automation and realize daily automation report by using local continuous integration (disadvantage: because the local node uses the host address as the IP, the computer and Jenkins address need to be turned on every time, and the external network cannot be accessed, so the limitation is small)

Step 1: install SSH agent plug-in in Jenkins

Install SSH agent plug-in system settings – plug-in management – optional plug-ins, and then enter SSH agent search

Step 2: add a node

System settings – node management – new node (node name cannot be duplicate)

explain:

Node name: try to use English and characters instead of Chinese characters (there will be a warning when selecting a node when creating a project!)

Node Description: optional

Number of actuators: the number of nodes running at the same time

Remote working directory: the working directory of the machine (/ users / songpeilun / Jenkins), which requires the user with root permission to have all permissions on this file

Label: optional

Usage: use this node whenever possible

Startup method: Launch agents via SSH

Host: local network IP address (Network – network settings – IP)

Credentials: select the added local account password

  Host Key Verification Strategy:Non verifying Veriflication Strategy

Availability: try to keep the agent online

Node attribute — git: installation path of GIT

Then start the node. The following indicates that the node is started successfully. Then check whether there are files of the node in the folder of the local host directory

3、 Method 3: install the python 3 environment in the container

Let’s talk about the specific logic of method 3, which is mainly to run the code in the container. Git pulls the code into the container by default and directly executes the code in the container without mapping (advantages: it does not occupy the resource size of the server and can be started at any time; it does not need to start the service locally and directly use the server address; disadvantages: installation is a little troublesome)

Step 1: you need to enter the Jenkins container  

Docker exec - ITU root container ID / bin / Sh

There are two ways to install Python 3

1. Webget installation (not recommended, basically the same as the current installation method. First install some dependent packages, such as GCC)

2. APK add installation (this version is alpine: APK add, which can be installed quickly)

Finally, pay attention to the system version:

If CentOS, it is Yum; If it is Ubuntu, it is apt get; If it’s alpine, it’s APK

Step 2: install python3 with apk


cd /etc/apk/
echo "https://mirrors.ustc.edu.cn/alpine/v3.6/main/" > repositories
echo "https://mirrors.ustc.edu.cn/alpine/v3.6/community/" >> repositories

Update the APK source so that the download is faster


apk update

Step 3: use APK add to install the python 3 environment


apk add python3

Step 4: update PIP3

pip3 install --upgrade pip

Verifying the python environment
python -V
Pip3 -V

After the above installation is successful, you can install the plug-ins of the third-party library. It is half done

Plug ins are installed according to their own project needs. My project is listed below

Step 5: install the python third-party library

First create the requirements.txt file

  pip freeze > requirements.txt

  pip install -r requirements.txt

Then use vi requirements.txt to open the file for editing

base64-test0926 ==1.0.0
        PyMySQL==1.0.2
        pytest==6.2.3
        python-jenkins==1.7.0
        PyYAML==5.4.1
        redis==3.5.3
        requests==2.25.1
        SQLAlchemy==1.4.10
        urllib3==1.26.4
        wheel==0.36.2
        allure-pytest==2.8.40
        allure-python-commons==2.8.40

The last two need to be installed after configuring the allure report, otherwise an error will be reported

⚠️ If the installation is wrong, don’t worry. Find out the reason slowly. I found it for a long time after the error. The reason is the lack of Greenlet plug-in (the method will be provided later)

If there is no problem, the installation will be successful. You can test your code CP into the container. When there is no problem, you can package the image

Copy the local files to the XX directory of the container

docker cp /Users/songpeilun/python_ data/allure-2.13.0.zip jenkint_ Test: / XX directory

I use the allure report here, so I want to install it

Step 6: install the allure Report

Allure is a decompression package. Here you can download it and copy it to the container for decompression

Link:https://pan.baidu.com/s/1JbBXOfA0j6saZh7H8RMipwPassword: qfnv

#Extract the file from the container
unzip allure-2.13.0.zip

#Configure environment variables
vi /etc/profile
Add after path: $path: / allure-2.13.0/bin (full path of allure)
Source / etc / profile is saved and effective

Step 7: verify allure


allure --version

4、 Package / upload image

Follow continuous updates

5、 Configure Jenkins environment

This is the end of this article about using docker + Jenkins + Python 3 environment to build a super detailed tutorial. For more information about docker + Jenkins + Python 3 environment, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!