一、实现思路

  1. 在 Linux 服务器安装 docker
  2. 创建 jenkins 容器
  3. jenkins 中创建 pipeline 项目
  4. 根据自动化项目依赖包构建 python 镜像(构建自动化 python 环境)
  5. 运行新的 python 容器,执行 jenkins 从仓库中拉下来的自动化项目
  6. 执行完成之后删除容器 

二、环境准备

  • Linux 服务器一台(我的是 CentOS7)

1、有云服务器的同学直接用云服务器

2、没有云服务器的同学参考我的博客自己动手安装虚拟机吧

  • 在服务器上正确安装 docker

1、拉取 jenkins 镜像jenkins/jenkins:alpine

2、拉取 python3 镜像docker pull python:3-alpine

三、创建 jenkins 容器

  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. 参数说明
    -i:表示运行容器-t:表示容器启动后会进入其命令行-d:守护式方式创建容器在后台运行--name:容器名称-p 9999:8080:端口映射,宿主机端口:jenkins 容器端口-u=root:指定容器用户为 root 用户-v /var/run/docker.sock:/var/run/docker.sock:将 docker.sock 映射到 jenkins 容器中docker.sock 文件是 docker client 与 docker daemon 通讯的文件-v /usr/bin/docker:/usr/bin/docker:将宿主机 docker 客户端映射到 jenkins 容器中
  3. 运行效果
    1、看到返回容器 ID 表示 jenkins 容器创建成功

gerrit jenkins docker配置 jenkins docker pipeline_软件测试

四、配置 jenkins 容器

  1. 浏览器访问 http://192.168.1.66:9999,来到 jenkins 解锁页面
    1、http://192.168.1.66 为 Linux 服务器 IP 地址2、9999 为宿主机映射端口

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_02

在 Linux 服务器执行命令获取管理员密码
执行命令:docker logs jenkins_save01

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_03

复制密码输入到密码框,点下一步,来到如下页面(中间需要等待一段时间,大概 1 分钟左右,耐心等待)

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_04

  

取消默认插件勾选,进入下一步

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_05

设置管理员用户名和密码(以后就用这个账号密码登陆你的 jenkins

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_06

配置实例

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_07

jenkins 初始化完成看到如下界面

gerrit jenkins docker配置 jenkins docker pipeline_python_08

五、jenkins 安装插件

点上一步骤开始使用 jenkins,来到如下界面

gerrit jenkins docker配置 jenkins docker pipeline_测试工具_09

点 Manage Jenkins 来到如下页面

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_10

点 Manage Plugins 安装插件
1.Git 插件安装

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_11

2、Docker 插件安装

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_12


 

3、HTML Publisher 插件安装

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_13


 

4、安装 Pipeline 插件

gerrit jenkins docker配置 jenkins docker pipeline_python_14

 

5、安装完成后重启 jenkins 容器

docker restart jenkins_save01

六、创建 jenkins 任务

重新登陆 jenkins,点 New Item

gerrit jenkins docker配置 jenkins docker pipeline_python_15

选择创建 Pipeline 项目

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_16

配置构建记录保留规则

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_17

配置 Git 仓库

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_18

 添加 Git 凭证

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_19

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_20

配置 Jenkinsfile 路径

gerrit jenkins docker配置 jenkins docker pipeline_python_21

配置完成直接点保存即可

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_22

七、构建有项目执行环境的 python 镜像

  1. 将项目依赖包导出来到 requirements.txt 文件中,txt 文件名称可以随意写
    pip freeze > requirements.txt
  2. 在宿主机(安装 docker 的机子)上新建一个目录(我的目录在/app),将 requirements.txt 文件复制进去
  3. 在 app 目录下创建 Dockerfile 文件(文件名必须是 Dockerfile,没有后缀)
  4. requirement.txt 恩建内容如下
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

5.Dockerfile 内容以及解释如下

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是我们自动化框架的执行入口文件

在 app 目录下执行镜像构建命令
docker build -t python3.9:haili .
-t:镜像名称
点(.):点表示 Dockerfile 文件所在的目录,我现在在 app 目录下,点表示当前目录

6.构建成功后会看到一个新的镜像(docker images)

gerrit jenkins docker配置 jenkins docker pipeline_python_23

八、创建 build.sh 文件并放到项目根目录下

echo "运行容器python执行自动化"
docker run --rm -w=$WORKSPACE --volumes-from=jenkins_save01 python3.9:haili
echo "python执行自动化执行成功"

九、写 Jenkinsfile 文件并将放到项目根目录下

pipeline{
    //配置执行环境
    agent any
    //配置构建过程
	stages{
		stage('build'){
			steps{
			    echo '开始执行shell脚本'
				sh 'sh build.sh'
			}
		}//stage
	}//stages

   //配置构建后操作
	post{
        always{
          echo 'Pipeline 构建成功'
                   		publishHTML(target:[allowMissing: false,
					 alwaysLinkToLastBuild: true,
					 keepAll: true,
					 reportDir: 'reports',
					 reportFiles: '*.html',
					 reportName: 'My Reports',
					 reportTitles: 'The Report'])
        }
    }//post
}//pipeline

十、提交代码到远程仓库

Git add .

Git commit -am'update'

Git push

十一、构建你 jenkins 任务

点构建

gerrit jenkins docker配置 jenkins docker pipeline_测试工具_24

查看构建日志

gerrit jenkins docker配置 jenkins docker pipeline_python_25

gerrit jenkins docker配置 jenkins docker pipeline_测试工具_26

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_27

查看测试报告,此时的测试报告会没有样式,因为被 Jenkins 禁用了 CSS 样式,需要我们手动解决

gerrit jenkins docker配置 jenkins docker pipeline_软件测试_28

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_29

  

十二、解决测试报告没有样式的问题

gerrit jenkins docker配置 jenkins docker pipeline_测试工具_30

gerrit jenkins docker配置 jenkins docker pipeline_自动化测试_31

把这个代码放进去执行:System.setProperty("


hudson.model.DirectoryBrowserSupport.CSP","")

gerrit jenkins docker配置 jenkins docker pipeline_python_32

执行成功返回是空,说明没问题

gerrit jenkins docker配置 jenkins docker pipeline_职场和发展_33

重新构建就可以看到漂亮的测试报告了

gerrit jenkins docker配置 jenkins docker pipeline_python_34

都看到这了,点个赞再走呗!

同时,在这我为大家准备了一份软件测试视频教程(含面试、接口、自动化、性能测试等),就在下方,需要的可以直接去观看。