目的:通过访问Jenkins得到各个项目的构建状态。
jenkins是一款持续集成的工具,插件众多,配置简单,跨平台。
使用python和jenkins的依赖包
pip install python-jenkins
-
jenkins连接配置
import jenkins jenkins_url = 'http://ip:port' jenkins_user = '' jenkins_passwd = '' jen = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_passwd) #实例化jenkins对象,然后使用jenkins对象做各种操作。
查询某job_name的最后一次构建信息
lastBuild_Number = jen.get_job_info(job_name)['lastBuild']['number']
lastBuild_url = jen.get_build_info(job_name, lastBuild_Number)['url']# 最后一次build号
times = jen.get_build_info(job_name, lastBuild_Number)['timestamp']# build的日期
result = jen.get_build_info(job_name, lastBuild_Number)['result']
得到项目scm是git还是svn
job_info_scm = jen.get_job_info(job_name)['scm']['_class']
#{'_class': 'hudson.plugins.git.GitSCM'}
if job_info_scm.find('git') != -1:
pci.pro_scm = 'git'
else:
pci.pro_scm = 'svn'
根据job_name 和 build_number更新构建状态
times = jen.get_build_info(job_name, build_number)['timestamp']
# build的日期
result = jen.get_build_info(job_name, build_number)['result']
获取jenkins中已有构建的tag信息
默认情况下是拿不到tag的信息,需修改jenkins的init.py 中方法
def get_build_info(self, name, number, depth=2):
#因我们只对构建成功的打tag
if result == 'SUCCESS':
if pci.pro_scm == 'git':
if jen.get_build_info(job_name, build_number)['actions'][2]['tags']:
tags = jen.get_build_info(job_name, build_number)['actions'][2]['tags'][0]['tag']
pci_build.build_tags = tags
logger.info('update_pci_build_by_jenkins' + 'job_name' + job_name + 'tags:' + tags)
else:
if jen.get_build_info(job_name, build_number)['actions'][1]['tags']:
tags = jen.get_build_info(job_name, build_number)['actions'][1]['tags'][0]['url']
pci_build.build_tags = tags
logger.info('update_pci_build_by_jenkins' + 'job_name' + job_name + 'tags:' + tags)