温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中怎么调用Jenkins接口

发布时间:2020-07-28 09:07:07 来源:亿速云 阅读:175 作者:小猪 栏目:开发技术

这篇文章主要讲解了python中怎么调用Jenkins接口,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

本来非常喜欢偷懒

最好就是不干活那种

所以最近在研究把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><h3>HTTP ERROR 403</h3>

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

python中怎么调用Jenkins接口

看完上述内容,是不是对python中怎么调用Jenkins接口有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI