前言

python自动化的脚本开发完成后需提交到git代码仓库,接下来就是用Jenkins拉取代码去构建自动化代码了
 

新建项目

打开Jenkins新建一个自由风格的项目
Jenkins(3)拉取git仓库代码,执行python自动化脚本_python
 

源码管理
  • Repository URL 代码仓库地址
  • Credentials git仓库登陆的账号和密码凭证
  • 指定分支(为空时代表any)分支默认*/master
    Jenkins(3)拉取git仓库代码,执行python自动化脚本_Jenkins_02
    Jenkins(3)拉取git仓库代码,执行python自动化脚本_python_03
     
构建shell脚本

执行shell,先pip3安装requirements.txt,再用pytest执行脚本
Jenkins(3)拉取git仓库代码,执行python自动化脚本_Jenkins_04
查看控制台输入出,console查看日志

+ ls
requirements.txt
test_demo.py
+ pip3 install -r requirements.txt
Collecting requests==2.18.4 (from -r requirements.txt (line 1))
  Downloading 

Installing collected packages: idna, urllib3, requests, atomicwrites, six, more-itertools, wcwidth, attrs, py, zipp, importlib-metadata, pluggy, pytest, pytest-metadata, pytest-html
  Found existing installation: idna 2.8
    Uninstalling idna-2.8:
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/var/jenkins_home/python3/lib/python3.7/site-packages/idna-2.8.dist-info/INSTALLER'
Consider using the `--user` option or check the permissions.

发现没有权限安装,可以进入容器内部安装

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

打开workspace目录安装 /home/jenkins/workspace/apitest_demo

[root@3be4b6cd9b8c]# cd /home/jenkins/workspace/apitest_demo
[root@3be4b6cd9b8c]# ls
requirements.txt  test_demo.py
[root@3be4b6cd9b8c]# pip3 install -r requirements.txt
安装完成后输入pytest检查pytest:-bash: pytest: command not found

查找pytest安装地址添加软链接,输入pytest --version查看环境

[root@3be4b6cd9b8c]# find / -name pytest
/var/jenkins_home/python3/bin/pytest
[root@3be4b6cd9b8c]# ln -s /var/jenkins_home/python3/bin/pytest /usr/bin/pytest
[root@3be4b6cd9b8c]# pytest --version
pytest 6.2.1

 

构建job

上面需要的环境都安装完成后,执行shell的时候,直接输入pytest命令就可以执行自动化的脚本了
Jenkins(3)拉取git仓库代码,执行python自动化脚本_Jenkins_05
 

构建成功

Jenkins(3)拉取git仓库代码,执行python自动化脚本_python_06