Jenkins集成自动化测试
Jenkins集成自动化测试
背景
实现每天自动运行自动化用例
 操作系统:macOS
安装jdk 与 jenkins
检查java
# 查看java版本
java -version
 
java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
 
安装jenkins
brew install jenkins
 
启动jenkins
#启动jenkins
brew services start jenkins                   1
==> Successfully started `jenkins` (label: homebrew.mxcl.jenkins)
 
#停止jenkins 
brew services stop jenkins
#重启jenkins
brew services restart jenkins
 
登录jenkins
# 启动jenkins后,获取密码
cat ~/.jenkins/secrets/initialAdminPassword
 
默认使用8080端口
 使用浏览器访问:http://localhost:8080
 
 安装推荐的插件
 
 
 新建管理员账户
 用户名:root
 密码:root
 全名:fs
 电子邮件地址:输入自己的邮箱
 
 
 url保持默认不变
 
 新建gitlib项目
 待补充
 新建本地gitlib项目
 待补充
新建jenkins项目
名称:fs auto test
 任务类型:Freestyle project
 
 直接保存,稍后依次修改项目配置
 
 
 设置包(构建)的保留天数和保留的最大个数

 修改ssh key
pbcopy < ~/.ssh/id_rsa
 
修改jenkins的Credentials

 
 类型、范围、Private Key固定,其他字段可以随意填写
 类型:SSH Username with private key
 范围:全局 (Jenkins, nodes, items, all child items, etc)
 ID: 1
 描述:gitlab ssh key
 Username:fs
 Private Key:粘贴id_rsa的私钥
保存
 
 修改jenkins时区
 
修改项目配置
修改源码配置
 Repository URL:填写gitlab的ssh地址
 Credentials:选择刚才配置的Credentials
 其他选项默认
 
 
修改触发器,每天定时22:00 触发一次
H 22 * * *
 

 修改构建环境
 
 修改构建
 使用shell命令触发
 source /etc/profile 缺少这个配置,会导致jenkins构建时找不到自己建立的模块跟包
 这种方式只适用于macos
#!/bin/bash
source /etc/profile
python3 main.py
 
/etc/profile内容
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
	eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
	[ -r /etc/bashrc ] && . /etc/bashrc
fi
 

 构建
 
遗留问题
- 在公司内网构建,提示“ImportError: No module named xxxx”,待解决
 - 在公司内网构建,写入kafka的消息体格式有变化,需要定位原因,待解决
 










