python的Jenkins介面呼叫方式
阿新 • • 發佈:2020-05-13
本來非常喜歡偷懶
最好就是不幹活那種
所以最近在研究把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,git_branch): #這個函式作用是構建專案 server = self.__conn_jenkins_server(url) server.build_job(project_name,{'Branch': git_branch}) def check_project_exist(self,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介面呼叫方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。