python的Jenkins接口调用方式


Posted in Python onMay 12, 2020

本来非常喜欢偷懒

最好就是不干活那种

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

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

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

jenkins的Python模块模块安装

pip:
pip install python-jenkins

easy_install:
easy_install python-jenkins

使用:

class jenkins_tools():
  def __init__(self):
    cf = get_conf()
    self.username = cf.get('jenkins', 'username')
    self.password = cf.get('jenkins', 'password')
    self.php_jenkins = '''			#本?抛约旱?enkins的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 = '''		#本?诺牧硗庖桓?enkins的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的时候,需要对一些作业进行定时对构建

python的Jenkins接口调用方式

报错:

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

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

python的Jenkins接口调用方式

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

Python 相关文章推荐
用Python脚本生成Android SALT扰码的方法
Sep 18 Python
使用python解析xml成对应的html示例分享
Apr 02 Python
Python实现的数据结构与算法之快速排序详解
Apr 22 Python
浅谈MySQL中的触发器
May 05 Python
Python检测字符串中是否包含某字符集合中的字符
May 21 Python
Python自然语言处理 NLTK 库用法入门教程【经典】
Jun 26 Python
python 应用之Pycharm 新建模板默认添加编码格式-作者-时间等信息【推荐】
Jun 17 Python
Python list与NumPy array 区分详解
Nov 06 Python
对tensorflow 中tile函数的使用详解
Feb 07 Python
python使用gdal对shp读取,新建和更新的实例
Mar 10 Python
Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)
Jan 29 Python
Python实现文本文件拆分写入到多个文本文件的方法
Apr 18 Python
jenkins+python自动化测试持续集成教程
May 12 #Python
python百行代码自制电脑端网速悬浮窗的实现
May 12 #Python
基于Python的Jenkins的二次开发操作
May 12 #Python
Python-jenkins模块获取jobs的执行状态操作
May 12 #Python
Python-jenkins 获取job构建信息方式
May 12 #Python
python进行参数传递的方法
May 12 #Python
python输出数学符号实例
May 11 #Python
You might like
PHP判断一个gif图片是否为动态图片的方法
2014/11/19 PHP
基于php的CMS中展示文章类实例分析
2015/06/18 PHP
PHP中时间加减函数strtotime用法分析
2017/04/26 PHP
PHP获取数据库表中的数据插入新的表再原删除数据方法
2018/10/12 PHP
ThinkPHP5与单元测试PHPUnit使用详解
2020/02/23 PHP
jquery animate 动画效果使用说明
2009/11/04 Javascript
用jquery实现下拉菜单效果的代码
2010/07/25 Javascript
LABjs、RequireJS、SeaJS的区别
2014/03/04 Javascript
Centos6.8下Node.js安装教程
2017/05/12 Javascript
通过命令行创建vue项目的方法
2017/07/20 Javascript
JS解决IOS中拍照图片预览旋转90度BUG的问题
2017/09/13 Javascript
vue路由嵌套的SPA实现步骤
2017/11/06 Javascript
angularjs数组判断是否含有某个元素的实例
2018/02/27 Javascript
JavaScript+H5实现微信摇一摇功能
2018/05/23 Javascript
BootStrap模态框闪退问题实例代码详解
2018/12/10 Javascript
ES6顶层对象、global对象实例分析
2019/06/14 Javascript
详解element-ui设置下拉选择切换必填和非必填
2019/06/17 Javascript
Python标准库urllib2的一些使用细节总结
2015/03/16 Python
python设计模式大全
2016/06/27 Python
python遍历文件夹下所有excel文件
2018/01/03 Python
python OpenCV学习笔记直方图反向投影的实现
2018/02/07 Python
pandas DataFrame实现几列数据合并成为新的一列方法
2018/06/08 Python
Appium+Python自动化测试之运行App程序示例
2019/01/23 Python
python挖矿算力测试程序详解
2019/07/03 Python
jenkins配置python脚本定时任务过程图解
2019/10/29 Python
树莓派升级python的具体步骤
2020/07/05 Python
python制作一个简单的gui 数据库查询界面
2020/11/19 Python
美国现代家具购物网站:LexMod
2019/01/09 全球购物
建筑自我鉴定
2013/10/19 职场文书
求职自荐信怎么写
2014/03/06 职场文书
农村结婚典礼司仪主持词
2014/03/14 职场文书
护士个人年终总结
2015/02/13 职场文书
推普标语口号大全
2015/12/26 职场文书
Mysql 用户权限管理实现
2021/05/25 MySQL
Python中文纠错的简单实现
2021/07/07 Python
使用Redis做预定库存缓存功能
2022/04/02 Redis