11 points tell you how to run python automation with Docker+jenkins

1. Implementation idea

  1. Install docker on Linux server

  2. Create jenkins container

  3. Build a python image based on the automation project dependency package (build an automated python environment)

  4. Run the new python container and execute the automation project pulled from the warehouse by jenkins

  5. Delete the container after execution

2. Environmental preparation

  1. 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)

  2. 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   

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. Why should interface automation be done in interface automation, 2. The overall view of request in interface automation, 3. Interface combat in interface automation, etc. UP hosts more exciting videos, please pay attention to UP account . icon-default.png?t=N7T8https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337.search-card.all.click

3. Create a jenkins container

  1. 命令
    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

  2. 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

  3. Running effect
    1. If you see the returned container ID, it means that the jenkins container is successfully created

    picture

Fourth, configure the jenkins container

  1. 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

    picture

  2. Execute the command on the Linux server to obtain the administrator password
    1. Execute the command: docker logs jenkins_save01

    picture

  3. 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)

    picture

  4. Uncheck the default plug-in and go to the next step

    picture

  5. Set the administrator username and password (you will use this account password to log in to your jenkins in the future)

    picture

  6. Configuration example

    picture

  7. After jenkins initialization is complete, you can see the following interface

    picture

Five, jenkins installation plug-in

  1. Click the previous step to start using jenkins and come to the following interface

    picture

  2. Click Manage Jenkins to go to the following page

    picture

  3. Click Manage Plugins to install plugins
    1, Git plugin installation

    picture

    2. Docker plugin installation

    picture

    3. HTML Publisher plug-in installation

    picture

    4. Restart the jenkins container after the installation is complete

    docker restart jenkins_save01

6. Create jenkins task

  1. Log in to jenkins again, click New Item

    picture

  2. Create Freestyle Projects

  3. Configure build record retention rules

    picture

  4. Git repository configuration

    picture

    picture


     

    picture


     

    picture

  5. Configure post-build operations
    1. Add a post-build operation module

    picture

     2. Configure the running script

picture


3. Configure the test report path displayed in jenkins

picture


 

picture



 

picture

  4. Save all configurations of the project

picture

6. Build a python image with a project execution environment

  1. Export the project dependency package to the requirements.txt file, the name of the txt file can be freely written
    pip freeze > requirements.txt

  2. 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

  3. Create a Dockerfile file in the app directory (the file name must be Dockerfile, no suffix)

  4. The contents of the requirements.txt file are as follows

    unittestreport==1.3.2openpyxl==3.0.7ddt==1.4.2pytest==6.2.2selenium==3.141.0pymysql==1.0.2faker==8.1.0jsonpath==0.82BeautifulReport==0.1.3rsa==4.7.2pytest-html==3.1.1allure-pytest==2.9.43requests-toolbelt==0.9.1
  5. 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)

picture

8. Complete build.sh

  1. 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:hailiecho "python执行自动化执行成功"
  2. Copy build.sh to your project root directory

  3. Submit the code to the Git repository
    Git add .
    Git commit -am'update'
    Git push

Nine, to build the jenkins task

  1. Go back to the jenkins home page and find the project point build

    picture

  2. View build process

    picture

  3. pip build record

    picture

  4. View the build process (the output log of the python automation project can also be seen here)

    picture

  5. The construction process is as follows

    picture

  6. View test report

    picture

  7. At this time, the test report will have no style, because the CSS style is disabled by Jenkins, we need to solve it manually

    picture

picture

10. Solve the problem that the test report has no style

picture

picture

Put this code in and execute it: System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

picture

If the execution is successful, the return is empty, indicating that there is no problem

picture

11. You can see a styled test report after rebuilding

picture

Guess you like

Origin blog.csdn.net/m0_73409141/article/details/132669944
Recommended