官方api文档:https://python-jenkins.readthedocs.io/en/latest/api.html
使用jenkins的时候发现一个问题,虽然jenkins可以定时构建,但是构建的时候只能使用默认参数,或者新建一个定义好参数的构建任务。当本身jenkins上构建任务就比较多的情况下看起来就会比较繁杂,因此使用python做了一个自动化构建的脚本
一、安装
pip install python-jenkins
二、实际使用脚本
from jenkins import Jenkins
import sys, os
import datetime
import time
"""
定时自动在jinkins上进行构建操作
"""
# ================================================ 需要手动配置的区域
# trunk服务器更新(QA_trunk, RD_trunk)
job_name_server = "QA_更新服务器版本"
RD_dev_server = {
"branch_name":"dev-A",
"target_server":"dev_a",
"is_rebuild":True,
"restart_server":True
}
# 客户端更新(android_trunk)
job_name_android_dev = "got_global_android_devA"
RDdev_android = {
"label":"master",
"channel":"RDdev",
"version":"0.0.1",
"build_res":True,
"bigPackage":True,
"smallPackage":False,
"isOBBVersion":False,
"bundle_version_code":"1",
"log":True,
"development":False,
"submit_qm":True,
"branch":"dev-A",
"build_supersdk_channel":False,
"sdk_channel":"",
"obb_sdk_channel":"",
"resCompareVersion":"",
"isPoco":True
}
# 客户端更新(ios_trunk)
job_name_ios_dev = "got_global_ios_devA"
RDdev_ios = {
"label":"master",
"channel":"RDdev",
"version":"0.0.1",
"build_package":True,
"build_ipa":True,
"build_res":True,
"smallPackage":False,
"bundle_version_code":"1",
"log":True,
"development":False,
"submit_qm":True,
"branch":"dev_a",
"gt":False,
"build_sdk_channel":False,
"sdk_channel":"",
"resCompareVersion":""
}
build_time = [1, 13, 19] # 填入打包时间,24小时制1-23
# ================================================= 脚本区域
# 定时构建函数,传入自动构建任务
def timedBuild(jobName, buildingParm, buildType):QA
# 创建jenkins链接实例QA
jen_server = Jenkins(url="http://127.0.0.1:8080/", username="QA", password="QA") # ip隐藏
jen_client = Jenkins(url="http://127.0.0.2:8080//", username="QA", password="QA") # ip隐藏
if buildType == "client":
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), jobName, buildType)
jen_client.build_job(jobName, buildingParm) # 构建 客户端
else:
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), jobName, buildType)
jen_server.build_job(jobName, buildingParm) # 构建 服务器
if __name__ == '__main__':
while True:
now_time = datetime.datetime.now().strftime('%H') # 获取当前时间,只获取小时
if int(now_time) in build_time:
timedBuild(job_name_android_dev, RDdev_android, "client") # 打包安卓
timedBuild(job_name_ios_dev, RDdev_ios, "client") # 打包ios
timedBuild(job_name_server, RD_dev_server, "server") # 更新服务器 QAdev
print("finished build_package ~!")
time.sleep(3600)
三、常用函数说明
# 导入
import jenkins
# 实例化jenkins对象,连接远程的jenkins
server=jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token)
# 构建job名为job_name的job(不带构建参数)
server.build_job(job_name)
# String参数化构建job名为job_name的job, 参数param_dict为字典形式
server.build_job(job_name, parameters=param_dict)
#获取job名为job_name的job的相关信息
server.get_job_info(job_name)
# 获取job名为job_name的job的最后次构建号
server.get_job_info(job_name)['lastBuild']['number']
# 获取job名为job_name的job的某次构建的执行结果状态
server.get_build_info(job_name,build_number)['result']
# 判断job名为job_name的job的某次构建是否还在构建中
server.get_build_info(job_name,build_number)['building']
网友评论