1. Implementation idea
-
Install docker on Linux server
-
Create jenkins container
-
Build a python image based on the automation project dependency package (build an automated python environment)
-
Run the new python container and execute the automation project pulled from the warehouse by jenkins
-
Delete the container after execution
2. Environmental preparation
-
One Linux server (mine is CentOS7)
1. Students who have a cloud server directly use the cloud server.
2. Students who do not have a cloud server can refer to my blog to install the virtual machine by themselves.
VMware installs Centos7 step diagram (http://testingpai. com/article/1629981536584) -
Install docker correctly on the server
1. Pull the jenkins image
jenkins/jenkins:alpine
2. Pull the python3 image
docker pull python:3-alpine
If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the number one automated testing tutorial on the entire network at station B. At the same time, the number of people online has reached 1,000, and there are notes to collect and share with you. Dashen Technical Exchange: 798478386
3. Create a jenkins container
-
命令
docker run -dit --name=jenkins_save01 -p 9999:8080 -u=root -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker jenkins/jenkins:alpine -
Parameter description
-i: means to run the container
-t: means that the container will enter its command line after startup
-d: create a container in a daemon mode and run in the background
--name: container name
-p 9999:8080: port mapping, host port: Jenkins container port
-u=root: specify the container user as the root user
-v /var/run/docker.sock:/var/run/docker.sock: map docker.sock to the jenkins container.
The docker.sock file is a docker client File for communication with docker daemon
-v /usr/bin/docker:/usr/bin/docker: Map the host docker client to the jenkins container -
Running effect
1. If you see the returned container ID, it means that the jenkins container is successfully created
Fourth, configure the jenkins container
-
Browser access http://192.168.1.66:9999, come to the jenkins unlock page
1, http://192.168.1.66 is the IP address of the Linux server
2, 9999 is the mapped port of the host -
Execute the command on the Linux server to obtain the administrator password
1. Execute the command: docker logs jenkins_save01 -
Copy the password and enter it into the password box, click Next, and you will come to the following page (you need to wait for a while in the middle, about 1 minute, wait patiently)
-
Uncheck the default plug-in and go to the next step
-
Set the administrator username and password (you will use this account password to log in to your jenkins in the future)
-
Configuration example
-
After jenkins initialization is complete, you can see the following interface
Five, jenkins installation plug-in
-
Click the previous step to start using jenkins and come to the following interface
-
Click Manage Jenkins to go to the following page
-
Click Manage Plugins to install plugins
1, Git plugin installation2. Docker plugin installation
3. HTML Publisher plug-in installation
4. Restart the jenkins container after the installation is complete
docker restart jenkins_save01
6. Create jenkins task
-
Log in to jenkins again, click New Item
-
Create Freestyle Projects
-
Configure build record retention rules
-
Git repository configuration
-
Configure post-build operations
1. Add a post-build operation module
2. Configure the running script
3. Configure the test report path displayed in jenkins
4. Save all configurations of the project
6. Build a python image with a project execution environment
-
Export the project dependency package to the requirements.txt file, the name of the txt file can be freely written
pip freeze > requirements.txt -
Create a new directory on the host machine (the machine where docker is installed) (my directory is in /app), and copy the requirements.txt file into it
-
Create a Dockerfile file in the app directory (the file name must be Dockerfile, no suffix)
-
The contents of the requirements.txt file are as follows
unittestreport==1.3.2
openpyxl==3.0.7
ddt==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
-
Dockerfile content and explanation are as follows
FROM python:3-alpine #基于python:3-alpine这个基础镜像镜像构建镜像
WORKDIR /app #切换工作空间目录
ADD ./requirements.txt /app #将python项目依赖包文件添加到镜像
RUN pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple #安装依赖包
CMD ["python3", "main.py"] #设置容器执行后自动执行的命令,这里main.py是我们自动化框架的执行入口文件
5. Execute the image build command in the app directory
docker build -t python3.9:haili .
-t: image name
Dot (.): The dot indicates the directory where the Dockerfile file is located. I am now in the app directory, and the dot indicates the current directory
6. After the build is successful, you will see a new image (docker images)
8. Complete build.sh
-
The content of build.sh is as follows
echo "运行容器python执行自动化" #输出日志
#-w=$WORKSPACE:指定workspace
#--volumes-from=jenkins_save01:将jenkins容器中的workspace映射到python容器中,此时jenkins中git拉下来的代码就会被映射进去
docker run --rm -w=$WORKSPACE --volumes-from=jenkins_save01 python3.9:haili
echo "python执行自动化执行成功"
-
Copy build.sh to your project root directory
-
Submit the code to the Git repository
Git add .
Git commit -am'update'
Git push
Nine, to build the jenkins task
-
Go back to the jenkins home page and find the project point build
-
View build process
-
pip build record
-
View the build process (the output log of the python automation project can also be seen here)
-
The construction process is as follows
-
View test report
-
At this time, the test report will have no style, because the CSS style is disabled by Jenkins, we need to solve it manually
10. Solve the problem that the test report has no style
Put this code in and execute it: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
If the execution is successful, the return is empty, indicating that there is no problem