python的Jenkins接口调用方式

作者:很长很长的名字 时间:2022-02-23 11:26:23 

本来非常喜欢偷懒

最好就是不干活那种

所以最近在研究把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 = '''#本 * 自己的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的时候,需要对一些作业进行定时对构建

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接口调用方式

来源:https://blog.csdn.net/u011019726/article/details/52050191

标签:python,Jenkins,接口
0
投稿

猜你喜欢

  • asp的command对象的使用

    2008-06-23 13:03:00
  • Python+OpenCV内置方法实现行人检测

    2023-10-19 12:52:47
  • Python实现动态二维码生成的示例代码

    2022-08-03 15:45:39
  • python实战练习之最新男女颜值打分小系统

    2021-06-12 04:56:27
  • 客户端限制只能上传jpg格式图片的js代码

    2023-07-16 00:56:04
  • 30个最常用css选择器解析

    2011-06-16 20:36:37
  • python matplotlib绘画十一种常见数据分析图

    2022-09-02 04:50:41
  • django celery redis使用具体实践

    2022-12-03 02:45:39
  • 用于打印的页面设计

    2009-07-06 12:47:00
  • 修改SQL Server 2005 sa用户密码的方法

    2008-12-10 14:41:00
  • Java使用正则表达式(regex)匹配中文实例代码

    2023-06-17 07:59:46
  • 如何理解PHP程序执行的过程原理

    2023-10-08 14:45:10
  • django认证系统 Authentication使用详解

    2021-10-02 19:05:07
  • 王孟友教你如何设计标志(LOGO)

    2008-04-17 13:30:00
  • python3爬取各类天气信息

    2022-01-18 06:09:35
  • 谈谈Javascript中的++和–操作符

    2009-05-08 11:43:00
  • 详解Anaconda安装tensorflow报错问题解决方法

    2022-04-09 19:49:29
  • Python叠加两幅栅格图像的实现方法

    2023-07-25 17:14:06
  • python中正则表达式的使用方法

    2021-08-14 09:36:59
  • 基于python获取本地时间并转换时间戳和日期格式

    2021-02-23 17:30:06
  • asp之家 网络编程 m.aspxhome.com