一、docker部署jenkins

docker run -d -p 7000:8080 --name py42jenkins -u root -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker jenkins/jenkins:alpine

部署成功图下图:

docker如何自动运行 docker运行自动化_docker


命令解释:

docker run :通过镜像运行容器
–name:启动容器的名字,如:培养py42jenkins
-u:指定启动用户为root(启动参数需要加上root权限,不然会遇到启动jenkins报权限不足错误)
-p:是将容器的8080端口映射到虚拟机的7000端口,在浏览器访问7000端口就能访问jenkins
-v: 目录映射 目的是让jenkins可以执行宿主机的docker命令
-v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker jenkins/jenkins:alpine
-d 以守护线程方式运行。不加该参数,关掉命令行,docker的jenkins就随之停止了。

二、查看Jenkins是否启动

执行命令:docker ps -a

status 状态显示up 即为启动jenkins服务成功,显示启动端口7000

docker如何自动运行 docker运行自动化_docker如何自动运行_02

三、浏览器中访问

通过7000端口

http://localhost:7000 来到jenkins配置界面

docker如何自动运行 docker运行自动化_jenkins_03


获取密码的方式有两种:

第一种是到:/var/jenkins_home/secrets/initialAdminPassword 这个目录下获取

第二种:通过查看容器日志命令:

docker logs py42jenkins

docker如何自动运行 docker运行自动化_docker_04


输入管理员密码,进入下一个页面:安装插件

docker如何自动运行 docker运行自动化_jenkins_05


下载一些依赖

docker如何自动运行 docker运行自动化_macos_06

下载完成之后,进入创建用户界面:

用户和密码: admin

docker如何自动运行 docker运行自动化_jenkins_07


docker如何自动运行 docker运行自动化_jenkins_08


docker如何自动运行 docker运行自动化_docker如何自动运行_09


点击开始使用jenkins

docker如何自动运行 docker运行自动化_jenkins_10

四、在jenkins上新建一个项目

1、点击新建item

docker如何自动运行 docker运行自动化_docker如何自动运行_11


2、

docker如何自动运行 docker运行自动化_docker如何自动运行_12


然后等待一会:

docker如何自动运行 docker运行自动化_docker_13


填写完git地址之后,会显示:无法链接

docker如何自动运行 docker运行自动化_jenkins_14


需要填写下gitee仓库的用户名和密码就好了。

点击添加:

docker如何自动运行 docker运行自动化_docker_15


选择刚添加的账号就好了。

docker如何自动运行 docker运行自动化_jenkins_16


这里可以选择,拉取的分支,我这是master分支

docker如何自动运行 docker运行自动化_docker如何自动运行_17


这里的构建步骤选择shell脚本

docker如何自动运行 docker运行自动化_3d_18


docker如何自动运行 docker运行自动化_docker_19


点击保存:

点击立即构建:

docker如何自动运行 docker运行自动化_docker如何自动运行_20


点击#1,进来,再点击控制台输出:

docker如何自动运行 docker运行自动化_docker如何自动运行_21


可以看到控制台输出:

docker如何自动运行 docker运行自动化_docker如何自动运行_22

Started by user admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/py42
The recommended git tool is: NONE
using credential 7f32f8c0-75ad-4bfe-9789-cc1b0073b62f
Cloning the remote Git repository
Cloning repository https://gitee.com/wcystart/py42-api.git
 > git init /var/jenkins_home/workspace/py42 # timeout=10
Fetching upstream changes from https://gitee.com/wcystart/py42-api.git
 > git --version # timeout=10
 > git --version # 'git version 2.34.1'
using GIT_ASKPASS to set credentials 
 > git fetch --tags --force --progress -- https://gitee.com/wcystart/py42-api.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://gitee.com/wcystart/py42-api.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision b3c4e866e6077a525da4e54d066ba94f45df32bf (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f b3c4e866e6077a525da4e54d066ba94f45df32bf # timeout=10
Commit message: "add dockerfile"
First time build. Skipping changelog.
[py42] $ /bin/sh -xe /tmp/jenkins9733523508438114569.sh
+ docker build -t py42test .
#1 [internal] load build definition from Dockerfile
#1 sha256:32a3869b9923db66deedbecc0323fd50db179465780ca289878f26b34dd15689
#1 transferring dockerfile:
#1 transferring dockerfile: 188B 0.0s done
#1 DONE 0.1s

#2 [internal] load .dockerignore
#2 sha256:24abdf63b561cdf1a4b5769c55d9b2145adb6e75598d530d5756e2b2cfaf46e0
#2 transferring context: 2B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/python:3-alpine
#3 sha256:5cfd63f819a536533ccd33ab472b68bc5eaf5534980201cfb2f10a631818880f
#3 DONE 16.6s

#4 [1/4] FROM docker.io/library/python:3-alpine@sha256:dce56d40d885d2c8847aa2a278a29d50450c8e3d10f9d7ffeb2f38dcc1eb0ea4
#4 sha256:872d85394d85121345306f306f434d13ce6d5c37218196bd7dffdfa2b1185487
#4 resolve docker.io/library/python:3-alpine@sha256:dce56d40d885d2c8847aa2a278a29d50450c8e3d10f9d7ffeb2f38dcc1eb0ea4 done
#4 sha256:eb5bc7d10d52904953df1e5f3460f4f4154b7911603a3889a35ffcb9b6e75b81 7.44kB / 7.44kB done
#4 sha256:dce56d40d885d2c8847aa2a278a29d50450c8e3d10f9d7ffeb2f38dcc1eb0ea4 1.65kB / 1.65kB done
#4 sha256:affe0faa14e7553fc570beec3864e74b5e36f8c19b2bb49ae8ba79c0e9e7236e 1.37kB / 1.37kB done
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 0B / 11.69MB 0.2s
#4 sha256:c7f1ca1171d975af881f16ba8005f599918648448f8d9b0be5436dd8ba3bf1f0 0B / 231B 0.2s
#4 sha256:07a400e93df3fcc09e5f874878c049b15515236f55fbf76013c0779a7cc4a301 0B / 678.30kB 0.2s
#4 ...

#6 [internal] load build context
#6 sha256:aee7f93c830ef46880233dc3124788aef038bbe8de3409b585cd21fb82a805ae
#6 transferring context: 224.28kB 0.4s done
#6 DONE 0.4s

#4 [1/4] FROM docker.io/library/python:3-alpine@sha256:dce56d40d885d2c8847aa2a278a29d50450c8e3d10f9d7ffeb2f38dcc1eb0ea4
#4 sha256:872d85394d85121345306f306f434d13ce6d5c37218196bd7dffdfa2b1185487
#4 extracting sha256:07a400e93df3fcc09e5f874878c049b15515236f55fbf76013c0779a7cc4a301
#4 sha256:07a400e93df3fcc09e5f874878c049b15515236f55fbf76013c0779a7cc4a301 678.30kB / 678.30kB 0.7s done
#4 sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678 0B / 2.35MB 0.8s
#4 sha256:c7f1ca1171d975af881f16ba8005f599918648448f8d9b0be5436dd8ba3bf1f0 231B / 231B 0.8s done
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 1.05MB / 11.69MB 1.3s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 2.10MB / 11.69MB 1.7s
#4 sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678 851.97kB / 2.35MB 1.8s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 3.15MB / 11.69MB 1.9s
#4 sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678 1.05MB / 2.35MB 1.9s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 4.19MB / 11.69MB 2.2s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 5.11MB / 11.69MB 2.5s
#4 sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678 2.10MB / 2.35MB 2.5s
#4 extracting sha256:07a400e93df3fcc09e5f874878c049b15515236f55fbf76013c0779a7cc4a301 1.8s done
#4 sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678 2.35MB / 2.35MB 2.6s done
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 6.29MB / 11.69MB 2.9s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 7.34MB / 11.69MB 3.3s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 8.39MB / 11.69MB 3.4s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 9.44MB / 11.69MB 3.7s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 10.49MB / 11.69MB 4.0s
#4 sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 11.69MB / 11.69MB 4.3s done
#4 extracting sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7
#4 extracting sha256:d8c681bc2183ac2de0b5e117ed74647ee5dfbdbf8a3730ac81c54f3b3db989f7 1.9s done
#4 extracting sha256:c7f1ca1171d975af881f16ba8005f599918648448f8d9b0be5436dd8ba3bf1f0 0.0s done
#4 extracting sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678
#4 extracting sha256:dc9256fcea2e541f17f0ed7d4dae23c03fb27a5ab8e7611e34f5a5e4d96d6678 0.8s done
#4 DONE 7.9s

#5 [2/4] WORKDIR /app
#5 sha256:3950f014d35b49bf98a5879949830e8ec2d1550416ecdeee4c8c8fb4b7e4f7ae
#5 DONE 0.2s

#7 [3/4] ADD . /app
#7 sha256:4d081bbef88a4767a1dee00da10f33fa7bb8acd5399a584e0cb475395c6d1af5
#7 DONE 0.1s

#8 [4/4] RUN pip3 install -r requirements.txt -i https://pypi.douban.com/simple
#8 sha256:3d2f5aa66e7e509a9fce445f284d51b0a0beb154d76899c239d670f9fdb357d6
#8 4.862 Looking in indexes: https://pypi.douban.com/simple
#8 5.391 Collecting BeautifulReport
#8 5.446   Downloading https://pypi.doubanio.com/packages/b3/2c/b944e50172b72ce94f2b495a761e66ee3c4d517cee226469727749224f13/BeautifulReport-0.1.3.tar.gz (31 kB)
#8 6.567 Collecting ddt
#8 6.622   Downloading https://pypi.doubanio.com/packages/16/51/45518c5a2f3ffc7fa782db8e34d4c6a619765fe5d19809030c771da44cfd/ddt-1.4.4-py2.py3-none-any.whl (6.3 kB)
#8 6.996 Collecting Faker
#8 7.031   Downloading https://pypi.doubanio.com/packages/33/58/02649e1b42ef4188c4a5aaec2d2ea424f50576f481646f0685e27021deb1/Faker-11.3.0-py3-none-any.whl (1.2 MB)
#8 7.399 Collecting PyMySQL
#8 7.419   Downloading https://pypi.doubanio.com/packages/4f/52/a115fe175028b058df353c5a3d5290b71514a83f67078a6482cff24d6137/PyMySQL-1.0.2-py3-none-any.whl (43 kB)
#8 7.683 Collecting pytest
#8 7.698   Downloading https://pypi.doubanio.com/packages/40/76/86f886e750b81a4357b6ed606b2bcf0ce6d6c27ad3c09ebf63ed674fc86e/pytest-6.2.5-py3-none-any.whl (280 kB)
#8 8.087 Collecting PyYAML
#8 8.140   Downloading https://pypi.doubanio.com/packages/36/2b/61d51a2c4f25ef062ae3f74576b01638bebad5e045f747ff12643df63844/PyYAML-6.0.tar.gz (124 kB)
#8 8.844   Installing build dependencies: started
#8 21.95   Installing build dependencies: finished with status 'done'
#8 21.96   Getting requirements to build wheel: started
#8 24.04   Getting requirements to build wheel: finished with status 'done'
#8 24.04     Preparing wheel metadata: started
#8 24.65     Preparing wheel metadata: finished with status 'done'
#8 24.84 Collecting requests
#8 24.89   Downloading https://pypi.doubanio.com/packages/2d/61/08076519c80041bc0ffa1a8af0cbd3bf3e2b62af10435d269a9d0f40564d/requests-2.27.1-py2.py3-none-any.whl (63 kB)
#8 25.02 Collecting rsa
#8 25.04   Downloading https://pypi.doubanio.com/packages/30/ab/8fd9e88e6fa5ec41afca995938bbefb72195278e0cfc5bd76a4f29b23fb2/rsa-4.8-py3-none-any.whl (39 kB)
#8 25.24 Collecting unittestreport
#8 25.41   Downloading https://pypi.doubanio.com/packages/1f/ca/4523ea890c37b80fbf6d52222ec9a133293f0cb62cf285c1296b3f275286/unittestreport-1.4.0-py3-none-any.whl (58 kB)
#8 25.51 Collecting jsonpath
#8 25.53   Downloading https://pypi.doubanio.com/packages/5f/c0/b54189dfe62f1a93ba294ab53508a81d440fc63adff253db369f557a996a/jsonpath-0.82.tar.gz (9.6 kB)
#8 26.24 Collecting openpyxl
#8 26.26   Downloading https://pypi.doubanio.com/packages/1c/a6/8ce4d2ef2c29be3235c08bb00e0b81e29d38ebc47d82b17af681bf662b74/openpyxl-3.0.9-py2.py3-none-any.whl (242 kB)
#8 26.50 Collecting text-unidecode==1.3
#8 26.52   Downloading https://pypi.doubanio.com/packages/a6/a5/c0b6468d3824fe3fde30dbb5e1f687b291608f9473681bbf7dabbf5a87d7/text_unidecode-1.3-py2.py3-none-any.whl (78 kB)
#8 26.90 Collecting python-dateutil>=2.4
#8 26.94   Downloading https://pypi.doubanio.com/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
#8 27.20 Collecting attrs>=19.2.0
#8 27.23   Downloading https://pypi.doubanio.com/packages/be/be/7abce643bfdf8ca01c48afa2ddf8308c2308b0c3b239a44e57d020afa0ef/attrs-21.4.0-py2.py3-none-any.whl (60 kB)
#8 27.32 Collecting iniconfig
#8 27.35   Downloading https://pypi.doubanio.com/packages/9b/dd/b3c12c6d707058fa947864b67f0c4e0c39ef8610988d7baea9578f3c48f3/iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB)
#8 27.46 Collecting pluggy<2.0,>=0.12
#8 27.48   Downloading https://pypi.doubanio.com/packages/9e/01/f38e2ff29715251cf25532b9082a1589ab7e4f571ced434f98d0139336dc/pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
#8 27.61 Collecting packaging
#8 27.64   Downloading https://pypi.doubanio.com/packages/05/8e/8de486cbd03baba4deef4142bd643a3e7bbe954a784dc1bb17142572d127/packaging-21.3-py3-none-any.whl (40 kB)
#8 27.71 Collecting toml
#8 27.74   Downloading https://pypi.doubanio.com/packages/44/6f/7120676b6d73228c96e17f1f794d8ab046fc910d781c8d151120c3f1569e/toml-0.10.2-py2.py3-none-any.whl (16 kB)
#8 27.93 Collecting py>=1.8.2
#8 27.97   Downloading https://pypi.doubanio.com/packages/f6/f0/10642828a8dfb741e5f3fbaac830550a518a775c7fff6f04a007259b0548/py-1.11.0-py2.py3-none-any.whl (98 kB)
#8 28.31 Collecting urllib3<1.27,>=1.21.1
#8 28.34   Downloading https://pypi.doubanio.com/packages/4e/b8/f5a25b22e803f0578e668daa33ba3701bb37858ec80e08a150bd7d2cf1b1/urllib3-1.26.8-py2.py3-none-any.whl (138 kB)
#8 28.54 Collecting certifi>=2017.4.17
#8 28.57   Downloading https://pypi.doubanio.com/packages/37/45/946c02767aabb873146011e665728b680884cd8fe70dde973c640e45b775/certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
#8 28.79 Collecting charset-normalizer~=2.0.0
#8 28.81   Downloading https://pypi.doubanio.com/packages/84/3e/1037abe6498e65d645ce7a22d3402605d49a3b2c7f20c3abb027760da4f0/charset_normalizer-2.0.10-py3-none-any.whl (39 kB)
#8 28.91 Collecting idna<4,>=2.5
#8 28.93   Downloading https://pypi.doubanio.com/packages/04/a2/d918dcd22354d8958fe113e1a3630137e0fc8b44859ade3063982eacd2a4/idna-3.3-py3-none-any.whl (61 kB)
#8 29.21 Collecting pyasn1>=0.1.3
#8 29.26   Downloading https://pypi.doubanio.com/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
#8 29.31 Collecting PyYAML
#8 29.34   Downloading https://pypi.doubanio.com/packages/64/c2/b80047c7ac2478f9501676c988a5411ed5572f35d1beff9cae07d321512c/PyYAML-5.3.1.tar.gz (269 kB)
#8 30.42 Collecting Jinja2==3.0.3
#8 30.46   Downloading https://pypi.doubanio.com/packages/20/9a/e5d9ec41927401e41aea8af6d16e78b5e612bca4699d417f646a9610a076/Jinja2-3.0.3-py3-none-any.whl (133 kB)
#8 30.54 Collecting requests
#8 30.57   Downloading https://pypi.doubanio.com/packages/45/1e/0c169c6a5381e241ba7404532c16a21d86ab872c9bed8bdcd4c423954103/requests-2.24.0-py2.py3-none-any.whl (61 kB)
#8 30.63 Collecting idna<4,>=2.5
#8 30.65   Downloading https://pypi.doubanio.com/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58 kB)
#8 30.70 Collecting urllib3<1.27,>=1.21.1
#8 30.75   Downloading https://pypi.doubanio.com/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11-py2.py3-none-any.whl (127 kB)
#8 30.85 Collecting chardet<4,>=3.0.2
#8 30.87   Downloading https://pypi.doubanio.com/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133 kB)
#8 31.24 Collecting MarkupSafe>=2.0
#8 31.27   Downloading https://pypi.doubanio.com/packages/04/69/c31e837e4bb5532b02d297152464b2cb8a0edeb9bef762c015e9b4e95e16/MarkupSafe-2.0.1-cp310-cp310-musllinux_1_1_x86_64.whl (30 kB)
#8 31.36 Collecting et-xmlfile
#8 31.38   Downloading https://pypi.doubanio.com/packages/96/c2/3dd434b0108730014f1b96fd286040dc3bcb70066346f7e01ec2ac95865f/et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
#8 31.64 Collecting six>=1.5
#8 31.67   Downloading https://pypi.doubanio.com/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
#8 31.94 Collecting pyparsing!=3.0.5,>=2.0.2
#8 31.97   Downloading https://pypi.doubanio.com/packages/80/c1/23fd82ad3121656b585351aba6c19761926bb0db2ebed9e4ff09a43a3fcc/pyparsing-3.0.7-py3-none-any.whl (98 kB)
#8 32.04 Building wheels for collected packages: BeautifulReport, PyYAML, jsonpath
#8 32.04   Building wheel for BeautifulReport (setup.py): started
#8 32.70   Building wheel for BeautifulReport (setup.py): finished with status 'done'
#8 32.70   Created wheel for BeautifulReport: filename=BeautifulReport-0.1.3-py3-none-any.whl size=33101 sha256=80c327569e1801884ebbc292376d949364e7429805076c14dd176911a3df3d6b
#8 32.70   Stored in directory: /root/.cache/pip/wheels/c7/37/fa/bfe232474a845922df6d2cc3eedcf298a7ed6f99f705c8cf81
#8 32.71   Building wheel for PyYAML (setup.py): started
#8 33.62   Building wheel for PyYAML (setup.py): finished with status 'done'
#8 33.62   Created wheel for PyYAML: filename=PyYAML-5.3.1-cp310-cp310-linux_x86_64.whl size=44635 sha256=18c89b2cba0b5b138522fdf36c812707eae6de7242ee9a9c526f17a63ff4701a
#8 33.63   Stored in directory: /root/.cache/pip/wheels/31/da/33/e45f5aa9bbb53552b384354ee01ed8c96a0ac3cf2ec053535d
#8 33.64   Building wheel for jsonpath (setup.py): started
#8 34.70   Building wheel for jsonpath (setup.py): finished with status 'done'
#8 34.70   Created wheel for jsonpath: filename=jsonpath-0.82-py3-none-any.whl size=5612 sha256=a3966420e4054c87bcd805e93da0e6107f6781e247fa0ac7757fa5f7d926a78e
#8 34.71   Stored in directory: /root/.cache/pip/wheels/fe/7b/6b/3891918a1a9627043dc05b5c045772576acff162330cbf0d99
#8 34.72 Successfully built BeautifulReport PyYAML jsonpath
#8 35.13 Installing collected packages: urllib3, six, pyparsing, MarkupSafe, idna, chardet, certifi, toml, text-unidecode, requests, PyYAML, python-dateutil, pyasn1, py, pluggy, packaging, Jinja2, iniconfig, et-xmlfile, attrs, unittestreport, rsa, pytest, PyMySQL, openpyxl, jsonpath, Faker, ddt, BeautifulReport
#8 42.17 Successfully installed BeautifulReport-0.1.3 Faker-11.3.0 Jinja2-3.0.3 MarkupSafe-2.0.1 PyMySQL-1.0.2 PyYAML-5.3.1 attrs-21.4.0 certifi-2021.10.8 chardet-3.0.4 ddt-1.4.4 et-xmlfile-1.1.0 idna-2.10 iniconfig-1.1.1 jsonpath-0.82 openpyxl-3.0.9 packaging-21.3 pluggy-1.0.0 py-1.11.0 pyasn1-0.4.8 pyparsing-3.0.7 pytest-6.2.5 python-dateutil-2.8.2 requests-2.24.0 rsa-4.8 six-1.16.0 text-unidecode-1.3 toml-0.10.2 unittestreport-1.4.0 urllib3-1.25.11
#8 42.17 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#8 42.20 WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
#8 42.20 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
#8 DONE 43.3s

#9 exporting to image
#9 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#9 exporting layers
#9 exporting layers 1.2s done
#9 writing image sha256:e97396f4b4caa88cf5a198d5be2e262c7159c5921553b893245fbac068e5bcc0 done
#9 naming to docker.io/library/py42test done
#9 DONE 1.2s
+ docker run --rm py42test
【2022-01-24 10:15:00,395-test_log-INFO-/app/tools/handle_log.py-42】: test
【2022-01-24 10:15:01,047-test_log-INFO-/app/tools/handle_excel.py-16】: 用例数据文件的名称/app/test_data/testCase.xlsx
【2022-01-24 10:15:01,050-test_log-INFO-/app/tools/handle_excel.py-17】: sheet的名称all
【2022-01-24 10:15:01,107-test_log-INFO-/app/tools/handle_excel.py-28】: 从用例数据文件中获取到的测试数据
[{'id': 1, 'title': '登陆成功', 'method': 'post', 'url': 'http://api.lemonban.com/futureloan/member/login', 'data': '{"mobile_phone":"18100000009","pwd":"Aa123456"}', 'replace_sql': None, 'expected_data': '{"code": 0, "msg": "OK"}', 'extract_data': '{"token":"$..token","member_id":"$..id"}', 'setup_sql': None, 'check_db': None, 'check_db_dynamic': None}, {'id': 2, 'title': '充值成功', 'method': 'post', 'url': 'http://api.lemonban.com/futureloan/member/recharge', 'data': '{"member_id":"#member_id#","amount":100}', 'replace_sql': None, 'expected_data': '{"code": 0, "msg": "OK"}', 'extract_data': None, 'setup_sql': None, 'check_db': None, 'check_db_dynamic': None}, {'id': 3, 'title': '投资成功', 'method': 'post', 'url': 'http://api.lemonban.com/futureloan/member/invest', 'data': '{"loan_id":"#loan_id#","member_id":"#member_id#","amount":100}', 'replace_sql': '["SELECT id AS loan_id FROM loan WHERE STATUS = 2 ORDER BY ID DESC LIMIT 1"]', 'expected_data': '{"code": 0, "msg": "OK"}', 'extract_data': None, 'setup_sql': '["update loan set status=2  where status !=2  order by id desc limit 1"]', 'check_db': None, 'check_db_dynamic': 1}, {'id': 4, 'title': '审核成功', 'method': 'patch', 'url': 'http://api.lemonban.com/futureloan/loan/audit', 'data': '{"loan_id":"#loan_id#", "approved_or_not":True }', 'replace_sql': '["SELECT id AS loan_id FROM loan WHERE STATUS=1 ORDER BY id DESC LIMIT 1"]', 'expected_data': '{"code": 0, "msg": "OK"}', 'extract_data': None, 'setup_sql': '["update loan set status=1  where status !=1 order by id desc limit 1"]', 'check_db': None, 'check_db_dynamic': None}, {'id': 5, 'title': '新增项目成功月标36个月', 'method': 'post', 'url': 'http://api.lemonban.com/futureloan/loan/add', 'data': '{"member_id":"#member_id#","title":\'月标36个月\',"amount":1000,"loan_rate":10.0,"loan_term":36,"loan_date_type":1,"bidding_days":10}', 'replace_sql': None, 'expected_data': '{"code": 0, "msg": "OK"}', 'extract_data': '{"loan_id":"$..id"}', 'setup_sql': None, 'check_db': '{"actual_data":"select amount from loan where id = #loan_id#","expect_data":1000}', 'check_db_dynamic': None}, {'id': 6, 'title': '获取用户信息', 'method': 'get', 'url': '/member/#member_id#/info', 'data': None, 'replace_sql': None, 'expected_data': '{"code": 0, "msg": "OK"}', 'extract_data': None, 'setup_sql': None, 'check_db': None, 'check_db_dynamic': None}]
【2022-01-24 10:15:01,270-test_log-INFO-/app/test_case/test_all.py-46】: 当前执行的用例数据

【2022-01-24 10:15:01,273-test_log-INFO-/app/test_case/test_all.py-53】: setup_sql字段为空,不需要前置Sql语句
【2022-01-24 10:15:01,274-test_log-INFO-/app/tools/handle_replace.py-82】: 参数替换传进来的数据:
{"mobile_phone":"18100000009","pwd":"Aa123456"}
【2022-01-24 10:15:01,275-test_log-INFO-/app/tools/handle_replace.py-83】: 用例数据字段replace_sql传进来的数据:
None
【2022-01-24 10:15:01,275-test_log-INFO-/app/tools/handle_replace.py-86】: data数据不为空,开始执行参数替换
【2022-01-24 10:15:01,276-test_log-INFO-/app/tools/handle_replace.py-88】: 需要替换的参数字段为:
[]
【2022-01-24 10:15:01,277-test_log-INFO-/app/tools/handle_replace.py-103】: 不需要做参数替换,返回转换成dict类型的数据
【2022-01-24 10:15:01,406-test_log-INFO-/app/test_case/test_all.py-46】: 当前执行的用例数据

【2022-01-24 10:15:01,407-test_log-INFO-/app/test_case/test_all.py-53】: setup_sql字段为空,不需要前置Sql语句
【2022-01-24 10:15:01,410-test_log-INFO-/app/tools/handle_replace.py-82】: 参数替换传进来的数据:
{"member_id":"#member_id#","amount":100}
【2022-01-24 10:15:01,411-test_log-INFO-/app/tools/handle_replace.py-83】: 用例数据字段replace_sql传进来的数据:
None
【2022-01-24 10:15:01,413-test_log-INFO-/app/tools/handle_replace.py-86】: data数据不为空,开始执行参数替换
【2022-01-24 10:15:01,415-test_log-INFO-/app/tools/handle_replace.py-88】: 需要替换的参数字段为:
['member_id']
【2022-01-24 10:15:01,415-test_log-INFO-/app/tools/handle_replace.py-90】: key_list不为空,开始进行参数替换
【2022-01-24 10:15:01,416-test_log-INFO-/app/tools/handle_replace.py-91】: 根据数据的来源去获取数据,然后设置为类属性
【2022-01-24 10:15:01,420-test_log-INFO-/app/tools/handle_replace.py-55】: 替换参数的key=member_id
【2022-01-24 10:15:01,421-test_log-INFO-/app/tools/handle_replace.py-71】: 该替换方式暂不支持,请检查替换规则
【2022-01-24 10:15:01,421-test_log-INFO-/app/tools/handle_replace.py-93】: 根据数据来源获取数据,并设置为类属性已完成
【2022-01-24 10:15:01,422-test_log-INFO-/app/tools/handle_replace.py-95】: 开始从类属性中获取参数,进行参数替换
【2022-01-24 10:15:01,423-test_log-INFO-/app/tools/handle_replace.py-97】: 替换参数的名称key=member_id
【2022-01-24 10:15:01,424-test_log-INFO-/app/tools/handle_replace.py-98】: 替换参数的value值=770683
【2022-01-24 10:15:01,424-test_log-INFO-/app/tools/handle_replace.py-100】: 参数替换完成,替换后的参数为:
{"member_id":"770683","amount":100}转换成dict类型的数据并返回
【2022-01-24 10:15:01,583-test_log-INFO-/app/test_case/test_all.py-46】: 当前执行的用例数据

【2022-01-24 10:15:01,584-test_log-INFO-/app/test_case/test_all.py-49】: 开始执行前置sql语句
【2022-01-24 10:15:01,585-test_log-INFO-/app/tools/handle_db.py-42】: 执行的sql语句是:
update loan set status=2  where status !=2  order by id desc limit 1
【2022-01-24 10:15:01,646-test_log-INFO-/app/tools/handle_replace.py-82】: 参数替换传进来的数据:
{"loan_id":"#loan_id#","member_id":"#member_id#","amount":100}
【2022-01-24 10:15:01,647-test_log-INFO-/app/tools/handle_replace.py-83】: 用例数据字段replace_sql传进来的数据:
["SELECT id AS loan_id FROM loan WHERE STATUS = 2 ORDER BY ID DESC LIMIT 1"]
【2022-01-24 10:15:01,647-test_log-INFO-/app/tools/handle_replace.py-86】: data数据不为空,开始执行参数替换
【2022-01-24 10:15:01,648-test_log-INFO-/app/tools/handle_replace.py-88】: 需要替换的参数字段为:
['loan_id', 'member_id']
【2022-01-24 10:15:01,650-test_log-INFO-/app/tools/handle_replace.py-90】: key_list不为空,开始进行参数替换
【2022-01-24 10:15:01,650-test_log-INFO-/app/tools/handle_replace.py-91】: 根据数据的来源去获取数据,然后设置为类属性
【2022-01-24 10:15:01,654-test_log-INFO-/app/tools/handle_replace.py-48】: replace_sql不为空,需要执行sql语句获取参数并设置为类属性
【2022-01-24 10:15:01,655-test_log-INFO-/app/tools/handle_replace.py-27】: 执行sql语句:
SELECT id AS loan_id FROM loan WHERE STATUS = 2 ORDER BY ID DESC LIMIT 1
【2022-01-24 10:15:01,657-test_log-INFO-/app/tools/handle_db.py-42】: 执行的sql语句是:
SELECT id AS loan_id FROM loan WHERE STATUS = 2 ORDER BY ID DESC LIMIT 1
【2022-01-24 10:15:01,705-test_log-INFO-/app/tools/handle_replace.py-29】: 执行sql语句拿到的结果:
[{'loan_id': 51662}]
【2022-01-24 10:15:01,706-test_log-INFO-/app/tools/handle_replace.py-32】: 遍历sql执行结果,并设置为类属性
【2022-01-24 10:15:01,708-test_log-INFO-/app/tools/handle_replace.py-21】: 类属性的key=loan_id,value=51662
【2022-01-24 10:15:01,709-test_log-INFO-/app/tools/handle_replace.py-55】: 替换参数的key=loan_id
【2022-01-24 10:15:01,710-test_log-INFO-/app/tools/handle_replace.py-71】: 该替换方式暂不支持,请检查替换规则
【2022-01-24 10:15:01,710-test_log-INFO-/app/tools/handle_replace.py-55】: 替换参数的key=member_id
【2022-01-24 10:15:01,712-test_log-INFO-/app/tools/handle_replace.py-71】: 该替换方式暂不支持,请检查替换规则
【2022-01-24 10:15:01,714-test_log-INFO-/app/tools/handle_replace.py-93】: 根据数据来源获取数据,并设置为类属性已完成
【2022-01-24 10:15:01,714-test_log-INFO-/app/tools/handle_replace.py-95】: 开始从类属性中获取参数,进行参数替换
【2022-01-24 10:15:01,715-test_log-INFO-/app/tools/handle_replace.py-97】: 替换参数的名称key=loan_id
【2022-01-24 10:15:01,715-test_log-INFO-/app/tools/handle_replace.py-98】: 替换参数的value值=51662
【2022-01-24 10:15:01,717-test_log-INFO-/app/tools/handle_replace.py-100】: 参数替换完成,替换后的参数为:
{"loan_id":"51662","member_id":"#member_id#","amount":100}转换成dict类型的数据并返回
【2022-01-24 10:15:01,717-test_log-INFO-/app/tools/handle_replace.py-97】: 替换参数的名称key=member_id
【2022-01-24 10:15:01,719-test_log-INFO-/app/tools/handle_replace.py-98】: 替换参数的value值=770683
【2022-01-24 10:15:01,719-test_log-INFO-/app/tools/handle_replace.py-100】: 参数替换完成,替换后的参数为:
{"loan_id":"51662","member_id":"770683","amount":100}转换成dict类型的数据并返回
【2022-01-24 10:15:01,946-test_log-INFO-/app/test_case/test_all.py-46】: 当前执行的用例数据

【2022-01-24 10:15:01,947-test_log-INFO-/app/test_case/test_all.py-49】: 开始执行前置sql语句
【2022-01-24 10:15:01,949-test_log-INFO-/app/tools/handle_db.py-42】: 执行的sql语句是:
update loan set status=1  where status !=1 order by id desc limit 1
【2022-01-24 10:15:02,001-test_log-INFO-/app/tools/handle_replace.py-82】: 参数替换传进来的数据:
{"loan_id":"#loan_id#", "approved_or_not":True }
【2022-01-24 10:15:02,001-test_log-INFO-/app/tools/handle_replace.py-83】: 用例数据字段replace_sql传进来的数据:
["SELECT id AS loan_id FROM loan WHERE STATUS=1 ORDER BY id DESC LIMIT 1"]
【2022-01-24 10:15:02,002-test_log-INFO-/app/tools/handle_replace.py-86】: data数据不为空,开始执行参数替换
【2022-01-24 10:15:02,003-test_log-INFO-/app/tools/handle_replace.py-88】: 需要替换的参数字段为:
['loan_id']
【2022-01-24 10:15:02,004-test_log-INFO-/app/tools/handle_replace.py-90】: key_list不为空,开始进行参数替换
【2022-01-24 10:15:02,004-test_log-INFO-/app/tools/handle_replace.py-91】: 根据数据的来源去获取数据,然后设置为类属性
【2022-01-24 10:15:02,004-test_log-INFO-/app/tools/handle_replace.py-48】: replace_sql不为空,需要执行sql语句获取参数并设置为类属性
【2022-01-24 10:15:02,005-test_log-INFO-/app/tools/handle_replace.py-27】: 执行sql语句:
SELECT id AS loan_id FROM loan WHERE STATUS=1 ORDER BY id DESC LIMIT 1
【2022-01-24 10:15:02,006-test_log-INFO-/app/tools/handle_db.py-42】: 执行的sql语句是:
SELECT id AS loan_id FROM loan WHERE STATUS=1 ORDER BY id DESC LIMIT 1
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-29】: 执行sql语句拿到的结果:
[{'loan_id': 51662}]
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-32】: 遍历sql执行结果,并设置为类属性
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-21】: 类属性的key=loan_id,value=51662
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-55】: 替换参数的key=loan_id
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-71】: 该替换方式暂不支持,请检查替换规则
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-93】: 根据数据来源获取数据,并设置为类属性已完成
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-95】: 开始从类属性中获取参数,进行参数替换
【2022-01-24 10:15:02,056-test_log-INFO-/app/tools/handle_replace.py-97】: 替换参数的名称key=loan_id
【2022-01-24 10:15:02,057-test_log-INFO-/app/tools/handle_replace.py-98】: 替换参数的value值=51662
【2022-01-24 10:15:02,057-test_log-INFO-/app/tools/handle_replace.py-100】: 参数替换完成,替换后的参数为:
{"loan_id":"51662", "approved_or_not":True }转换成dict类型的数据并返回
【2022-01-24 10:15:02,161-test_log-INFO-/app/test_case/test_all.py-46】: 当前执行的用例数据

【2022-01-24 10:15:02,162-test_log-INFO-/app/test_case/test_all.py-53】: setup_sql字段为空,不需要前置Sql语句
【2022-01-24 10:15:02,162-test_log-INFO-/app/tools/handle_replace.py-82】: 参数替换传进来的数据:
{"member_id":"#member_id#","title":'月标36个月',"amount":1000,"loan_rate":10.0,"loan_term":36,"loan_date_type":1,"bidding_days":10}
【2022-01-24 10:15:02,163-test_log-INFO-/app/tools/handle_replace.py-83】: 用例数据字段replace_sql传进来的数据:
None
【2022-01-24 10:15:02,163-test_log-INFO-/app/tools/handle_replace.py-86】: data数据不为空,开始执行参数替换
【2022-01-24 10:15:02,164-test_log-INFO-/app/tools/handle_replace.py-88】: 需要替换的参数字段为:
['member_id']
【2022-01-24 10:15:02,166-test_log-INFO-/app/tools/handle_replace.py-90】: key_list不为空,开始进行参数替换
【2022-01-24 10:15:02,167-test_log-INFO-/app/tools/handle_replace.py-91】: 根据数据的来源去获取数据,然后设置为类属性
【2022-01-24 10:15:02,170-test_log-INFO-/app/tools/handle_replace.py-55】: 替换参数的key=member_id
【2022-01-24 10:15:02,170-test_log-INFO-/app/tools/handle_replace.py-71】: 该替换方式暂不支持,请检查替换规则
【2022-01-24 10:15:02,171-test_log-INFO-/app/tools/handle_replace.py-93】: 根据数据来源获取数据,并设置为类属性已完成
【2022-01-24 10:15:02,171-test_log-INFO-/app/tools/handle_replace.py-95】: 开始从类属性中获取参数,进行参数替换
【2022-01-24 10:15:02,172-test_log-INFO-/app/tools/handle_replace.py-97】: 替换参数的名称key=member_id
【2022-01-24 10:15:02,172-test_log-INFO-/app/tools/handle_replace.py-98】: 替换参数的value值=770683
【2022-01-24 10:15:02,173-test_log-INFO-/app/tools/handle_replace.py-100】: 参数替换完成,替换后的参数为:
{"member_id":"770683","title":'月标36个月',"amount":1000,"loan_rate":10.0,"loan_term":36,"loan_date_type":1,"bidding_days":10}转换成dict类型的数据并返回
【2022-01-24 10:15:02,331-test_log-INFO-/app/test_case/test_all.py-46】: 当前执行的用例数据

【2022-01-24 10:15:02,332-test_log-INFO-/app/test_case/test_all.py-53】: setup_sql字段为空,不需要前置Sql语句
【2022-01-24 10:15:02,335-test_log-INFO-/app/tools/handle_replace.py-82】: 参数替换传进来的数据:
None
【2022-01-24 10:15:02,338-test_log-INFO-/app/tools/handle_replace.py-83】: 用例数据字段replace_sql传进来的数据:
None
【2022-01-24 10:15:02,339-test_log-INFO-/app/tools/handle_replace.py-107】: data参数为空,无需替换参数,返回空的{}
token不存在,无需鉴权
不需要替换请求地址
非V3鉴权方式,不需要处理
['eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc3MDY4MywiZXhwIjoxNjQzMDE5NjAxfQ._qaV51oOi12Nke0DNhX8ck4XzNrFFe5qvLULRYBM-ItxE6FJHCCC4I3aoRRdIn1KT4GGOlHSYDnyFcpmqE484g']
[770683]
eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc3MDY4MywiZXhwIjoxNjQzMDE5NjAxfQ._qaV51oOi12Nke0DNhX8ck4XzNrFFe5qvLULRYBM-ItxE6FJHCCC4I3aoRRdIn1KT4GGOlHSYDnyFcpmqE484g 770683
不需要做数据库断言
test_all_001 (test_all.TestAll)执行——>【通过】
该替换方式还不支持
不需要替换请求地址
extract_data为空,无需从response中提取参数
不需要做数据库断言
test_all_002 (test_all.TestAll)执行——>【通过】
该替换方式还不支持
该替换方式还不支持
接口请求前的余额: 183000.0
不需要替换请求地址
extract_data为空,无需从response中提取参数
不需要做数据库断言
接口请求后的余额: 182900.0
test_all_003 (test_all.TestAll)执行——>【通过】
该替换方式还不支持
不需要替换请求地址
extract_data为空,无需从response中提取参数
不需要做数据库断言
test_all_004 (test_all.TestAll)执行——>【通过】
该替换方式还不支持
不需要替换请求地址
[51663]
eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc3MDY4MywiZXhwIjoxNjQzMDE5NjAxfQ._qaV51oOi12Nke0DNhX8ck4XzNrFFe5qvLULRYBM-ItxE6FJHCCC4I3aoRRdIn1KT4GGOlHSYDnyFcpmqE484g 770683
其他渠道拓展
select amount from loan where id = 51663========
select amount from loan where id = 51663--------------------
test_all_005 (test_all.TestAll)执行——>【通过】
extract_data为空,无需从response中提取参数
不需要做数据库断言
test_all_006 (test_all.TestAll)执行——>【通过】
所有用例执行完毕,正在生成测试报告中......
测试报告已经生成,报告路径为:/app/reports/report.html
Finished: SUCCESS

五、总结

jenkins容器 — 启动python容器 — 在python容器中执行自动化框架
构建过程:启动jenkins – 从远程仓库拉取代码 – 执行Dockerfile创建镜像 – 基于镜像启动容器跑自动化框架
1、启动jenkins与配置:
1、通过jenkins镜像启动一个jenkins容器
2、让jenkins可以执行宿主机的docker命令
-v /var/run/docker.sock:/var/run/docker.sock
-v /usr/bin/docker:/usr/bin/docker
3、将写好的Dockerfile、requirements.txt放到项目跟目录下,并上传到远程仓库中
4、配置jenkins构建操作命令
docker build -t py42test .
docker run --rm py42test

2、写Dockerfile文件构建python自动化环境镜像
FROM python:3-alpine
WORKDIR /app
ADD . /app
RUN pip3 install -r requirements.txt -i https://pypi.douban.com/simple
CMD [“python3”, “main.py”]
1、选择python镜像
2、切换工作目录
3、将jenkins拉下来的自动化代码添加到python容器的工作目录下
4、安装自动化环境需要的依赖包:
pip3 install freeze > requirements.txt
pip3 install -r python.txt -i https://pypi.douban.com/simple
5、设置容器运行后默认执行的命令
CMD [“python3”, “main.py”]

六、构建之后操作

docker如何自动运行 docker运行自动化_docker如何自动运行_23


安装html 插件

docker如何自动运行 docker运行自动化_docker如何自动运行_24


安装完重启jenkins

docker如何自动运行 docker运行自动化_macos_25


然后返回,在插件管理-已安装的插件中,就可以看到:

docker如何自动运行 docker运行自动化_jenkins_26


然后到配置-构建中,选择

docker如何自动运行 docker运行自动化_macos_27

docker如何自动运行 docker运行自动化_jenkins_28


然后在构建一次

docker如何自动运行 docker运行自动化_docker如何自动运行_29

在控制台日志中可以看到报告路径:

docker如何自动运行 docker运行自动化_macos_30


返回,在HTML Report中也可以看到详细报告

docker如何自动运行 docker运行自动化_docker_31

要想让测试报告,样式好看些,需要安装连个插件

docker如何自动运行 docker运行自动化_jenkins_32


docker如何自动运行 docker运行自动化_jenkins_33


docker如何自动运行 docker运行自动化_jenkins_34


添加脚本:System.setProperty(“hudson.model.DirectoryBrowserSupport.CSP”,"")

docker如何自动运行 docker运行自动化_jenkins_35


配置完,Html报告模板样式就好看了。