使用 Docker 搭建 Jenkins+python3+selenium\helium 项目

86 篇文章 8 订阅
23 篇文章 1 订阅

项目背景

用 python3 在本地写了一个项目,这个项目采用 helium+selenium+chrome 进行界面操作,代码放在 gitee 上托管。现在基本功能已经实现,想在 Linux 服务器上部署项目代码,实现自动构建。

  • 自动构建解决方案
    • 自动构建工具选型:jenkins
    • 中间件选型:docker
    • 代码运行环境:python3
    • 脚本运行工具:Chrome 浏览器、chromedriver、helium 库

Ⅰ、安装 docker

  • Linux 系统中安装 docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
  • docker 镜像国内加速

vim /etc/docker/daemon.json

{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"

]
}

# 重启服务
systemctl daemon-reload
systemctl restart docker

Ⅱ、安装 jenkins

  • 在 Linux 上任意路径,创建一个用于存放 Jenkins 文件的文件夹 "jenkins_py"
mkdir jenkins_py docker run -itd --name=jenkins_py -p 8989:8080 -p 50001:50000 -v $PWD:/var/jenkins_home --privileged=true -u root jenkins/jenkins 

出现输入密码页面时,获取密码:

  • 方法 1:
# 进入容器 docker exec -it -u root jenkins_py /bin/bash # 查看密码 cat /var/jenkins_home/secrets/initialAdminPassword 
  • 方法 2:
# 在当前宿主linux机器上,进入前面自己创建的jenkins_py文件夹 cd secrets cat initialAdminPassword

复制密码,填入 Web 页面的密码框,在选择安装插件页面,点击安装推荐插件,等待插件自动安装

2020092303.png

 

Ⅲ、容器安装必要软件

  • 进入容器

docker exec -it -u root jenkins_py /bin/bash

  • 安装更新

 

# 安装更新

apt-get update

# 查看系统
cat /etc/issue

# 此时容器默认的系统为 debain 9
# debain默认的软件源非常慢, 可以更换为国内阿里源

# 安装vim
apt-get install -y vim
apt-get install -y wget

# 更换源为阿里源
vim /etc/apt/sources.list

deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib

# 更新系统
apt-get update

# 安装libssl-dev
apt-get install libssl-dev

Ⅳ、配置 Web 自动化环境

  • 下载浏览器
    • 百度搜索"chrome for linux", 下载适合 debain 版本的 Chrome 浏览器
    • 把下载的文件上传到 Linux 机器
  • 下载浏览器驱动
    • 下载 Chrome 浏览器 Linux 对应版本的驱动
    • 把下载的文件上传到 Linux 机器
  • Jenkins 容器中,安装 Chrome 浏览器

# 把chrome文件,复制到上面创建的 jenkins_py 文件夹
cp google-chrome-stable_current_amd64_85.0.4183.102.deb ./jenkins_py/

# 进入容器
docker exec -it -u root jenkins_py /bin/bash

cd /var/jenkins_home
chmod +x google-chrome-stable_current_amd64_85.0.4183.102.deb
dpkg -i google-chrome-stable_current_amd64_85.0.4183.102.deb

  • 配置浏览器驱动
# 解压驱动zip包 unzip chromedriver_linux64.zip # 拷贝到 jenkins_py文件夹 cp chromedriver ./jenkins_py/ # 进入容器 docker exec -it -u root jenkins_py /bin/bash cd /var/jenkins_home chmod +x chromedriver mv chromedriver /usr/bin/ cp /usr/bin/chromedriver /usr/local/bin/

Ⅴ、安装 python3

  • 安装 python3
apt-get install -y python3 
  • 安装 pip
# 下载pip wget https://bootstrap.pypa.io/get-pip.py python3 get-pip.py pip3 install --upgrade --force-reinstall setuptools 
  • 修改系统默认 python 版本为 python3
cd /usr/bin ls -l python* # 查看上面的python3的版本 rm -rf python ln -s python3.5 pythonpython3.5 # 修改为上面对于的python3版本* 修改pip源 
  • 修改 pip 源
vim /etc/pip.conf [global] trusted-host = mirrors.aliyun.com index-url = http://mirrors.aliyun.com/pypi/simple/

Ⅵ、jenkins 配置任务

  • 创建任务

2020092306.png

 

  • 配置 Git

2020092307.png

 

2020092308.png

 

  • 配置构建触发器(可选)

2020092309.png

 

  • 配置构建脚本

2020092310.png

 

pip install -r requirements.txt 执行项目根目录中的 requirements.txt 文件,自动安装项目需要的库,这样就不用担心代码引入了新库,而 Jenkins 中不存在,导致报错了。

cp ../../tools.ini $PWD/src/conf 拷贝配置文件。因为 gitee 托管项目时,没有提交 ini 带私密信息的配置文件

python main.py 用 python 执行项目

  • 构建

2020092311.png

 

注意: 用 docker+jenkins 来运行 selenium 的项目,是无图形界面的,所以项目中,浏览器必须采用无头模式

# 参考代码 self.options = ChromeOptions() self.options.add_argument('--headless') self.options.add_argument('--no-sandbox') self.options.add_argument('--disable-gpu') self.options.add_argument('--disable-dev-shm-usage') start_chrome(headless=True,options=self.options) 

好了,项目构建成功。整个构建步骤、方法,你学会了吗?

本文由柠檬班Allen老师原创,转载需注明出处!

想了解更多咨询的同学扫描下方二维码,可以加Q群领取学习资料:753665853  备注:CSDN  



 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值