First, the idea of implementation
- Install Docker on the Linux server
- Create Jenkins container
- Build a Python image from the automation project dependencies (Build an automated Python environment)
- Run the new Python container and execute the automation project Jenkins pulled from the repository
- Delete the container after execution
two Environment to prepare
- 1, have a cloud server directly use the cloud server 2, do not have a cloud server students refer to my blog to install their own virtual machine VMware install CentOS7 steps diagram
- Docker pull Python :3-alpine docker pull Python :3-alpine
3. Create Jenkins container
- Command docker run – dit – name = jenkins_save01 -p 9999:8080 – u = root – v/var/run/docker. The sock: / var/run/docker. The sock – v /usr/bin/docker:/usr/bin/docker jenkins/jenkins:alpine
- Parameter Description -i: run the container. -T: enter the command line after the container is started. -D: create the container in daemon mode and run it in background. Container users has been designated as the root user – v/var/run/docker. The sock: / var/run/docker. The sock: The docker. The sock is mapped to the Jenkins container docker. The sock file is docker client daemon communications with the docker file – v/usr/bin/docker: / usr/bin/docker: Map host docker client to Jenkins container
- 1. If the Jenkins container ID is returned, the Jenkins container is successfully created
Four. Configuring Jenkins Containers
- 1. http://192.168.1.66 is the IP address of the Linux server; 2. 9999 is the port mapped to the host
2. Run the docker logs jenkins_save01 command on the Linux server to obtain the administrator password
3. Copy the password and enter it in the password box. Click Next to go to the following page (wait for a while, about 1 minute).
4. Deselect the default plug-in and go to the next step
5. Set the administrator username and password (use this to log in to Jenkins in the future)
6. Configure an example
7. After Jenkins initialization, see the following interface
Five. Jenkins install plugins
-
Click on the next step to start using Jenkins and go to the following interface
2. Click On Manage Jenkins to go to the following page
1. Install the Git plug-in
2. Docker plug-in installation
3. HTML Publisher plug-in installation
Docker restart Jenkins_save01
Six. Create Jenkins task
- Re-log in Jenkins and click New Item
2. Create free-style projects
Seven. Configuring Jenkins Tasks
- Configure build record retention rules
2. Configure the Git repository
3. Perform the configuration after construction
1. Add the post-build operation module
2. Configure the running script
3. Configure the test report path shown in Jenkins
4. Save all configurations of the project
Build a Python image with the project execution environment
-
Export the project dependencies to a requirements. TXT file named PIP freeze > requirements.txt
-
Create a new directory on the host machine (my directory is /app) and copy the requirements. TXT file into it
-
Create Dockerfile in app directory (filename must be Dockerfile, no suffix)
-
The requirements. TXT file contains the following contents
Unittestreport ==1.3.2 OpenPyXL == 3.0.7DDT ==1.4.2 Pytest ==6.2.2 Selenium ==3.141.0 Pymysql ==1.0.2 FAker ==8.1.0 Jsonpath ==0.82 BeautifulReport==0.1.3 RSA ==4.7.2 pytest-html==3.1.1 allure-pytest==2.9.43 requests- Toolbelt ==0.9.1
5. The content and explanation of the Dockerfile are as follows
WORKDIR /app # Change the workspace directory ADD./requirements.txt /app # add python project depend on the package file to the mirror RUN pip3 install - r requirements. TXT - I # https://pypi.tuna.tsinghua.edu.cn/simple installation depend on packet CMD ["python3", "main.py"] # set the command to be executed automatically by the containerCopy the code
Docker build -t python3.9:haili. -t: python name (.) : dot indicates the directory where the Dockerfile file is located. I am now in the app directory. Dot indicates the current directory
6. You will see a new image (Docker images) after a successful build.
Eight. To complete the build. Sh
-
The build.sh content is as follows
-w=WORKSPACE #–volumes from=jenkins_save01: Map workspace from the Jenkins container to the Python container, Docker run –rm -w=WORKSPACE –volumes-from=jenkins_save01 python3.9:haili echo “Python execution automation succeeded”
2. Copy build.sh to your project root directory
Git commit -am’update’ Git push
Nine. To build the Jenkins task
- Go back to the Jenkins home page and find the project site to build
2. View the build process
3. Click build record
4. View the build process (Output logs from the Python automation project can also be seen here)
5. The construction process is shown below
6. View the test report
7. At this time, there will be no style in the test report, because Jenkins has disabled THE CSS style, so we need to solve it manually
Solve the problem that the test report has no style
Put this code in implementation: System. SetProperty (” Hudson. Model. DirectoryBrowserSupport. CSP “, “”)
If nothing is returned after the execution succeeds, no problem is found