前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python的Jenkins接口调用方式

python的Jenkins接口调用方式

作者头像
砸漏
发布2020-11-02 10:55:28
8930
发布2020-11-02 10:55:28
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本来非常喜欢偷懒

最好就是不干活那种

所以最近在研究把Jenkins模块集成起来

做成傻瓜界面这样就给他们用

本人Python搓望大神不要喷,多多指导

jenkins的Python模块模块安装

pip: pip install python-jenkins

easy_install: easy_install python-jenkins

使用:

代码语言:javascript
复制
class jenkins_tools():
def __init__(self):
cf = get_conf()
self.username = cf.get('jenkins', 'username')
self.password = cf.get('jenkins', 'password')
self.php_jenkins = '''			#本屌自己的jenkins的conf文件
<project 				#这里可以去抄jenkins的项目文件夹里面的配置文件
<actions/ 				#记得不要加xml头,源码哪里帮我们加了,自己加就是作死
<description </description 		#项目需求不一样,配置文件也不一样,你们不要抄我的
<keepDependencies false</keepDependencies 
<properties 
<hudson.model.ParametersDefinitionProperty 
<parameterDefinitions 
<hudson.model.StringParameterDefinition 
<name Branch</name 
<description </description 
<defaultValue %s</defaultValue 
</hudson.model.StringParameterDefinition 
</parameterDefinitions 
</hudson.model.ParametersDefinitionProperty 
</properties 
<scm class="hudson.scm.NullSCM"/ 
<canRoam true</canRoam 
<disabled false</disabled 
<blockBuildWhenDownstreamBuilding false</blockBuildWhenDownstreamBuilding 
<blockBuildWhenUpstreamBuilding false</blockBuildWhenUpstreamBuilding 
<triggers/ 
<concurrentBuild false</concurrentBuild 
<builders 
<hudson.tasks.Shell 
<command xxxxxxx</command 	
</hudson.tasks.Shell 
</builders 
<publishers/ 
<buildWrappers/ 
</project 
'''
self.java_newjenkins = '''		#本屌的另外一个jenkins的conf文件
<project 
<actions/ 
<description </description 
<keepDependencies false</keepDependencies 
<properties 
<hudson.model.ParametersDefinitionProperty 
<parameterDefinitions 
<hudson.model.StringParameterDefinition 
<name Branch</name 
<description </description 
<defaultValue %s</defaultValue 
</hudson.model.StringParameterDefinition 
</parameterDefinitions 
</hudson.model.ParametersDefinitionProperty 
</properties 
<scm class="hudson.scm.NullSCM"/ 
<canRoam true</canRoam 
<disabled false</disabled 
<blockBuildWhenDownstreamBuilding false</blockBuildWhenDownstreamBuilding 
<blockBuildWhenUpstreamBuilding false</blockBuildWhenUpstreamBuilding 
<triggers/ 
<concurrentBuild false</concurrentBuild 
<builders 
<hudson.tasks.Shell 
<command xxxx</command 	
</hudson.tasks.Shell 
</builders 
<publishers/ 
<buildWrappers/ 
</project 
'''
def __conn_jenkins_server(self, url):
try:
#获得一个jenkins的操作实例
server = jenkins.Jenkins(url, username=self.username, password=self.password)
return server
except Exception:
logging.warning('login jenkins failed!')
return None
def create_project(self, host_ip, project_name, git_path, git_branch, url, environment):
server = self.__conn_jenkins_server(url)
if server:
server.create_job(project_name, self.php_jenkins)	#参数1写的是项目名称,参数2是xml文档
return True
else:
return None
def project_built(self, url, project_name, git_branch):	#这个函数作用是构建项目
server = self.__conn_jenkins_server(url)		
server.build_job(project_name, {'Branch': git_branch})
def check_project_exist(self, project_name, url):		#这个函数是检查项目是否已经存在虽然写得很挫忘不要见怪
server = self.__conn_jenkins_server(url)
name = server.get_job_name(project_name)
if name is None:
return False
return True

详细可以看官方文档:http://python-jenkins.readthedocs.io/en/latest/api.html

补充知识:python调用jenkinsapi

在通过python 调用jenkinsapi的时候,需要对一些作业进行定时对构建

报错:

<title Error 403 No valid crumb was included in the request</title \n</head \n<body <h2 HTTP ERROR 403</h2

原因是在jenkins的安全配置里勾选里下面这个选项,在预防跨站点请求,将其勾掉即可。

以上这篇python的Jenkins接口调用方式就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档